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

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