cast.go 23 KB

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