cast.go 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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 ToFloat32Slice(input interface{}, sn Strictness) ([]float32, error) {
  874. s := reflect.ValueOf(input)
  875. if s.Kind() != reflect.Slice {
  876. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to float slice)", input)
  877. }
  878. var result []float32
  879. for i := 0; i < s.Len(); i++ {
  880. ele, err := ToFloat32(s.Index(i).Interface(), sn)
  881. if err != nil {
  882. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to float slice for the %d element: %v", input, i, err)
  883. }
  884. result = append(result, ele)
  885. }
  886. return result, nil
  887. }
  888. func ToBoolSlice(input interface{}, sn Strictness) ([]bool, error) {
  889. s := reflect.ValueOf(input)
  890. if s.Kind() != reflect.Slice {
  891. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to bool slice)", input)
  892. }
  893. var result []bool
  894. for i := 0; i < s.Len(); i++ {
  895. ele, err := ToBool(s.Index(i).Interface(), sn)
  896. if err != nil {
  897. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to bool slice for the %d element: %v", input, i, err)
  898. }
  899. result = append(result, ele)
  900. }
  901. return result, nil
  902. }
  903. func ToStringSlice(input interface{}, sn Strictness) ([]string, 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 []string
  909. for i := 0; i < s.Len(); i++ {
  910. ele, err := ToString(s.Index(i).Interface(), sn)
  911. if err != nil {
  912. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to string slice for the %d element: %v", input, i, err)
  913. }
  914. result = append(result, ele)
  915. }
  916. return result, nil
  917. }
  918. func ToBytesSlice(input interface{}, sn Strictness) ([][]byte, error) {
  919. s := reflect.ValueOf(input)
  920. if s.Kind() != reflect.Slice {
  921. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to string slice)", input)
  922. }
  923. var result [][]byte
  924. for i := 0; i < s.Len(); i++ {
  925. ele, err := ToBytes(s.Index(i).Interface(), sn)
  926. if err != nil {
  927. return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to bytes slice for the %d element: %v", input, i, err)
  928. }
  929. result = append(result, ele)
  930. }
  931. return result, nil
  932. }
  933. //MapToStruct
  934. /*
  935. * Convert a map into a struct. The output parameter must be a pointer to a struct
  936. * The struct can have the json meta data
  937. */
  938. func MapToStruct(input, output interface{}) error {
  939. config := &mapstructure.DecoderConfig{
  940. TagName: "json",
  941. Result: output,
  942. }
  943. decoder, err := mapstructure.NewDecoder(config)
  944. if err != nil {
  945. return err
  946. }
  947. return decoder.Decode(input)
  948. }
  949. // MapToStructStrict
  950. /*
  951. * Convert a map into a struct. The output parameter must be a pointer to a struct
  952. * If the input have key/value pair output do not defined, will report error
  953. */
  954. func MapToStructStrict(input, output interface{}) error {
  955. config := &mapstructure.DecoderConfig{
  956. ErrorUnused: true,
  957. TagName: "json",
  958. Result: output,
  959. }
  960. decoder, err := mapstructure.NewDecoder(config)
  961. if err != nil {
  962. return err
  963. }
  964. return decoder.Decode(input)
  965. }
  966. func ConvertMap(s map[interface{}]interface{}) map[string]interface{} {
  967. r := make(map[string]interface{})
  968. for k, v := range s {
  969. switch t := v.(type) {
  970. case map[interface{}]interface{}:
  971. v = ConvertMap(t)
  972. case []interface{}:
  973. v = ConvertArray(t)
  974. }
  975. r[fmt.Sprintf("%v", k)] = v
  976. }
  977. return r
  978. }
  979. func ConvertArray(s []interface{}) []interface{} {
  980. r := make([]interface{}, len(s))
  981. for i, e := range s {
  982. switch t := e.(type) {
  983. case map[interface{}]interface{}:
  984. e = ConvertMap(t)
  985. case []interface{}:
  986. e = ConvertArray(t)
  987. }
  988. r[i] = e
  989. }
  990. return r
  991. }
  992. func SyncMapToMap(sm *sync.Map) map[string]interface{} {
  993. m := make(map[string]interface{})
  994. sm.Range(func(k interface{}, v interface{}) bool {
  995. m[fmt.Sprintf("%v", k)] = v
  996. return true
  997. })
  998. return m
  999. }
  1000. func MapToSyncMap(m map[string]interface{}) *sync.Map {
  1001. sm := new(sync.Map)
  1002. for k, v := range m {
  1003. sm.Store(k, v)
  1004. }
  1005. return sm
  1006. }
  1007. func isIntegral64(val float64) bool {
  1008. return val == float64(int(val))
  1009. }
  1010. func isIntegral32(val float32) bool {
  1011. return val == float32(int(val))
  1012. }
  1013. func ConvertToInterfaceArr(orig map[string]interface{}) map[string]interface{} {
  1014. result := make(map[string]interface{})
  1015. for k, v := range orig {
  1016. vt := reflect.TypeOf(v)
  1017. if vt == nil {
  1018. result[k] = nil
  1019. continue
  1020. }
  1021. switch vt.Kind() {
  1022. case reflect.Slice:
  1023. result[k] = ConvertSlice(v)
  1024. case reflect.Map:
  1025. result[k] = ConvertToInterfaceArr(v.(map[string]interface{}))
  1026. default:
  1027. result[k] = v
  1028. }
  1029. }
  1030. return result
  1031. }
  1032. func ConvertSlice(v interface{}) []interface{} {
  1033. value := reflect.ValueOf(v)
  1034. tempArr := make([]interface{}, value.Len())
  1035. for i := 0; i < value.Len(); i++ {
  1036. item := value.Index(i)
  1037. if item.Kind() == reflect.Map {
  1038. tempArr[i] = ConvertToInterfaceArr(item.Interface().(map[string]interface{}))
  1039. } else if item.Kind() == reflect.Slice {
  1040. tempArr[i] = ConvertSlice(item.Interface())
  1041. } else {
  1042. tempArr[i] = item.Interface()
  1043. }
  1044. }
  1045. return tempArr
  1046. }