cast.go 22 KB

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