cast.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. // Copyright 2021-2022 EMQ Technologies Co., Ltd.
  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 cast
  15. import (
  16. "fmt"
  17. "github.com/mitchellh/mapstructure"
  18. "reflect"
  19. "strconv"
  20. "sync"
  21. )
  22. type Strictness int8
  23. const (
  24. STRICT Strictness = iota
  25. CONVERT_SAMEKIND
  26. CONVERT_ALL
  27. )
  28. /*********** Type Cast Utilities *****/
  29. //TODO datetime type
  30. func ToStringAlways(input interface{}) string {
  31. if input == nil {
  32. return ""
  33. }
  34. return fmt.Sprintf("%v", input)
  35. }
  36. func ToString(input interface{}, sn Strictness) (string, error) {
  37. switch s := input.(type) {
  38. case string:
  39. return s, nil
  40. case []byte:
  41. return string(s), nil
  42. default:
  43. if sn == CONVERT_ALL {
  44. switch s := input.(type) {
  45. case string:
  46. return s, nil
  47. case bool:
  48. return strconv.FormatBool(s), nil
  49. case float64:
  50. return strconv.FormatFloat(s, 'f', -1, 64), nil
  51. case float32:
  52. return strconv.FormatFloat(float64(s), 'f', -1, 32), nil
  53. case int:
  54. return strconv.Itoa(s), nil
  55. case int64:
  56. return strconv.FormatInt(s, 10), nil
  57. case int32:
  58. return strconv.Itoa(int(s)), nil
  59. case int16:
  60. return strconv.FormatInt(int64(s), 10), nil
  61. case int8:
  62. return strconv.FormatInt(int64(s), 10), nil
  63. case uint:
  64. return strconv.FormatUint(uint64(s), 10), nil
  65. case uint64:
  66. return strconv.FormatUint(s, 10), nil
  67. case uint32:
  68. return strconv.FormatUint(uint64(s), 10), nil
  69. case uint16:
  70. return strconv.FormatUint(uint64(s), 10), nil
  71. case uint8:
  72. return strconv.FormatUint(uint64(s), 10), nil
  73. case nil:
  74. return "", nil
  75. case fmt.Stringer:
  76. return s.String(), nil
  77. case error:
  78. return s.Error(), nil
  79. }
  80. }
  81. }
  82. return "", fmt.Errorf("cannot convert %[1]T(%[1]v) to string", input)
  83. }
  84. func ToInt(input interface{}, sn Strictness) (int, error) {
  85. switch s := input.(type) {
  86. case int:
  87. return s, nil
  88. case int64:
  89. return int(s), nil
  90. case int32:
  91. return int(s), nil
  92. case int16:
  93. return int(s), nil
  94. case int8:
  95. return int(s), nil
  96. case uint:
  97. return int(s), nil
  98. case uint64:
  99. return int(s), nil
  100. case uint32:
  101. return int(s), nil
  102. case uint16:
  103. return int(s), nil
  104. case uint8:
  105. return int(s), nil
  106. case float64:
  107. if sn != STRICT || isIntegral64(s) {
  108. return int(s), nil
  109. }
  110. case float32:
  111. if sn != STRICT || isIntegral32(s) {
  112. return int(s), nil
  113. }
  114. case string:
  115. if sn == CONVERT_ALL {
  116. v, err := strconv.ParseInt(s, 0, 0)
  117. if err == nil {
  118. return int(v), nil
  119. }
  120. }
  121. case bool:
  122. if sn == CONVERT_ALL {
  123. if s {
  124. return 1, nil
  125. }
  126. return 0, nil
  127. }
  128. case nil:
  129. if sn == CONVERT_ALL {
  130. return 0, nil
  131. }
  132. }
  133. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to int", input)
  134. }
  135. func ToInt8(input interface{}, sn Strictness) (int8, error) {
  136. switch s := input.(type) {
  137. case int:
  138. return int8(s), nil
  139. case int64:
  140. return int8(s), nil
  141. case int32:
  142. return int8(s), nil
  143. case int16:
  144. return int8(s), nil
  145. case int8:
  146. return s, nil
  147. case uint:
  148. return int8(s), nil
  149. case uint64:
  150. return int8(s), nil
  151. case uint32:
  152. return int8(s), nil
  153. case uint16:
  154. return int8(s), nil
  155. case uint8:
  156. return int8(s), nil
  157. case float64:
  158. if sn != STRICT || isIntegral64(s) {
  159. return int8(s), nil
  160. }
  161. case float32:
  162. if sn != STRICT || isIntegral32(s) {
  163. return int8(s), nil
  164. }
  165. case string:
  166. if sn == CONVERT_ALL {
  167. v, err := strconv.ParseInt(s, 0, 0)
  168. if err == nil {
  169. return int8(v), nil
  170. }
  171. }
  172. case bool:
  173. if sn == CONVERT_ALL {
  174. if s {
  175. return 1, nil
  176. }
  177. return 0, nil
  178. }
  179. case nil:
  180. if sn == CONVERT_ALL {
  181. return 0, nil
  182. }
  183. }
  184. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to int", input)
  185. }
  186. func ToInt16(input interface{}, sn Strictness) (int16, error) {
  187. switch s := input.(type) {
  188. case int:
  189. return int16(s), nil
  190. case int64:
  191. return int16(s), nil
  192. case int32:
  193. return int16(s), nil
  194. case int16:
  195. return int16(s), nil
  196. case int8:
  197. return int16(s), nil
  198. case uint:
  199. return int16(s), nil
  200. case uint64:
  201. return int16(s), nil
  202. case uint32:
  203. return int16(s), nil
  204. case uint16:
  205. return int16(s), nil
  206. case uint8:
  207. return int16(s), nil
  208. case float64:
  209. if sn != STRICT || isIntegral64(s) {
  210. return int16(s), nil
  211. }
  212. case float32:
  213. if sn != STRICT || isIntegral32(s) {
  214. return int16(s), nil
  215. }
  216. case string:
  217. if sn == CONVERT_ALL {
  218. v, err := strconv.ParseInt(s, 0, 0)
  219. if err == nil {
  220. return int16(v), nil
  221. }
  222. }
  223. case bool:
  224. if sn == CONVERT_ALL {
  225. if s {
  226. return 1, nil
  227. }
  228. return 0, nil
  229. }
  230. case nil:
  231. if sn == CONVERT_ALL {
  232. return 0, nil
  233. }
  234. }
  235. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to int", input)
  236. }
  237. func ToInt32(input interface{}, sn Strictness) (int32, error) {
  238. switch s := input.(type) {
  239. case int:
  240. return int32(s), nil
  241. case int64:
  242. return int32(s), nil
  243. case int32:
  244. return s, nil
  245. case int16:
  246. return int32(s), nil
  247. case int8:
  248. return int32(s), nil
  249. case uint:
  250. return int32(s), nil
  251. case uint64:
  252. return int32(s), nil
  253. case uint32:
  254. return int32(s), nil
  255. case uint16:
  256. return int32(s), nil
  257. case uint8:
  258. return int32(s), nil
  259. case float64:
  260. if sn != STRICT || isIntegral64(s) {
  261. return int32(s), nil
  262. }
  263. case float32:
  264. if sn != STRICT || isIntegral32(s) {
  265. return int32(s), nil
  266. }
  267. case string:
  268. if sn == CONVERT_ALL {
  269. v, err := strconv.ParseInt(s, 0, 0)
  270. if err == nil {
  271. return int32(v), nil
  272. }
  273. }
  274. case bool:
  275. if sn == CONVERT_ALL {
  276. if s {
  277. return 1, nil
  278. }
  279. return 0, nil
  280. }
  281. case nil:
  282. if sn == CONVERT_ALL {
  283. return 0, nil
  284. }
  285. }
  286. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to int", input)
  287. }
  288. func ToInt64(input interface{}, sn Strictness) (int64, error) {
  289. switch s := input.(type) {
  290. case int:
  291. return int64(s), nil
  292. case int64:
  293. return s, nil
  294. case int32:
  295. return int64(s), nil
  296. case int16:
  297. return int64(s), nil
  298. case int8:
  299. return int64(s), nil
  300. case uint:
  301. return int64(s), nil
  302. case uint64:
  303. return int64(s), nil
  304. case uint32:
  305. return int64(s), nil
  306. case uint16:
  307. return int64(s), nil
  308. case uint8:
  309. return int64(s), nil
  310. case float64:
  311. if sn != STRICT || isIntegral64(s) {
  312. return int64(s), nil
  313. }
  314. case float32:
  315. if sn != STRICT || isIntegral32(s) {
  316. return int64(s), nil
  317. }
  318. case string:
  319. if sn == CONVERT_ALL {
  320. v, err := strconv.ParseInt(s, 0, 0)
  321. if err == nil {
  322. return int64(v), nil
  323. }
  324. }
  325. case bool:
  326. if sn == CONVERT_ALL {
  327. if s {
  328. return 1, nil
  329. }
  330. return 0, nil
  331. }
  332. case nil:
  333. if sn == CONVERT_ALL {
  334. return 0, nil
  335. }
  336. }
  337. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to int64", input)
  338. }
  339. func ToFloat64(input interface{}, sn Strictness) (float64, error) {
  340. switch s := input.(type) {
  341. case float64:
  342. return s, nil
  343. case float32:
  344. return float64(s), nil
  345. case int:
  346. if sn != STRICT {
  347. return float64(s), nil
  348. }
  349. case int64:
  350. if sn != STRICT {
  351. return float64(s), nil
  352. }
  353. case int32:
  354. if sn != STRICT {
  355. return float64(s), nil
  356. }
  357. case int16:
  358. if sn != STRICT {
  359. return float64(s), nil
  360. }
  361. case int8:
  362. if sn != STRICT {
  363. return float64(s), nil
  364. }
  365. case uint:
  366. if sn != STRICT {
  367. return float64(s), nil
  368. }
  369. case uint64:
  370. if sn != STRICT {
  371. return float64(s), nil
  372. }
  373. case uint32:
  374. if sn != STRICT {
  375. return float64(s), nil
  376. }
  377. case uint16:
  378. if sn != STRICT {
  379. return float64(s), nil
  380. }
  381. case uint8:
  382. if sn != STRICT {
  383. return float64(s), nil
  384. }
  385. case string:
  386. if sn == CONVERT_ALL {
  387. v, err := strconv.ParseFloat(s, 64)
  388. if err == nil {
  389. return v, nil
  390. }
  391. }
  392. case bool:
  393. if sn == CONVERT_ALL {
  394. if s {
  395. return 1, nil
  396. }
  397. return 0, nil
  398. }
  399. }
  400. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to float64", input)
  401. }
  402. func ToFloat32(input interface{}, sn Strictness) (float32, error) {
  403. switch s := input.(type) {
  404. case float64:
  405. return float32(s), nil
  406. case float32:
  407. return s, nil
  408. case int:
  409. if sn != STRICT {
  410. return float32(s), nil
  411. }
  412. case int64:
  413. if sn != STRICT {
  414. return float32(s), nil
  415. }
  416. case int32:
  417. if sn != STRICT {
  418. return float32(s), nil
  419. }
  420. case int16:
  421. if sn != STRICT {
  422. return float32(s), nil
  423. }
  424. case int8:
  425. if sn != STRICT {
  426. return float32(s), nil
  427. }
  428. case uint:
  429. if sn != STRICT {
  430. return float32(s), nil
  431. }
  432. case uint64:
  433. if sn != STRICT {
  434. return float32(s), nil
  435. }
  436. case uint32:
  437. if sn != STRICT {
  438. return float32(s), nil
  439. }
  440. case uint16:
  441. if sn != STRICT {
  442. return float32(s), nil
  443. }
  444. case uint8:
  445. if sn != STRICT {
  446. return float32(s), nil
  447. }
  448. case string:
  449. if sn == CONVERT_ALL {
  450. v, err := strconv.ParseFloat(s, 32)
  451. if err == nil {
  452. return float32(v), nil
  453. }
  454. }
  455. case bool:
  456. if sn == CONVERT_ALL {
  457. if s {
  458. return 1, nil
  459. }
  460. return 0, nil
  461. }
  462. }
  463. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to float64", input)
  464. }
  465. func ToUint64(i interface{}, sn Strictness) (uint64, error) {
  466. switch s := i.(type) {
  467. case string:
  468. if sn == CONVERT_ALL {
  469. v, err := strconv.ParseUint(s, 0, 64)
  470. if err == nil {
  471. return v, nil
  472. }
  473. }
  474. case int:
  475. if s < 0 {
  476. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  477. }
  478. return uint64(s), nil
  479. case int64:
  480. if s < 0 {
  481. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  482. }
  483. return uint64(s), nil
  484. case int32:
  485. if s < 0 {
  486. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  487. }
  488. return uint64(s), nil
  489. case int16:
  490. if s < 0 {
  491. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  492. }
  493. return uint64(s), nil
  494. case int8:
  495. if s < 0 {
  496. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  497. }
  498. return uint64(s), nil
  499. case uint:
  500. return uint64(s), nil
  501. case uint64:
  502. return s, nil
  503. case uint32:
  504. return uint64(s), nil
  505. case uint16:
  506. return uint64(s), nil
  507. case uint8:
  508. return uint64(s), nil
  509. case float32:
  510. if s < 0 {
  511. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  512. }
  513. if sn != STRICT || isIntegral32(s) {
  514. return uint64(s), nil
  515. }
  516. case float64:
  517. if s < 0 {
  518. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  519. }
  520. if sn != STRICT || isIntegral64(s) {
  521. return uint64(s), nil
  522. }
  523. case bool:
  524. if sn == CONVERT_ALL {
  525. if s {
  526. return 1, nil
  527. }
  528. return 0, nil
  529. }
  530. case nil:
  531. if sn == CONVERT_ALL {
  532. return 0, nil
  533. }
  534. }
  535. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint", i)
  536. }
  537. func ToUint8(i interface{}, sn Strictness) (uint8, error) {
  538. switch s := i.(type) {
  539. case string:
  540. if sn == CONVERT_ALL {
  541. v, err := strconv.ParseUint(s, 0, 64)
  542. if err == nil {
  543. return uint8(v), nil
  544. }
  545. }
  546. case int:
  547. if s < 0 {
  548. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  549. }
  550. return uint8(s), nil
  551. case int64:
  552. if s < 0 {
  553. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  554. }
  555. return uint8(s), nil
  556. case int32:
  557. if s < 0 {
  558. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  559. }
  560. return uint8(s), nil
  561. case int16:
  562. if s < 0 {
  563. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  564. }
  565. return uint8(s), nil
  566. case int8:
  567. if s < 0 {
  568. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  569. }
  570. return uint8(s), nil
  571. case uint:
  572. return uint8(s), nil
  573. case uint64:
  574. return uint8(s), nil
  575. case uint32:
  576. return uint8(s), nil
  577. case uint16:
  578. return uint8(s), nil
  579. case uint8:
  580. return s, nil
  581. case float32:
  582. if s < 0 {
  583. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  584. }
  585. if sn != STRICT || isIntegral32(s) {
  586. return uint8(s), nil
  587. }
  588. case float64:
  589. if s < 0 {
  590. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  591. }
  592. if sn != STRICT || isIntegral64(s) {
  593. return uint8(s), nil
  594. }
  595. case bool:
  596. if sn == CONVERT_ALL {
  597. if s {
  598. return 1, nil
  599. }
  600. return 0, nil
  601. }
  602. case nil:
  603. if sn == CONVERT_ALL {
  604. return 0, nil
  605. }
  606. }
  607. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint", i)
  608. }
  609. func ToUint16(i interface{}, sn Strictness) (uint16, error) {
  610. switch s := i.(type) {
  611. case string:
  612. if sn == CONVERT_ALL {
  613. v, err := strconv.ParseUint(s, 0, 64)
  614. if err == nil {
  615. return uint16(v), nil
  616. }
  617. }
  618. case int:
  619. if s < 0 {
  620. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  621. }
  622. return uint16(s), nil
  623. case int64:
  624. if s < 0 {
  625. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  626. }
  627. return uint16(s), nil
  628. case int32:
  629. if s < 0 {
  630. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  631. }
  632. return uint16(s), nil
  633. case int16:
  634. if s < 0 {
  635. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  636. }
  637. return uint16(s), nil
  638. case int8:
  639. if s < 0 {
  640. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  641. }
  642. return uint16(s), nil
  643. case uint:
  644. return uint16(s), nil
  645. case uint64:
  646. return uint16(s), nil
  647. case uint32:
  648. return uint16(s), nil
  649. case uint16:
  650. return s, nil
  651. case uint8:
  652. return uint16(s), nil
  653. case float32:
  654. if s < 0 {
  655. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  656. }
  657. if sn != STRICT || isIntegral32(s) {
  658. return uint16(s), nil
  659. }
  660. case float64:
  661. if s < 0 {
  662. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  663. }
  664. if sn != STRICT || isIntegral64(s) {
  665. return uint16(s), nil
  666. }
  667. case bool:
  668. if sn == CONVERT_ALL {
  669. if s {
  670. return 1, nil
  671. }
  672. return 0, nil
  673. }
  674. case nil:
  675. if sn == CONVERT_ALL {
  676. return 0, nil
  677. }
  678. }
  679. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint", i)
  680. }
  681. func ToUint32(i interface{}, sn Strictness) (uint32, error) {
  682. switch s := i.(type) {
  683. case string:
  684. if sn == CONVERT_ALL {
  685. v, err := strconv.ParseUint(s, 0, 64)
  686. if err == nil {
  687. return uint32(v), nil
  688. }
  689. }
  690. case int:
  691. if s < 0 {
  692. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  693. }
  694. return uint32(s), nil
  695. case int64:
  696. if s < 0 {
  697. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  698. }
  699. return uint32(s), nil
  700. case int32:
  701. if s < 0 {
  702. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  703. }
  704. return uint32(s), nil
  705. case int16:
  706. if s < 0 {
  707. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  708. }
  709. return uint32(s), nil
  710. case int8:
  711. if s < 0 {
  712. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  713. }
  714. return uint32(s), nil
  715. case uint:
  716. return uint32(s), nil
  717. case uint64:
  718. return uint32(s), nil
  719. case uint32:
  720. return s, nil
  721. case uint16:
  722. return uint32(s), nil
  723. case uint8:
  724. return uint32(s), nil
  725. case float32:
  726. if s < 0 {
  727. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  728. }
  729. if sn != STRICT || isIntegral32(s) {
  730. return uint32(s), nil
  731. }
  732. case float64:
  733. if s < 0 {
  734. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint, negative not allowed", i)
  735. }
  736. if sn != STRICT || isIntegral64(s) {
  737. return uint32(s), nil
  738. }
  739. case bool:
  740. if sn == CONVERT_ALL {
  741. if s {
  742. return 1, nil
  743. }
  744. return 0, nil
  745. }
  746. case nil:
  747. if sn == CONVERT_ALL {
  748. return 0, nil
  749. }
  750. }
  751. return 0, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint", i)
  752. }
  753. func ToBool(input interface{}, sn Strictness) (bool, error) {
  754. switch b := input.(type) {
  755. case bool:
  756. return b, nil
  757. case nil:
  758. if sn == CONVERT_ALL {
  759. return false, nil
  760. }
  761. case int:
  762. if sn == CONVERT_ALL {
  763. if b != 0 {
  764. return true, nil
  765. }
  766. return false, nil
  767. }
  768. case string:
  769. if sn == CONVERT_ALL {
  770. return strconv.ParseBool(b)
  771. }
  772. }
  773. return false, fmt.Errorf("cannot convert %[1]T(%[1]v) to bool", input)
  774. }
  775. func ToBytes(input interface{}, sn Strictness) ([]byte, error) {
  776. switch b := input.(type) {
  777. case []byte:
  778. return b, nil
  779. case string:
  780. if sn != STRICT {
  781. return []byte(b), nil
  782. }
  783. }
  784. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to bytes", input)
  785. }
  786. func ToStringMap(input interface{}) (map[string]interface{}, error) {
  787. var m = map[string]interface{}{}
  788. switch v := input.(type) {
  789. case map[interface{}]interface{}:
  790. for k, val := range v {
  791. m[ToStringAlways(k)] = val
  792. }
  793. return m, nil
  794. case map[string]interface{}:
  795. return v, nil
  796. //case string:
  797. // err := jsonStringToObject(v, &m)
  798. // return m, err
  799. default:
  800. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to map", input)
  801. }
  802. }
  803. func ToTypedSlice(input interface{}, conv func(interface{}, Strictness) (interface{}, error), eleType string, sn Strictness) (interface{}, error) {
  804. s := reflect.ValueOf(input)
  805. if s.Kind() != reflect.Slice {
  806. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to %s slice)", input, eleType)
  807. }
  808. if s.Len() == 0 {
  809. result := reflect.MakeSlice(reflect.TypeOf([]interface{}{}), s.Len(), s.Len())
  810. return result.Interface(), nil
  811. }
  812. ele, err := conv(s.Index(0).Interface(), sn)
  813. et := reflect.TypeOf(ele)
  814. if err != nil || et == nil {
  815. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to %s slice for the %d element: %v", input, eleType, 0, err)
  816. }
  817. result := reflect.MakeSlice(reflect.SliceOf(et), s.Len(), s.Len())
  818. result.Index(0).Set(reflect.ValueOf(ele))
  819. for i := 1; i < s.Len(); i++ {
  820. ele, err := conv(s.Index(i).Interface(), sn)
  821. if err != nil {
  822. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to int slice for the %d element: %v", input, i, err)
  823. }
  824. result.Index(i).Set(reflect.ValueOf(ele))
  825. }
  826. return result.Interface(), nil
  827. }
  828. func ToInt64Slice(input interface{}, sn Strictness) ([]int64, error) {
  829. s := reflect.ValueOf(input)
  830. if s.Kind() != reflect.Slice {
  831. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to int slice)", input)
  832. }
  833. var result []int64
  834. for i := 0; i < s.Len(); i++ {
  835. ele, err := ToInt64(s.Index(i).Interface(), sn)
  836. if err != nil {
  837. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to int slice for the %d element: %v", input, i, err)
  838. }
  839. result = append(result, ele)
  840. }
  841. return result, nil
  842. }
  843. func ToUint64Slice(input interface{}, sn Strictness) ([]uint64, error) {
  844. s := reflect.ValueOf(input)
  845. if s.Kind() != reflect.Slice {
  846. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint slice)", input)
  847. }
  848. var result []uint64
  849. for i := 0; i < s.Len(); i++ {
  850. ele, err := ToUint64(s.Index(i).Interface(), sn)
  851. if err != nil {
  852. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to uint slice for the %d element: %v", input, i, err)
  853. }
  854. result = append(result, ele)
  855. }
  856. return result, nil
  857. }
  858. func ToFloat64Slice(input interface{}, sn Strictness) ([]float64, error) {
  859. s := reflect.ValueOf(input)
  860. if s.Kind() != reflect.Slice {
  861. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to float slice)", input)
  862. }
  863. var result []float64
  864. for i := 0; i < s.Len(); i++ {
  865. ele, err := ToFloat64(s.Index(i).Interface(), sn)
  866. if err != nil {
  867. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to float slice for the %d element: %v", input, i, err)
  868. }
  869. result = append(result, ele)
  870. }
  871. return result, nil
  872. }
  873. func ToBoolSlice(input interface{}, sn Strictness) ([]bool, error) {
  874. s := reflect.ValueOf(input)
  875. if s.Kind() != reflect.Slice {
  876. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to bool slice)", input)
  877. }
  878. var result []bool
  879. for i := 0; i < s.Len(); i++ {
  880. ele, err := ToBool(s.Index(i).Interface(), sn)
  881. if err != nil {
  882. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to bool slice for the %d element: %v", input, i, err)
  883. }
  884. result = append(result, ele)
  885. }
  886. return result, nil
  887. }
  888. func ToStringSlice(input interface{}, sn Strictness) ([]string, error) {
  889. s := reflect.ValueOf(input)
  890. if s.Kind() != reflect.Slice {
  891. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to string slice)", input)
  892. }
  893. var result []string
  894. for i := 0; i < s.Len(); i++ {
  895. ele, err := ToString(s.Index(i).Interface(), sn)
  896. if err != nil {
  897. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to string slice for the %d element: %v", input, i, err)
  898. }
  899. result = append(result, ele)
  900. }
  901. return result, nil
  902. }
  903. func ToBytesSlice(input interface{}, sn Strictness) ([][]byte, error) {
  904. s := reflect.ValueOf(input)
  905. if s.Kind() != reflect.Slice {
  906. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to string slice)", input)
  907. }
  908. var result [][]byte
  909. for i := 0; i < s.Len(); i++ {
  910. ele, err := ToBytes(s.Index(i).Interface(), sn)
  911. if err != nil {
  912. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to bytes slice for the %d element: %v", input, i, err)
  913. }
  914. result = append(result, ele)
  915. }
  916. return result, nil
  917. }
  918. //MapToStruct
  919. /*
  920. * Convert a map into a struct. The output parameter must be a pointer to a struct
  921. * The struct can have the json meta data
  922. */
  923. func MapToStruct(input, output interface{}) error {
  924. config := &mapstructure.DecoderConfig{
  925. TagName: "json",
  926. Result: output,
  927. }
  928. decoder, err := mapstructure.NewDecoder(config)
  929. if err != nil {
  930. return err
  931. }
  932. return decoder.Decode(input)
  933. }
  934. // MapToStructStrict
  935. /*
  936. * Convert a map into a struct. The output parameter must be a pointer to a struct
  937. * If the input have key/value pair output do not defined, will report error
  938. */
  939. func MapToStructStrict(input, output interface{}) error {
  940. config := &mapstructure.DecoderConfig{
  941. ErrorUnused: true,
  942. TagName: "json",
  943. Result: output,
  944. }
  945. decoder, err := mapstructure.NewDecoder(config)
  946. if err != nil {
  947. return err
  948. }
  949. return decoder.Decode(input)
  950. }
  951. func ConvertMap(s map[interface{}]interface{}) map[string]interface{} {
  952. r := make(map[string]interface{})
  953. for k, v := range s {
  954. switch t := v.(type) {
  955. case map[interface{}]interface{}:
  956. v = ConvertMap(t)
  957. case []interface{}:
  958. v = ConvertArray(t)
  959. }
  960. r[fmt.Sprintf("%v", k)] = v
  961. }
  962. return r
  963. }
  964. func ConvertArray(s []interface{}) []interface{} {
  965. r := make([]interface{}, len(s))
  966. for i, e := range s {
  967. switch t := e.(type) {
  968. case map[interface{}]interface{}:
  969. e = ConvertMap(t)
  970. case []interface{}:
  971. e = ConvertArray(t)
  972. }
  973. r[i] = e
  974. }
  975. return r
  976. }
  977. func SyncMapToMap(sm *sync.Map) map[string]interface{} {
  978. m := make(map[string]interface{})
  979. sm.Range(func(k interface{}, v interface{}) bool {
  980. m[fmt.Sprintf("%v", k)] = v
  981. return true
  982. })
  983. return m
  984. }
  985. func MapToSyncMap(m map[string]interface{}) *sync.Map {
  986. sm := new(sync.Map)
  987. for k, v := range m {
  988. sm.Store(k, v)
  989. }
  990. return sm
  991. }
  992. func isIntegral64(val float64) bool {
  993. return val == float64(int(val))
  994. }
  995. func isIntegral32(val float32) bool {
  996. return val == float32(int(val))
  997. }
  998. func ConvertToInterfaceArr(orig map[string]interface{}) map[string]interface{} {
  999. result := make(map[string]interface{})
  1000. for k, v := range orig {
  1001. vt := reflect.TypeOf(v)
  1002. if vt == nil {
  1003. result[k] = nil
  1004. continue
  1005. }
  1006. switch vt.Kind() {
  1007. case reflect.Slice:
  1008. result[k] = ConvertSlice(v)
  1009. case reflect.Map:
  1010. result[k] = ConvertToInterfaceArr(v.(map[string]interface{}))
  1011. default:
  1012. result[k] = v
  1013. }
  1014. }
  1015. return result
  1016. }
  1017. func ConvertSlice(v interface{}) []interface{} {
  1018. value := reflect.ValueOf(v)
  1019. tempArr := make([]interface{}, value.Len())
  1020. for i := 0; i < value.Len(); i++ {
  1021. item := value.Index(i)
  1022. if item.Kind() == reflect.Map {
  1023. tempArr[i] = ConvertToInterfaceArr(item.Interface().(map[string]interface{}))
  1024. } else if item.Kind() == reflect.Slice {
  1025. tempArr[i] = ConvertSlice(item.Interface())
  1026. } else {
  1027. tempArr[i] = item.Interface()
  1028. }
  1029. }
  1030. return tempArr
  1031. }