在Go中轻松安全地从一种数据类型转换为另一种数据类型
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1250 lines
27 KiB

  1. // Copyright © 2014 Steve Francia <spf@spf13.com>.
  2. //
  3. // Use of this source code is governed by an MIT-style
  4. // license that can be found in the LICENSE file.
  5. package cast
  6. import (
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "html/template"
  11. "reflect"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. var errNegativeNotAllowed = errors.New("unable to cast negative value")
  17. // ToTimeE casts an interface to a time.Time type.
  18. func ToTimeE(i interface{}) (tim time.Time, err error) {
  19. i = indirect(i)
  20. switch v := i.(type) {
  21. case time.Time:
  22. return v, nil
  23. case string:
  24. return StringToDate(v)
  25. case int:
  26. return time.Unix(int64(v), 0), nil
  27. case int64:
  28. return time.Unix(v, 0), nil
  29. case int32:
  30. return time.Unix(int64(v), 0), nil
  31. case uint:
  32. return time.Unix(int64(v), 0), nil
  33. case uint64:
  34. return time.Unix(int64(v), 0), nil
  35. case uint32:
  36. return time.Unix(int64(v), 0), nil
  37. default:
  38. return time.Time{}, fmt.Errorf("unable to cast %#v of type %T to Time", i, i)
  39. }
  40. }
  41. // ToDurationE casts an interface to a time.Duration type.
  42. func ToDurationE(i interface{}) (d time.Duration, err error) {
  43. i = indirect(i)
  44. switch s := i.(type) {
  45. case time.Duration:
  46. return s, nil
  47. case int, int64, int32, int16, int8, uint, uint64, uint32, uint16, uint8:
  48. d = time.Duration(ToInt64(s))
  49. return
  50. case float32, float64:
  51. d = time.Duration(ToFloat64(s))
  52. return
  53. case string:
  54. if strings.ContainsAny(s, "nsuµmh") {
  55. d, err = time.ParseDuration(s)
  56. } else {
  57. d, err = time.ParseDuration(s + "ns")
  58. }
  59. return
  60. default:
  61. err = fmt.Errorf("unable to cast %#v of type %T to Duration", i, i)
  62. return
  63. }
  64. }
  65. // ToBoolE casts an interface to a bool type.
  66. func ToBoolE(i interface{}) (bool, error) {
  67. i = indirect(i)
  68. switch b := i.(type) {
  69. case bool:
  70. return b, nil
  71. case nil:
  72. return false, nil
  73. case int:
  74. if i.(int) != 0 {
  75. return true, nil
  76. }
  77. return false, nil
  78. case string:
  79. return strconv.ParseBool(i.(string))
  80. default:
  81. return false, fmt.Errorf("unable to cast %#v of type %T to bool", i, i)
  82. }
  83. }
  84. // ToFloat64E casts an interface to a float64 type.
  85. func ToFloat64E(i interface{}) (float64, error) {
  86. i = indirect(i)
  87. switch s := i.(type) {
  88. case float64:
  89. return s, nil
  90. case float32:
  91. return float64(s), nil
  92. case int:
  93. return float64(s), nil
  94. case int64:
  95. return float64(s), nil
  96. case int32:
  97. return float64(s), nil
  98. case int16:
  99. return float64(s), nil
  100. case int8:
  101. return float64(s), nil
  102. case uint:
  103. return float64(s), nil
  104. case uint64:
  105. return float64(s), nil
  106. case uint32:
  107. return float64(s), nil
  108. case uint16:
  109. return float64(s), nil
  110. case uint8:
  111. return float64(s), nil
  112. case string:
  113. v, err := strconv.ParseFloat(s, 64)
  114. if err == nil {
  115. return v, nil
  116. }
  117. return 0, fmt.Errorf("unable to cast %#v of type %T to float64", i, i)
  118. case bool:
  119. if s {
  120. return 1, nil
  121. }
  122. return 0, nil
  123. default:
  124. return 0, fmt.Errorf("unable to cast %#v of type %T to float64", i, i)
  125. }
  126. }
  127. // ToFloat32E casts an interface to a float32 type.
  128. func ToFloat32E(i interface{}) (float32, error) {
  129. i = indirect(i)
  130. switch s := i.(type) {
  131. case float64:
  132. return float32(s), nil
  133. case float32:
  134. return s, nil
  135. case int:
  136. return float32(s), nil
  137. case int64:
  138. return float32(s), nil
  139. case int32:
  140. return float32(s), nil
  141. case int16:
  142. return float32(s), nil
  143. case int8:
  144. return float32(s), nil
  145. case uint:
  146. return float32(s), nil
  147. case uint64:
  148. return float32(s), nil
  149. case uint32:
  150. return float32(s), nil
  151. case uint16:
  152. return float32(s), nil
  153. case uint8:
  154. return float32(s), nil
  155. case string:
  156. v, err := strconv.ParseFloat(s, 32)
  157. if err == nil {
  158. return float32(v), nil
  159. }
  160. return 0, fmt.Errorf("unable to cast %#v of type %T to float32", i, i)
  161. case bool:
  162. if s {
  163. return 1, nil
  164. }
  165. return 0, nil
  166. default:
  167. return 0, fmt.Errorf("unable to cast %#v of type %T to float32", i, i)
  168. }
  169. }
  170. // ToInt64E casts an interface to an int64 type.
  171. func ToInt64E(i interface{}) (int64, error) {
  172. i = indirect(i)
  173. switch s := i.(type) {
  174. case int:
  175. return int64(s), nil
  176. case int64:
  177. return s, nil
  178. case int32:
  179. return int64(s), nil
  180. case int16:
  181. return int64(s), nil
  182. case int8:
  183. return int64(s), nil
  184. case uint:
  185. return int64(s), nil
  186. case uint64:
  187. return int64(s), nil
  188. case uint32:
  189. return int64(s), nil
  190. case uint16:
  191. return int64(s), nil
  192. case uint8:
  193. return int64(s), nil
  194. case float64:
  195. return int64(s), nil
  196. case float32:
  197. return int64(s), nil
  198. case string:
  199. v, err := strconv.ParseInt(s, 0, 0)
  200. if err == nil {
  201. return v, nil
  202. }
  203. return 0, fmt.Errorf("unable to cast %#v of type %T to int64", i, i)
  204. case bool:
  205. if s {
  206. return 1, nil
  207. }
  208. return 0, nil
  209. case nil:
  210. return 0, nil
  211. default:
  212. return 0, fmt.Errorf("unable to cast %#v of type %T to int64", i, i)
  213. }
  214. }
  215. // ToInt32E casts an interface to an int32 type.
  216. func ToInt32E(i interface{}) (int32, error) {
  217. i = indirect(i)
  218. switch s := i.(type) {
  219. case int:
  220. return int32(s), nil
  221. case int64:
  222. return int32(s), nil
  223. case int32:
  224. return s, nil
  225. case int16:
  226. return int32(s), nil
  227. case int8:
  228. return int32(s), nil
  229. case uint:
  230. return int32(s), nil
  231. case uint64:
  232. return int32(s), nil
  233. case uint32:
  234. return int32(s), nil
  235. case uint16:
  236. return int32(s), nil
  237. case uint8:
  238. return int32(s), nil
  239. case float64:
  240. return int32(s), nil
  241. case float32:
  242. return int32(s), nil
  243. case string:
  244. v, err := strconv.ParseInt(s, 0, 0)
  245. if err == nil {
  246. return int32(v), nil
  247. }
  248. return 0, fmt.Errorf("unable to cast %#v of type %T to int32", i, i)
  249. case bool:
  250. if s {
  251. return 1, nil
  252. }
  253. return 0, nil
  254. case nil:
  255. return 0, nil
  256. default:
  257. return 0, fmt.Errorf("unable to cast %#v of type %T to int32", i, i)
  258. }
  259. }
  260. // ToInt16E casts an interface to an int16 type.
  261. func ToInt16E(i interface{}) (int16, error) {
  262. i = indirect(i)
  263. switch s := i.(type) {
  264. case int:
  265. return int16(s), nil
  266. case int64:
  267. return int16(s), nil
  268. case int32:
  269. return int16(s), nil
  270. case int16:
  271. return s, nil
  272. case int8:
  273. return int16(s), nil
  274. case uint:
  275. return int16(s), nil
  276. case uint64:
  277. return int16(s), nil
  278. case uint32:
  279. return int16(s), nil
  280. case uint16:
  281. return int16(s), nil
  282. case uint8:
  283. return int16(s), nil
  284. case float64:
  285. return int16(s), nil
  286. case float32:
  287. return int16(s), nil
  288. case string:
  289. v, err := strconv.ParseInt(s, 0, 0)
  290. if err == nil {
  291. return int16(v), nil
  292. }
  293. return 0, fmt.Errorf("unable to cast %#v of type %T to int16", i, i)
  294. case bool:
  295. if s {
  296. return 1, nil
  297. }
  298. return 0, nil
  299. case nil:
  300. return 0, nil
  301. default:
  302. return 0, fmt.Errorf("unable to cast %#v of type %T to int16", i, i)
  303. }
  304. }
  305. // ToInt8E casts an interface to an int8 type.
  306. func ToInt8E(i interface{}) (int8, error) {
  307. i = indirect(i)
  308. switch s := i.(type) {
  309. case int:
  310. return int8(s), nil
  311. case int64:
  312. return int8(s), nil
  313. case int32:
  314. return int8(s), nil
  315. case int16:
  316. return int8(s), nil
  317. case int8:
  318. return s, nil
  319. case uint:
  320. return int8(s), nil
  321. case uint64:
  322. return int8(s), nil
  323. case uint32:
  324. return int8(s), nil
  325. case uint16:
  326. return int8(s), nil
  327. case uint8:
  328. return int8(s), nil
  329. case float64:
  330. return int8(s), nil
  331. case float32:
  332. return int8(s), nil
  333. case string:
  334. v, err := strconv.ParseInt(s, 0, 0)
  335. if err == nil {
  336. return int8(v), nil
  337. }
  338. return 0, fmt.Errorf("unable to cast %#v of type %T to int8", i, i)
  339. case bool:
  340. if s {
  341. return 1, nil
  342. }
  343. return 0, nil
  344. case nil:
  345. return 0, nil
  346. default:
  347. return 0, fmt.Errorf("unable to cast %#v of type %T to int8", i, i)
  348. }
  349. }
  350. // ToIntE casts an interface to an int type.
  351. func ToIntE(i interface{}) (int, error) {
  352. i = indirect(i)
  353. switch s := i.(type) {
  354. case int:
  355. return s, nil
  356. case int64:
  357. return int(s), nil
  358. case int32:
  359. return int(s), nil
  360. case int16:
  361. return int(s), nil
  362. case int8:
  363. return int(s), nil
  364. case uint:
  365. return int(s), nil
  366. case uint64:
  367. return int(s), nil
  368. case uint32:
  369. return int(s), nil
  370. case uint16:
  371. return int(s), nil
  372. case uint8:
  373. return int(s), nil
  374. case float64:
  375. return int(s), nil
  376. case float32:
  377. return int(s), nil
  378. case string:
  379. v, err := strconv.ParseInt(s, 0, 0)
  380. if err == nil {
  381. return int(v), nil
  382. }
  383. return 0, fmt.Errorf("unable to cast %#v of type %T to int", i, i)
  384. case bool:
  385. if s {
  386. return 1, nil
  387. }
  388. return 0, nil
  389. case nil:
  390. return 0, nil
  391. default:
  392. return 0, fmt.Errorf("unable to cast %#v of type %T to int", i, i)
  393. }
  394. }
  395. // ToUintE casts an interface to a uint type.
  396. func ToUintE(i interface{}) (uint, error) {
  397. i = indirect(i)
  398. switch s := i.(type) {
  399. case string:
  400. v, err := strconv.ParseUint(s, 0, 0)
  401. if err == nil {
  402. return uint(v), nil
  403. }
  404. return 0, fmt.Errorf("unable to cast %#v to uint: %s", i, err)
  405. case int:
  406. if s < 0 {
  407. return 0, errNegativeNotAllowed
  408. }
  409. return uint(s), nil
  410. case int64:
  411. if s < 0 {
  412. return 0, errNegativeNotAllowed
  413. }
  414. return uint(s), nil
  415. case int32:
  416. if s < 0 {
  417. return 0, errNegativeNotAllowed
  418. }
  419. return uint(s), nil
  420. case int16:
  421. if s < 0 {
  422. return 0, errNegativeNotAllowed
  423. }
  424. return uint(s), nil
  425. case int8:
  426. if s < 0 {
  427. return 0, errNegativeNotAllowed
  428. }
  429. return uint(s), nil
  430. case uint:
  431. return s, nil
  432. case uint64:
  433. return uint(s), nil
  434. case uint32:
  435. return uint(s), nil
  436. case uint16:
  437. return uint(s), nil
  438. case uint8:
  439. return uint(s), nil
  440. case float64:
  441. if s < 0 {
  442. return 0, errNegativeNotAllowed
  443. }
  444. return uint(s), nil
  445. case float32:
  446. if s < 0 {
  447. return 0, errNegativeNotAllowed
  448. }
  449. return uint(s), nil
  450. case bool:
  451. if s {
  452. return 1, nil
  453. }
  454. return 0, nil
  455. case nil:
  456. return 0, nil
  457. default:
  458. return 0, fmt.Errorf("unable to cast %#v of type %T to uint", i, i)
  459. }
  460. }
  461. // ToUint64E casts an interface to a uint64 type.
  462. func ToUint64E(i interface{}) (uint64, error) {
  463. i = indirect(i)
  464. switch s := i.(type) {
  465. case string:
  466. v, err := strconv.ParseUint(s, 0, 64)
  467. if err == nil {
  468. return v, nil
  469. }
  470. return 0, fmt.Errorf("unable to cast %#v to uint64: %s", i, err)
  471. case int:
  472. if s < 0 {
  473. return 0, errNegativeNotAllowed
  474. }
  475. return uint64(s), nil
  476. case int64:
  477. if s < 0 {
  478. return 0, errNegativeNotAllowed
  479. }
  480. return uint64(s), nil
  481. case int32:
  482. if s < 0 {
  483. return 0, errNegativeNotAllowed
  484. }
  485. return uint64(s), nil
  486. case int16:
  487. if s < 0 {
  488. return 0, errNegativeNotAllowed
  489. }
  490. return uint64(s), nil
  491. case int8:
  492. if s < 0 {
  493. return 0, errNegativeNotAllowed
  494. }
  495. return uint64(s), nil
  496. case uint:
  497. return uint64(s), nil
  498. case uint64:
  499. return s, nil
  500. case uint32:
  501. return uint64(s), nil
  502. case uint16:
  503. return uint64(s), nil
  504. case uint8:
  505. return uint64(s), nil
  506. case float32:
  507. if s < 0 {
  508. return 0, errNegativeNotAllowed
  509. }
  510. return uint64(s), nil
  511. case float64:
  512. if s < 0 {
  513. return 0, errNegativeNotAllowed
  514. }
  515. return uint64(s), nil
  516. case bool:
  517. if s {
  518. return 1, nil
  519. }
  520. return 0, nil
  521. case nil:
  522. return 0, nil
  523. default:
  524. return 0, fmt.Errorf("unable to cast %#v of type %T to uint64", i, i)
  525. }
  526. }
  527. // ToUint32E casts an interface to a uint32 type.
  528. func ToUint32E(i interface{}) (uint32, error) {
  529. i = indirect(i)
  530. switch s := i.(type) {
  531. case string:
  532. v, err := strconv.ParseUint(s, 0, 32)
  533. if err == nil {
  534. return uint32(v), nil
  535. }
  536. return 0, fmt.Errorf("unable to cast %#v to uint32: %s", i, err)
  537. case int:
  538. if s < 0 {
  539. return 0, errNegativeNotAllowed
  540. }
  541. return uint32(s), nil
  542. case int64:
  543. if s < 0 {
  544. return 0, errNegativeNotAllowed
  545. }
  546. return uint32(s), nil
  547. case int32:
  548. if s < 0 {
  549. return 0, errNegativeNotAllowed
  550. }
  551. return uint32(s), nil
  552. case int16:
  553. if s < 0 {
  554. return 0, errNegativeNotAllowed
  555. }
  556. return uint32(s), nil
  557. case int8:
  558. if s < 0 {
  559. return 0, errNegativeNotAllowed
  560. }
  561. return uint32(s), nil
  562. case uint:
  563. return uint32(s), nil
  564. case uint64:
  565. return uint32(s), nil
  566. case uint32:
  567. return s, nil
  568. case uint16:
  569. return uint32(s), nil
  570. case uint8:
  571. return uint32(s), nil
  572. case float64:
  573. if s < 0 {
  574. return 0, errNegativeNotAllowed
  575. }
  576. return uint32(s), nil
  577. case float32:
  578. if s < 0 {
  579. return 0, errNegativeNotAllowed
  580. }
  581. return uint32(s), nil
  582. case bool:
  583. if s {
  584. return 1, nil
  585. }
  586. return 0, nil
  587. case nil:
  588. return 0, nil
  589. default:
  590. return 0, fmt.Errorf("unable to cast %#v of type %T to uint32", i, i)
  591. }
  592. }
  593. // ToUint16E casts an interface to a uint16 type.
  594. func ToUint16E(i interface{}) (uint16, error) {
  595. i = indirect(i)
  596. switch s := i.(type) {
  597. case string:
  598. v, err := strconv.ParseUint(s, 0, 16)
  599. if err == nil {
  600. return uint16(v), nil
  601. }
  602. return 0, fmt.Errorf("unable to cast %#v to uint16: %s", i, err)
  603. case int:
  604. if s < 0 {
  605. return 0, errNegativeNotAllowed
  606. }
  607. return uint16(s), nil
  608. case int64:
  609. if s < 0 {
  610. return 0, errNegativeNotAllowed
  611. }
  612. return uint16(s), nil
  613. case int32:
  614. if s < 0 {
  615. return 0, errNegativeNotAllowed
  616. }
  617. return uint16(s), nil
  618. case int16:
  619. if s < 0 {
  620. return 0, errNegativeNotAllowed
  621. }
  622. return uint16(s), nil
  623. case int8:
  624. if s < 0 {
  625. return 0, errNegativeNotAllowed
  626. }
  627. return uint16(s), nil
  628. case uint:
  629. return uint16(s), nil
  630. case uint64:
  631. return uint16(s), nil
  632. case uint32:
  633. return uint16(s), nil
  634. case uint16:
  635. return s, nil
  636. case uint8:
  637. return uint16(s), nil
  638. case float64:
  639. if s < 0 {
  640. return 0, errNegativeNotAllowed
  641. }
  642. return uint16(s), nil
  643. case float32:
  644. if s < 0 {
  645. return 0, errNegativeNotAllowed
  646. }
  647. return uint16(s), nil
  648. case bool:
  649. if s {
  650. return 1, nil
  651. }
  652. return 0, nil
  653. case nil:
  654. return 0, nil
  655. default:
  656. return 0, fmt.Errorf("unable to cast %#v of type %T to uint16", i, i)
  657. }
  658. }
  659. // ToUint8E casts an interface to a uint type.
  660. func ToUint8E(i interface{}) (uint8, error) {
  661. i = indirect(i)
  662. switch s := i.(type) {
  663. case string:
  664. v, err := strconv.ParseUint(s, 0, 8)
  665. if err == nil {
  666. return uint8(v), nil
  667. }
  668. return 0, fmt.Errorf("unable to cast %#v to uint8: %s", i, err)
  669. case int:
  670. if s < 0 {
  671. return 0, errNegativeNotAllowed
  672. }
  673. return uint8(s), nil
  674. case int64:
  675. if s < 0 {
  676. return 0, errNegativeNotAllowed
  677. }
  678. return uint8(s), nil
  679. case int32:
  680. if s < 0 {
  681. return 0, errNegativeNotAllowed
  682. }
  683. return uint8(s), nil
  684. case int16:
  685. if s < 0 {
  686. return 0, errNegativeNotAllowed
  687. }
  688. return uint8(s), nil
  689. case int8:
  690. if s < 0 {
  691. return 0, errNegativeNotAllowed
  692. }
  693. return uint8(s), nil
  694. case uint:
  695. return uint8(s), nil
  696. case uint64:
  697. return uint8(s), nil
  698. case uint32:
  699. return uint8(s), nil
  700. case uint16:
  701. return uint8(s), nil
  702. case uint8:
  703. return s, nil
  704. case float64:
  705. if s < 0 {
  706. return 0, errNegativeNotAllowed
  707. }
  708. return uint8(s), nil
  709. case float32:
  710. if s < 0 {
  711. return 0, errNegativeNotAllowed
  712. }
  713. return uint8(s), nil
  714. case bool:
  715. if s {
  716. return 1, nil
  717. }
  718. return 0, nil
  719. case nil:
  720. return 0, nil
  721. default:
  722. return 0, fmt.Errorf("unable to cast %#v of type %T to uint8", i, i)
  723. }
  724. }
  725. // From html/template/content.go
  726. // Copyright 2011 The Go Authors. All rights reserved.
  727. // indirect returns the value, after dereferencing as many times
  728. // as necessary to reach the base type (or nil).
  729. func indirect(a interface{}) interface{} {
  730. if a == nil {
  731. return nil
  732. }
  733. if t := reflect.TypeOf(a); t.Kind() != reflect.Ptr {
  734. // Avoid creating a reflect.Value if it's not a pointer.
  735. return a
  736. }
  737. v := reflect.ValueOf(a)
  738. for v.Kind() == reflect.Ptr && !v.IsNil() {
  739. v = v.Elem()
  740. }
  741. return v.Interface()
  742. }
  743. // From html/template/content.go
  744. // Copyright 2011 The Go Authors. All rights reserved.
  745. // indirectToStringerOrError returns the value, after dereferencing as many times
  746. // as necessary to reach the base type (or nil) or an implementation of fmt.Stringer
  747. // or error,
  748. func indirectToStringerOrError(a interface{}) interface{} {
  749. if a == nil {
  750. return nil
  751. }
  752. var errorType = reflect.TypeOf((*error)(nil)).Elem()
  753. var fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
  754. v := reflect.ValueOf(a)
  755. for !v.Type().Implements(fmtStringerType) && !v.Type().Implements(errorType) && v.Kind() == reflect.Ptr && !v.IsNil() {
  756. v = v.Elem()
  757. }
  758. return v.Interface()
  759. }
  760. // ToStringE casts an interface to a string type.
  761. func ToStringE(i interface{}) (string, error) {
  762. i = indirectToStringerOrError(i)
  763. switch s := i.(type) {
  764. case string:
  765. return s, nil
  766. case bool:
  767. return strconv.FormatBool(s), nil
  768. case float64:
  769. return strconv.FormatFloat(s, 'f', -1, 64), nil
  770. case float32:
  771. return strconv.FormatFloat(float64(s), 'f', -1, 32), nil
  772. case int:
  773. return strconv.Itoa(s), nil
  774. case int64:
  775. return strconv.FormatInt(s, 10), nil
  776. case int32:
  777. return strconv.Itoa(int(s)), nil
  778. case int16:
  779. return strconv.FormatInt(int64(s), 10), nil
  780. case int8:
  781. return strconv.FormatInt(int64(s), 10), nil
  782. case uint:
  783. return strconv.FormatUint(uint64(s), 10), nil
  784. case uint64:
  785. return strconv.FormatUint(uint64(s), 10), nil
  786. case uint32:
  787. return strconv.FormatUint(uint64(s), 10), nil
  788. case uint16:
  789. return strconv.FormatUint(uint64(s), 10), nil
  790. case uint8:
  791. return strconv.FormatUint(uint64(s), 10), nil
  792. case []byte:
  793. return string(s), nil
  794. case template.HTML:
  795. return string(s), nil
  796. case template.URL:
  797. return string(s), nil
  798. case template.JS:
  799. return string(s), nil
  800. case template.CSS:
  801. return string(s), nil
  802. case template.HTMLAttr:
  803. return string(s), nil
  804. case nil:
  805. return "", nil
  806. case fmt.Stringer:
  807. return s.String(), nil
  808. case error:
  809. return s.Error(), nil
  810. default:
  811. return "", fmt.Errorf("unable to cast %#v of type %T to string", i, i)
  812. }
  813. }
  814. // ToStringMapStringE casts an interface to a map[string]string type.
  815. func ToStringMapStringE(i interface{}) (map[string]string, error) {
  816. var m = map[string]string{}
  817. switch v := i.(type) {
  818. case map[string]string:
  819. return v, nil
  820. case map[string]interface{}:
  821. for k, val := range v {
  822. m[ToString(k)] = ToString(val)
  823. }
  824. return m, nil
  825. case map[interface{}]string:
  826. for k, val := range v {
  827. m[ToString(k)] = ToString(val)
  828. }
  829. return m, nil
  830. case map[interface{}]interface{}:
  831. for k, val := range v {
  832. m[ToString(k)] = ToString(val)
  833. }
  834. return m, nil
  835. case string:
  836. err := jsonStringToObject(v, &m)
  837. return m, err
  838. default:
  839. return m, fmt.Errorf("unable to cast %#v of type %T to map[string]string", i, i)
  840. }
  841. }
  842. // ToStringMapStringSliceE casts an interface to a map[string][]string type.
  843. func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) {
  844. var m = map[string][]string{}
  845. switch v := i.(type) {
  846. case map[string][]string:
  847. return v, nil
  848. case map[string][]interface{}:
  849. for k, val := range v {
  850. m[ToString(k)] = ToStringSlice(val)
  851. }
  852. return m, nil
  853. case map[string]string:
  854. for k, val := range v {
  855. m[ToString(k)] = []string{val}
  856. }
  857. case map[string]interface{}:
  858. for k, val := range v {
  859. switch vt := val.(type) {
  860. case []interface{}:
  861. m[ToString(k)] = ToStringSlice(vt)
  862. case []string:
  863. m[ToString(k)] = vt
  864. default:
  865. m[ToString(k)] = []string{ToString(val)}
  866. }
  867. }
  868. return m, nil
  869. case map[interface{}][]string:
  870. for k, val := range v {
  871. m[ToString(k)] = ToStringSlice(val)
  872. }
  873. return m, nil
  874. case map[interface{}]string:
  875. for k, val := range v {
  876. m[ToString(k)] = ToStringSlice(val)
  877. }
  878. return m, nil
  879. case map[interface{}][]interface{}:
  880. for k, val := range v {
  881. m[ToString(k)] = ToStringSlice(val)
  882. }
  883. return m, nil
  884. case map[interface{}]interface{}:
  885. for k, val := range v {
  886. key, err := ToStringE(k)
  887. if err != nil {
  888. return m, fmt.Errorf("unable to cast %#v of type %T to map[string][]string", i, i)
  889. }
  890. value, err := ToStringSliceE(val)
  891. if err != nil {
  892. return m, fmt.Errorf("unable to cast %#v of type %T to map[string][]string", i, i)
  893. }
  894. m[key] = value
  895. }
  896. case string:
  897. err := jsonStringToObject(v, &m)
  898. return m, err
  899. default:
  900. return m, fmt.Errorf("unable to cast %#v of type %T to map[string][]string", i, i)
  901. }
  902. return m, nil
  903. }
  904. // ToStringMapBoolE casts an interface to a map[string]bool type.
  905. func ToStringMapBoolE(i interface{}) (map[string]bool, error) {
  906. var m = map[string]bool{}
  907. switch v := i.(type) {
  908. case map[interface{}]interface{}:
  909. for k, val := range v {
  910. m[ToString(k)] = ToBool(val)
  911. }
  912. return m, nil
  913. case map[string]interface{}:
  914. for k, val := range v {
  915. m[ToString(k)] = ToBool(val)
  916. }
  917. return m, nil
  918. case map[string]bool:
  919. return v, nil
  920. case string:
  921. err := jsonStringToObject(v, &m)
  922. return m, err
  923. default:
  924. return m, fmt.Errorf("unable to cast %#v of type %T to map[string]bool", i, i)
  925. }
  926. }
  927. // ToStringMapE casts an interface to a map[string]interface{} type.
  928. func ToStringMapE(i interface{}) (map[string]interface{}, error) {
  929. var m = map[string]interface{}{}
  930. switch v := i.(type) {
  931. case map[interface{}]interface{}:
  932. for k, val := range v {
  933. m[ToString(k)] = val
  934. }
  935. return m, nil
  936. case map[string]interface{}:
  937. return v, nil
  938. case string:
  939. err := jsonStringToObject(v, &m)
  940. return m, err
  941. default:
  942. return m, fmt.Errorf("unable to cast %#v of type %T to map[string]interface{}", i, i)
  943. }
  944. }
  945. // ToStringMapIntE casts an interface to a map[string]int{} type.
  946. func ToStringMapIntE(i interface{}) (map[string]int, error) {
  947. var m = map[string]int{}
  948. if i == nil {
  949. return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int", i, i)
  950. }
  951. switch v := i.(type) {
  952. case map[interface{}]interface{}:
  953. for k, val := range v {
  954. m[ToString(k)] = ToInt(val)
  955. }
  956. return m, nil
  957. case map[string]interface{}:
  958. for k, val := range v {
  959. m[k] = ToInt(val)
  960. }
  961. return m, nil
  962. case map[string]int:
  963. return v, nil
  964. case string:
  965. err := jsonStringToObject(v, &m)
  966. return m, err
  967. }
  968. if reflect.TypeOf(i).Kind() != reflect.Map {
  969. return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int", i, i)
  970. }
  971. mVal := reflect.ValueOf(m)
  972. v := reflect.ValueOf(i)
  973. for _, keyVal := range v.MapKeys() {
  974. val, err := ToIntE(v.MapIndex(keyVal).Interface())
  975. if err != nil {
  976. return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int", i, i)
  977. }
  978. mVal.SetMapIndex(keyVal, reflect.ValueOf(val))
  979. }
  980. return m, nil
  981. }
  982. // ToStringMapInt64E casts an interface to a map[string]int64{} type.
  983. func ToStringMapInt64E(i interface{}) (map[string]int64, error) {
  984. var m = map[string]int64{}
  985. if i == nil {
  986. return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int64", i, i)
  987. }
  988. switch v := i.(type) {
  989. case map[interface{}]interface{}:
  990. for k, val := range v {
  991. m[ToString(k)] = ToInt64(val)
  992. }
  993. return m, nil
  994. case map[string]interface{}:
  995. for k, val := range v {
  996. m[k] = ToInt64(val)
  997. }
  998. return m, nil
  999. case map[string]int64:
  1000. return v, nil
  1001. case string:
  1002. err := jsonStringToObject(v, &m)
  1003. return m, err
  1004. }
  1005. if reflect.TypeOf(i).Kind() != reflect.Map {
  1006. return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int64", i, i)
  1007. }
  1008. mVal := reflect.ValueOf(m)
  1009. v := reflect.ValueOf(i)
  1010. for _, keyVal := range v.MapKeys() {
  1011. val, err := ToInt64E(v.MapIndex(keyVal).Interface())
  1012. if err != nil {
  1013. return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int64", i, i)
  1014. }
  1015. mVal.SetMapIndex(keyVal, reflect.ValueOf(val))
  1016. }
  1017. return m, nil
  1018. }
  1019. // ToSliceE casts an interface to a []interface{} type.
  1020. func ToSliceE(i interface{}) ([]interface{}, error) {
  1021. var s []interface{}
  1022. switch v := i.(type) {
  1023. case []interface{}:
  1024. return append(s, v...), nil
  1025. case []map[string]interface{}:
  1026. for _, u := range v {
  1027. s = append(s, u)
  1028. }
  1029. return s, nil
  1030. default:
  1031. return s, fmt.Errorf("unable to cast %#v of type %T to []interface{}", i, i)
  1032. }
  1033. }
  1034. // ToBoolSliceE casts an interface to a []bool type.
  1035. func ToBoolSliceE(i interface{}) ([]bool, error) {
  1036. if i == nil {
  1037. return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i)
  1038. }
  1039. switch v := i.(type) {
  1040. case []bool:
  1041. return v, nil
  1042. }
  1043. kind := reflect.TypeOf(i).Kind()
  1044. switch kind {
  1045. case reflect.Slice, reflect.Array:
  1046. s := reflect.ValueOf(i)
  1047. a := make([]bool, s.Len())
  1048. for j := 0; j < s.Len(); j++ {
  1049. val, err := ToBoolE(s.Index(j).Interface())
  1050. if err != nil {
  1051. return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i)
  1052. }
  1053. a[j] = val
  1054. }
  1055. return a, nil
  1056. default:
  1057. return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i)
  1058. }
  1059. }
  1060. // ToStringSliceE casts an interface to a []string type.
  1061. func ToStringSliceE(i interface{}) ([]string, error) {
  1062. var a []string
  1063. switch v := i.(type) {
  1064. case []interface{}:
  1065. for _, u := range v {
  1066. a = append(a, ToString(u))
  1067. }
  1068. return a, nil
  1069. case []string:
  1070. return v, nil
  1071. case string:
  1072. return strings.Fields(v), nil
  1073. case interface{}:
  1074. str, err := ToStringE(v)
  1075. if err != nil {
  1076. return a, fmt.Errorf("unable to cast %#v of type %T to []string", i, i)
  1077. }
  1078. return []string{str}, nil
  1079. default:
  1080. return a, fmt.Errorf("unable to cast %#v of type %T to []string", i, i)
  1081. }
  1082. }
  1083. // ToIntSliceE casts an interface to a []int type.
  1084. func ToIntSliceE(i interface{}) ([]int, error) {
  1085. if i == nil {
  1086. return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i)
  1087. }
  1088. switch v := i.(type) {
  1089. case []int:
  1090. return v, nil
  1091. }
  1092. kind := reflect.TypeOf(i).Kind()
  1093. switch kind {
  1094. case reflect.Slice, reflect.Array:
  1095. s := reflect.ValueOf(i)
  1096. a := make([]int, s.Len())
  1097. for j := 0; j < s.Len(); j++ {
  1098. val, err := ToIntE(s.Index(j).Interface())
  1099. if err != nil {
  1100. return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i)
  1101. }
  1102. a[j] = val
  1103. }
  1104. return a, nil
  1105. default:
  1106. return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i)
  1107. }
  1108. }
  1109. // ToDurationSliceE casts an interface to a []time.Duration type.
  1110. func ToDurationSliceE(i interface{}) ([]time.Duration, error) {
  1111. if i == nil {
  1112. return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", i, i)
  1113. }
  1114. switch v := i.(type) {
  1115. case []time.Duration:
  1116. return v, nil
  1117. }
  1118. kind := reflect.TypeOf(i).Kind()
  1119. switch kind {
  1120. case reflect.Slice, reflect.Array:
  1121. s := reflect.ValueOf(i)
  1122. a := make([]time.Duration, s.Len())
  1123. for j := 0; j < s.Len(); j++ {
  1124. val, err := ToDurationE(s.Index(j).Interface())
  1125. if err != nil {
  1126. return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", i, i)
  1127. }
  1128. a[j] = val
  1129. }
  1130. return a, nil
  1131. default:
  1132. return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", i, i)
  1133. }
  1134. }
  1135. // StringToDate attempts to parse a string into a time.Time type using a
  1136. // predefined list of formats. If no suitable format is found, an error is
  1137. // returned.
  1138. func StringToDate(s string) (time.Time, error) {
  1139. return parseDateWith(s, []string{
  1140. time.RFC3339,
  1141. "2006-01-02T15:04:05", // iso8601 without timezone
  1142. time.RFC1123Z,
  1143. time.RFC1123,
  1144. time.RFC822Z,
  1145. time.RFC822,
  1146. time.RFC850,
  1147. time.ANSIC,
  1148. time.UnixDate,
  1149. time.RubyDate,
  1150. "2006-01-02 15:04:05.999999999 -0700 MST", // Time.String()
  1151. "2006-01-02",
  1152. "02 Jan 2006",
  1153. "2006-01-02T15:04:05-0700", // RFC3339 without timezone hh:mm colon
  1154. "2006-01-02 15:04:05 -07:00",
  1155. "2006-01-02 15:04:05 -0700",
  1156. "2006-01-02 15:04:05Z07:00", // RFC3339 without T
  1157. "2006-01-02 15:04:05Z0700", // RFC3339 without T or timezone hh:mm colon
  1158. "2006-01-02 15:04:05",
  1159. time.Kitchen,
  1160. time.Stamp,
  1161. time.StampMilli,
  1162. time.StampMicro,
  1163. time.StampNano,
  1164. })
  1165. }
  1166. func parseDateWith(s string, dates []string) (d time.Time, e error) {
  1167. for _, dateType := range dates {
  1168. if d, e = time.Parse(dateType, s); e == nil {
  1169. return
  1170. }
  1171. }
  1172. return d, fmt.Errorf("unable to parse date: %s", s)
  1173. }
  1174. // jsonStringToObject attempts to unmarshall a string as JSON into
  1175. // the object passed as pointer.
  1176. func jsonStringToObject(s string, v interface{}) error {
  1177. data := []byte(s)
  1178. return json.Unmarshal(data, v)
  1179. }