cast.go 22 KB

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