cast.go 24 KB

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