cast.go 26 KB

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