cast.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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. "reflect"
  19. "strconv"
  20. "sync"
  21. "github.com/mitchellh/mapstructure"
  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 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 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 bytea", input)
  807. }
  808. func ToStringMap(input interface{}) (map[string]interface{}, error) {
  809. 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, bytea
  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. case "bytea":
  1110. r, e := ToByteA(value, CONVERT_ALL)
  1111. if e != nil {
  1112. return e, false
  1113. } else {
  1114. return r, true
  1115. }
  1116. default:
  1117. return fmt.Errorf("unknow type, only support bigint, float, string, boolean and datetime"), false
  1118. }
  1119. } else {
  1120. return fmt.Errorf("expect string type for type parameter"), false
  1121. }
  1122. }