reflect.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package sqlchemy
  15. import (
  16. "database/sql"
  17. "fmt"
  18. "reflect"
  19. "strconv"
  20. "strings"
  21. "time"
  22. "yunion.io/x/jsonutils"
  23. "yunion.io/x/pkg/errors"
  24. "yunion.io/x/pkg/gotypes"
  25. "yunion.io/x/pkg/tristate"
  26. "yunion.io/x/pkg/util/timeutils"
  27. )
  28. func getQuoteStringValue(dat interface{}) string {
  29. value := reflect.ValueOf(dat)
  30. switch value.Kind() {
  31. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  32. return fmt.Sprintf("%d", value.Int())
  33. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  34. return fmt.Sprintf("%d", value.Uint())
  35. case reflect.Float32, reflect.Float64:
  36. return fmt.Sprintf("%f", value.Float())
  37. }
  38. strVal := GetStringValue(dat)
  39. strVal = strings.ReplaceAll(strVal, "'", "\\'")
  40. return "'" + strVal + "'"
  41. }
  42. func GetStringValue(dat interface{}) string {
  43. switch g := dat.(type) {
  44. case tristate.TriState:
  45. return g.String()
  46. case time.Time:
  47. return timeutils.MysqlTime(g)
  48. case []byte:
  49. return string(g)
  50. }
  51. value := reflect.Indirect(reflect.ValueOf(dat))
  52. switch value.Kind() {
  53. case reflect.Bool:
  54. if value.Bool() {
  55. return "true"
  56. }
  57. return "false"
  58. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  59. return fmt.Sprintf("%d", value.Int())
  60. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  61. return fmt.Sprintf("%d", value.Uint())
  62. case reflect.Float32, reflect.Float64:
  63. return fmt.Sprintf("%v", value.Float())
  64. case reflect.String:
  65. return value.String()
  66. }
  67. serializable, ok := value.Interface().(gotypes.ISerializable)
  68. if ok {
  69. return serializable.String()
  70. } else {
  71. return jsonutils.Marshal(value.Interface()).String()
  72. }
  73. }
  74. func setValueBySQLString(value reflect.Value, val string) error {
  75. if !value.CanSet() {
  76. return errors.Wrap(ErrReadOnly, "value is not settable")
  77. }
  78. switch value.Type() {
  79. case tristate.TriStateType:
  80. if val == "1" {
  81. value.Set(tristate.TriStateTrueValue)
  82. } else if val == "0" {
  83. value.Set(tristate.TriStateFalseValue)
  84. } else {
  85. value.Set(tristate.TriStateNoneValue)
  86. }
  87. return nil
  88. case gotypes.TimeType:
  89. if val != "0000-00-00 00:00:00" {
  90. tm, err := timeutils.ParseTimeStr(val)
  91. if err != nil {
  92. return errors.Wrap(err, "ParseTimeStr")
  93. }
  94. value.Set(reflect.ValueOf(tm))
  95. }
  96. return nil
  97. }
  98. switch value.Kind() {
  99. case reflect.Bool:
  100. if val == "0" {
  101. value.SetBool(false)
  102. } else {
  103. value.SetBool(true)
  104. }
  105. return nil
  106. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  107. valInt, err := strconv.ParseInt(val, 10, 64)
  108. if err != nil {
  109. return errors.Wrap(err, "ParseInt")
  110. }
  111. value.SetInt(valInt)
  112. return nil
  113. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  114. valUint, err := strconv.ParseUint(val, 10, 64)
  115. if err != nil {
  116. return errors.Wrap(err, "ParseUint")
  117. }
  118. value.SetUint(valUint)
  119. return nil
  120. case reflect.Float32, reflect.Float64:
  121. valFloat, err := strconv.ParseFloat(val, 64)
  122. if err != nil {
  123. return errors.Wrap(err, "ParseFloat")
  124. }
  125. value.SetFloat(valFloat)
  126. return nil
  127. case reflect.String:
  128. value.SetString(val)
  129. return nil
  130. case reflect.Slice:
  131. jsonV, err := jsonutils.ParseString(val)
  132. if err != nil {
  133. return errors.Wrapf(err, "jsonutils.ParseString %s", val)
  134. }
  135. if jsonV == jsonutils.JSONNull {
  136. return nil
  137. }
  138. jsonA, err := jsonV.GetArray()
  139. if err != nil {
  140. return errors.Wrap(err, "jsonV.GetArray")
  141. }
  142. sliceValue := reflect.MakeSlice(value.Type(), 0, len(jsonA))
  143. value.Set(sliceValue)
  144. for i := range jsonA {
  145. elemValue := reflect.New(value.Type().Elem()).Elem()
  146. jsonStr, _ := jsonA[i].GetString()
  147. err := setValueBySQLString(elemValue, jsonStr)
  148. if err != nil {
  149. return errors.Wrapf(err, "TestSetValueBySQLString %s", jsonA[i].String())
  150. }
  151. value.Set(reflect.Append(value, elemValue))
  152. }
  153. return nil
  154. case reflect.Map:
  155. jsonV, err := jsonutils.ParseString(val)
  156. if err != nil {
  157. return errors.Wrapf(err, "jsonutils.ParseString %s", val)
  158. }
  159. if jsonV == jsonutils.JSONNull {
  160. return nil
  161. }
  162. mapValue := reflect.MakeMap(value.Type())
  163. value.Set(mapValue)
  164. err = jsonV.Unmarshal(mapValue.Interface())
  165. if err != nil {
  166. return errors.Wrapf(err, "jsonV.Unmarshal")
  167. }
  168. return nil
  169. default:
  170. if valueType := value.Type(); valueType.Implements(gotypes.ISerializableType) {
  171. serializable, err := jsonutils.JSONDeserialize(valueType, val)
  172. if err != nil {
  173. return err
  174. }
  175. value.Set(reflect.ValueOf(serializable))
  176. return nil
  177. } else if value.Kind() == reflect.Ptr {
  178. if value.IsNil() {
  179. value.Set(reflect.New(value.Type().Elem()))
  180. }
  181. return setValueBySQLString(value.Elem(), val)
  182. } else {
  183. jsonV, err := jsonutils.ParseString(val)
  184. if err != nil {
  185. return errors.Wrapf(err, "%s not a json string: %s", val, err)
  186. }
  187. if jsonV == jsonutils.JSONNull {
  188. return nil
  189. }
  190. newVal := reflect.New(value.Type())
  191. err = jsonV.Unmarshal(newVal.Interface())
  192. if err != nil {
  193. return errors.Wrap(err, "Unmarshal fail")
  194. }
  195. value.Set(reflect.Indirect(newVal))
  196. return nil
  197. }
  198. }
  199. }
  200. func ConvertValueToTime(val interface{}) time.Time {
  201. switch v := val.(type) {
  202. case string:
  203. tm, _ := timeutils.ParseTimeStr(v)
  204. return tm
  205. case time.Time:
  206. return v
  207. case int:
  208. return time.Unix(int64(v), 0)
  209. case int32:
  210. return time.Unix(int64(v), 0)
  211. case int64:
  212. return time.Unix(int64(v), 0)
  213. case uint:
  214. return time.Unix(int64(v), 0)
  215. case uint32:
  216. return time.Unix(int64(v), 0)
  217. case uint64:
  218. return time.Unix(int64(v), 0)
  219. case float32:
  220. return time.Unix(int64(v), int64((v-float32(int64(v)))*1000000000))
  221. case float64:
  222. return time.Unix(int64(v), int64((v-float64(int64(v)))*1000000000))
  223. case *string:
  224. tm, _ := timeutils.ParseTimeStr(*v)
  225. return tm
  226. case *time.Time:
  227. return *v
  228. case *int:
  229. return time.Unix(int64(*v), 0)
  230. case *int32:
  231. return time.Unix(int64(*v), 0)
  232. case *int64:
  233. return time.Unix(int64(*v), 0)
  234. case *uint:
  235. return time.Unix(int64(*v), 0)
  236. case *uint32:
  237. return time.Unix(int64(*v), 0)
  238. case *uint64:
  239. return time.Unix(int64(*v), 0)
  240. case *float32:
  241. return time.Unix(int64(*v), int64((*v-float32(int64(*v)))*1000000000))
  242. case *float64:
  243. return time.Unix(int64(*v), int64((*v-float64(int64(*v)))*1000000000))
  244. }
  245. return time.Time{}
  246. }
  247. func ConvertValueToInteger(val interface{}) int64 {
  248. switch v := val.(type) {
  249. case string:
  250. intv, _ := strconv.ParseInt(v, 10, 64)
  251. return intv
  252. case int:
  253. return int64(v)
  254. case int32:
  255. return int64(v)
  256. case int64:
  257. return v
  258. case uint:
  259. return int64(v)
  260. case uint32:
  261. return int64(v)
  262. case uint64:
  263. return int64(v)
  264. case float32:
  265. return int64(v)
  266. case float64:
  267. return int64(v)
  268. case time.Time:
  269. return v.Unix()
  270. case *string:
  271. intv, _ := strconv.ParseInt(*v, 10, 64)
  272. return intv
  273. case *int:
  274. return int64(*v)
  275. case *int32:
  276. return int64(*v)
  277. case *int64:
  278. return *v
  279. case *uint:
  280. return int64(*v)
  281. case *uint32:
  282. return int64(*v)
  283. case *uint64:
  284. return int64(*v)
  285. case *float32:
  286. return int64(*v)
  287. case *float64:
  288. return int64(*v)
  289. case *time.Time:
  290. return v.Unix()
  291. }
  292. return 0
  293. }
  294. func ConvertValueToFloat(val interface{}) float64 {
  295. switch v := val.(type) {
  296. case string:
  297. intv, _ := strconv.ParseFloat(v, 64)
  298. return intv
  299. case int:
  300. return float64(v)
  301. case int32:
  302. return float64(v)
  303. case int64:
  304. return float64(v)
  305. case uint:
  306. return float64(v)
  307. case uint32:
  308. return float64(v)
  309. case uint64:
  310. return float64(v)
  311. case float32:
  312. return float64(v)
  313. case float64:
  314. return v
  315. case time.Time:
  316. return float64(v.Unix())
  317. case *string:
  318. intv, _ := strconv.ParseFloat(*v, 64)
  319. return intv
  320. case *int:
  321. return float64(*v)
  322. case *int32:
  323. return float64(*v)
  324. case *int64:
  325. return float64(*v)
  326. case *uint:
  327. return float64(*v)
  328. case *uint32:
  329. return float64(*v)
  330. case *uint64:
  331. return float64(*v)
  332. case *float32:
  333. return float64(*v)
  334. case *float64:
  335. return *v
  336. case *time.Time:
  337. return float64(v.Unix())
  338. }
  339. return 0
  340. }
  341. func ConvertValueToBool(val interface{}) bool {
  342. switch v := val.(type) {
  343. case string:
  344. v = strings.ToLower(v)
  345. return v == "1" || v == "true" || v == "ok" || v == "yes" || v == "on"
  346. case bool:
  347. return v
  348. case tristate.TriState:
  349. return v == tristate.True
  350. case int8:
  351. return v > 0
  352. case int16:
  353. return v > 0
  354. case int:
  355. return v > 0
  356. case int32:
  357. return v > 0
  358. case int64:
  359. return v > 0
  360. case uint8:
  361. return v > 0
  362. case uint16:
  363. return v > 0
  364. case uint:
  365. return v > 0
  366. case uint32:
  367. return v > 0
  368. case uint64:
  369. return v > 0
  370. case float32:
  371. return v > 0
  372. case float64:
  373. return v > 0
  374. case *string:
  375. if gotypes.IsNil(v) {
  376. return false
  377. }
  378. nv := strings.ToLower(*v)
  379. return nv == "1" || nv == "true" || nv == "ok" || nv == "yes" || nv == "on"
  380. case *bool:
  381. if gotypes.IsNil(v) {
  382. return false
  383. }
  384. return *v
  385. case *tristate.TriState:
  386. if gotypes.IsNil(v) {
  387. return false
  388. }
  389. return *v == tristate.True
  390. case *int8:
  391. if gotypes.IsNil(v) {
  392. return false
  393. }
  394. return *v > 0
  395. case *int16:
  396. if gotypes.IsNil(v) {
  397. return false
  398. }
  399. return *v > 0
  400. case *int:
  401. if gotypes.IsNil(v) {
  402. return false
  403. }
  404. return *v > 0
  405. case *int32:
  406. if gotypes.IsNil(v) {
  407. return false
  408. }
  409. return *v > 0
  410. case *int64:
  411. if gotypes.IsNil(v) {
  412. return false
  413. }
  414. return *v > 0
  415. case *uint8:
  416. if gotypes.IsNil(v) {
  417. return false
  418. }
  419. return *v > 0
  420. case *uint16:
  421. if gotypes.IsNil(v) {
  422. return false
  423. }
  424. return *v > 0
  425. case *uint:
  426. if gotypes.IsNil(v) {
  427. return false
  428. }
  429. return *v > 0
  430. case *uint32:
  431. if gotypes.IsNil(v) {
  432. return false
  433. }
  434. return *v > 0
  435. case *uint64:
  436. if gotypes.IsNil(v) {
  437. return false
  438. }
  439. return *v > 0
  440. case *float32:
  441. if gotypes.IsNil(v) {
  442. return false
  443. }
  444. return *v > 0
  445. case *float64:
  446. if gotypes.IsNil(v) {
  447. return false
  448. }
  449. return *v > 0
  450. }
  451. return false
  452. }
  453. func ConvertValueToTriState(val interface{}) tristate.TriState {
  454. switch v := val.(type) {
  455. case tristate.TriState:
  456. return v
  457. case string:
  458. switch strings.ToLower(v) {
  459. case "true", "yes", "on", "ok", "1":
  460. return tristate.True
  461. case "none", "null", "unknown":
  462. return tristate.None
  463. default:
  464. return tristate.False
  465. }
  466. case bool:
  467. if v {
  468. return tristate.True
  469. } else {
  470. return tristate.False
  471. }
  472. case sql.NullInt32:
  473. if v.Valid {
  474. return ConvertValueToTriState(v.Int32)
  475. }
  476. return tristate.None
  477. case int8:
  478. switch {
  479. case v > 0:
  480. return tristate.True
  481. default:
  482. return tristate.False
  483. }
  484. case int16:
  485. switch {
  486. case v > 0:
  487. return tristate.True
  488. default:
  489. return tristate.False
  490. }
  491. case int:
  492. switch {
  493. case v > 0:
  494. return tristate.True
  495. default:
  496. return tristate.False
  497. }
  498. case int32:
  499. switch {
  500. case v > 0:
  501. return tristate.True
  502. default:
  503. return tristate.False
  504. }
  505. case int64:
  506. switch {
  507. case v > 0:
  508. return tristate.True
  509. default:
  510. return tristate.False
  511. }
  512. case uint8:
  513. switch {
  514. case v > 0:
  515. return tristate.True
  516. default:
  517. return tristate.False
  518. }
  519. case uint16:
  520. switch {
  521. case v > 0:
  522. return tristate.True
  523. default:
  524. return tristate.False
  525. }
  526. case uint:
  527. switch {
  528. case v > 0:
  529. return tristate.True
  530. default:
  531. return tristate.False
  532. }
  533. case uint32:
  534. switch {
  535. case v > 0:
  536. return tristate.True
  537. default:
  538. return tristate.False
  539. }
  540. case uint64:
  541. switch {
  542. case v > 0:
  543. return tristate.True
  544. default:
  545. return tristate.False
  546. }
  547. case float32:
  548. switch {
  549. case v > 0:
  550. return tristate.True
  551. default:
  552. return tristate.False
  553. }
  554. case float64:
  555. switch {
  556. case v > 0:
  557. return tristate.True
  558. default:
  559. return tristate.False
  560. }
  561. case *string:
  562. if gotypes.IsNil(v) {
  563. return tristate.None
  564. }
  565. switch strings.ToLower(*v) {
  566. case "true", "yes", "on", "ok", "1":
  567. return tristate.True
  568. case "none", "null", "unknown":
  569. return tristate.None
  570. default:
  571. return tristate.False
  572. }
  573. case *bool:
  574. if gotypes.IsNil(v) {
  575. return tristate.None
  576. }
  577. if *v {
  578. return tristate.True
  579. } else {
  580. return tristate.False
  581. }
  582. case *tristate.TriState:
  583. return *v
  584. case *int8:
  585. if gotypes.IsNil(v) {
  586. return tristate.None
  587. }
  588. if *v > 0 {
  589. return tristate.True
  590. } else {
  591. return tristate.False
  592. }
  593. case *int16:
  594. if gotypes.IsNil(v) {
  595. return tristate.None
  596. }
  597. if *v > 0 {
  598. return tristate.True
  599. } else {
  600. return tristate.False
  601. }
  602. case *int:
  603. if gotypes.IsNil(v) {
  604. return tristate.None
  605. }
  606. if *v > 0 {
  607. return tristate.True
  608. } else {
  609. return tristate.False
  610. }
  611. case *int32:
  612. if gotypes.IsNil(v) {
  613. return tristate.None
  614. }
  615. if *v > 0 {
  616. return tristate.True
  617. } else {
  618. return tristate.False
  619. }
  620. case *int64:
  621. if gotypes.IsNil(v) {
  622. return tristate.None
  623. }
  624. if *v > 0 {
  625. return tristate.True
  626. } else {
  627. return tristate.False
  628. }
  629. case *uint8:
  630. if gotypes.IsNil(v) {
  631. return tristate.None
  632. }
  633. if *v > 0 {
  634. return tristate.True
  635. } else {
  636. return tristate.False
  637. }
  638. case *uint16:
  639. if gotypes.IsNil(v) {
  640. return tristate.None
  641. }
  642. if *v > 0 {
  643. return tristate.True
  644. } else {
  645. return tristate.False
  646. }
  647. case *uint:
  648. if gotypes.IsNil(v) {
  649. return tristate.None
  650. }
  651. if *v > 0 {
  652. return tristate.True
  653. } else {
  654. return tristate.False
  655. }
  656. case *uint32:
  657. if gotypes.IsNil(v) {
  658. return tristate.None
  659. }
  660. if *v > 0 {
  661. return tristate.True
  662. } else {
  663. return tristate.False
  664. }
  665. case *uint64:
  666. if gotypes.IsNil(v) {
  667. return tristate.None
  668. }
  669. if *v > 0 {
  670. return tristate.True
  671. } else {
  672. return tristate.False
  673. }
  674. case *float32:
  675. if gotypes.IsNil(v) {
  676. return tristate.None
  677. }
  678. if *v > 0 {
  679. return tristate.True
  680. } else {
  681. return tristate.False
  682. }
  683. case *float64:
  684. if gotypes.IsNil(v) {
  685. return tristate.None
  686. }
  687. if *v > 0 {
  688. return tristate.True
  689. } else {
  690. return tristate.False
  691. }
  692. }
  693. return tristate.None
  694. }
  695. func ConvertValueToString(val interface{}) string {
  696. if gotypes.IsNil(val) {
  697. return ""
  698. }
  699. switch v := val.(type) {
  700. case string:
  701. return v
  702. case int8, int16, int, int32, int64, uint8, uint16, uint, uint32, uint64:
  703. return fmt.Sprintf("%d", v)
  704. case float32, float64:
  705. return fmt.Sprintf("%f", v)
  706. case bool:
  707. return fmt.Sprintf("%v", v)
  708. case time.Time:
  709. return timeutils.IsoTime(v)
  710. case *string:
  711. return *v
  712. case *int8:
  713. return fmt.Sprintf("%d", *v)
  714. case *int16:
  715. return fmt.Sprintf("%d", *v)
  716. case *int:
  717. return fmt.Sprintf("%d", *v)
  718. case *int32:
  719. return fmt.Sprintf("%d", *v)
  720. case *int64:
  721. return fmt.Sprintf("%d", *v)
  722. case *uint8:
  723. return fmt.Sprintf("%d", *v)
  724. case *uint16:
  725. return fmt.Sprintf("%d", *v)
  726. case *uint:
  727. return fmt.Sprintf("%d", *v)
  728. case *uint32:
  729. return fmt.Sprintf("%d", *v)
  730. case *uint64:
  731. return fmt.Sprintf("%d", *v)
  732. case *float32:
  733. return fmt.Sprintf("%f", *v)
  734. case *float64:
  735. return fmt.Sprintf("%f", *v)
  736. case *time.Time:
  737. return timeutils.IsoTime(*v)
  738. case tristate.TriState:
  739. return v.String()
  740. case *tristate.TriState:
  741. return (*v).String()
  742. }
  743. if reflect.ValueOf(val).Kind() == reflect.String {
  744. return val.(string)
  745. }
  746. return jsonutils.Marshal(val).String()
  747. }
  748. func getFloatValue(val interface{}) (float64, error) {
  749. switch v := val.(type) {
  750. case float32:
  751. return float64(v), nil
  752. case float64:
  753. return v, nil
  754. case int:
  755. return float64(v), nil
  756. case int8:
  757. return float64(v), nil
  758. case int16:
  759. return float64(v), nil
  760. case int32:
  761. return float64(v), nil
  762. case int64:
  763. return float64(v), nil
  764. case uint:
  765. return float64(v), nil
  766. case uint8:
  767. return float64(v), nil
  768. case uint16:
  769. return float64(v), nil
  770. case uint32:
  771. return float64(v), nil
  772. case uint64:
  773. return float64(v), nil
  774. case *float32:
  775. return float64(*v), nil
  776. case *float64:
  777. return *v, nil
  778. case *int:
  779. return float64(*v), nil
  780. case *int8:
  781. return float64(*v), nil
  782. case *int16:
  783. return float64(*v), nil
  784. case *int32:
  785. return float64(*v), nil
  786. case *int64:
  787. return float64(*v), nil
  788. case *uint:
  789. return float64(*v), nil
  790. case *uint8:
  791. return float64(*v), nil
  792. case *uint16:
  793. return float64(*v), nil
  794. case *uint32:
  795. return float64(*v), nil
  796. case *uint64:
  797. return float64(*v), nil
  798. case string:
  799. return strconv.ParseFloat(v, 64)
  800. case *string:
  801. return strconv.ParseFloat(*v, 64)
  802. }
  803. return 0, errors.ErrInvalidFormat
  804. }
  805. func getTimeValue(val interface{}) (time.Time, error) {
  806. switch v := val.(type) {
  807. case time.Time:
  808. return v, nil
  809. case *time.Time:
  810. return *v, nil
  811. case string:
  812. return timeutils.ParseTimeStr(v)
  813. case *string:
  814. return timeutils.ParseTimeStr(*v)
  815. }
  816. return time.Time{}, errors.ErrInvalidFormat
  817. }
  818. const MIN_FLOAT_EQUAL_DIFF = float64(0.000001)
  819. func floatEqual(of, nf float64) bool {
  820. if of > nf {
  821. return of < MIN_FLOAT_EQUAL_DIFF+nf
  822. } else if of < nf {
  823. return of+MIN_FLOAT_EQUAL_DIFF > nf
  824. } else {
  825. return true
  826. }
  827. }
  828. const MIN_MICRO_SECOND_EQUAL_DIFF = 1000000
  829. func timeEqual(of, nf time.Time) bool {
  830. ofUnix := of.UnixMicro()
  831. nfUnix := nf.UnixMicro()
  832. if ofUnix == nfUnix {
  833. return true
  834. }
  835. if ofUnix > nfUnix {
  836. return ofUnix < MIN_MICRO_SECOND_EQUAL_DIFF+nfUnix
  837. } else {
  838. return ofUnix+MIN_MICRO_SECOND_EQUAL_DIFF > nfUnix
  839. }
  840. }
  841. func EqualsGrossValue(of, nf interface{}) bool {
  842. ofFloat, ofErr := getFloatValue(of)
  843. nfFloat, nfErr := getFloatValue(nf)
  844. if ofErr == nil && nfErr == nil {
  845. return floatEqual(ofFloat, nfFloat)
  846. }
  847. ofTime, ofErr := getTimeValue(of)
  848. nfTime, nfErr := getTimeValue(nf)
  849. if ofErr == nil && nfErr == nil {
  850. return timeEqual(ofTime, nfTime)
  851. }
  852. return false
  853. }