cast.go 22 KB

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