在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.
 
 
 

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