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

1115 lines
24 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. if s < 0 {
  404. return 0, fmt.Errorf("unable to cast negative value to uint")
  405. }
  406. return uint(s), nil
  407. case int64:
  408. if s < 0 {
  409. return 0, fmt.Errorf("unable to cast negative value to uint")
  410. }
  411. return uint(s), nil
  412. case int32:
  413. if s < 0 {
  414. return 0, fmt.Errorf("unable to cast negative value to uint")
  415. }
  416. return uint(s), nil
  417. case int16:
  418. if s < 0 {
  419. return 0, fmt.Errorf("unable to cast negative value to uint")
  420. }
  421. return uint(s), nil
  422. case int8:
  423. if s < 0 {
  424. return 0, fmt.Errorf("unable to cast negative value to uint")
  425. }
  426. return uint(s), nil
  427. case uint:
  428. return s, nil
  429. case uint64:
  430. return uint(s), nil
  431. case uint32:
  432. return uint(s), nil
  433. case uint16:
  434. return uint(s), nil
  435. case uint8:
  436. return uint(s), nil
  437. case float64:
  438. if s < 0 {
  439. return 0, fmt.Errorf("unable to cast negative value to uint")
  440. }
  441. return uint(s), nil
  442. case float32:
  443. if s < 0 {
  444. return 0, fmt.Errorf("unable to cast negative value to uint")
  445. }
  446. return uint(s), nil
  447. case bool:
  448. if s {
  449. return 1, nil
  450. }
  451. return 0, nil
  452. case nil:
  453. return 0, nil
  454. default:
  455. return 0, fmt.Errorf("unable to cast %#v of type %T to uint", i, i)
  456. }
  457. }
  458. // ToUint64E casts an interface to a uint64 type.
  459. func ToUint64E(i interface{}) (uint64, error) {
  460. i = indirect(i)
  461. switch s := i.(type) {
  462. case string:
  463. v, err := strconv.ParseUint(s, 0, 64)
  464. if err == nil {
  465. return v, nil
  466. }
  467. return 0, fmt.Errorf("unable to cast %#v to uint64: %s", i, err)
  468. case int:
  469. if s < 0 {
  470. return 0, fmt.Errorf("unable to cast negative value to uint64")
  471. }
  472. return uint64(s), nil
  473. case int64:
  474. if s < 0 {
  475. return 0, fmt.Errorf("unable to cast negative value to uint64")
  476. }
  477. return uint64(s), nil
  478. case int32:
  479. if s < 0 {
  480. return 0, fmt.Errorf("unable to cast negative value to uint64")
  481. }
  482. return uint64(s), nil
  483. case int16:
  484. if s < 0 {
  485. return 0, fmt.Errorf("unable to cast negative value to uint64")
  486. }
  487. return uint64(s), nil
  488. case int8:
  489. if s < 0 {
  490. return 0, fmt.Errorf("unable to cast negative value to uint64")
  491. }
  492. return uint64(s), nil
  493. case uint:
  494. return uint64(s), nil
  495. case uint64:
  496. return s, nil
  497. case uint32:
  498. return uint64(s), nil
  499. case uint16:
  500. return uint64(s), nil
  501. case uint8:
  502. return uint64(s), nil
  503. case float32:
  504. if s < 0 {
  505. return 0, fmt.Errorf("unable to cast negative value to uint64")
  506. }
  507. return uint64(s), nil
  508. case float64:
  509. if s < 0 {
  510. return 0, fmt.Errorf("unable to cast negative value to uint64")
  511. }
  512. return uint64(s), nil
  513. case bool:
  514. if s {
  515. return 1, nil
  516. }
  517. return 0, nil
  518. case nil:
  519. return 0, nil
  520. default:
  521. return 0, fmt.Errorf("unable to cast %#v of type %T to uint64", i, i)
  522. }
  523. }
  524. // ToUint32E casts an interface to a uint32 type.
  525. func ToUint32E(i interface{}) (uint32, error) {
  526. i = indirect(i)
  527. switch s := i.(type) {
  528. case string:
  529. v, err := strconv.ParseUint(s, 0, 32)
  530. if err == nil {
  531. return uint32(v), nil
  532. }
  533. return 0, fmt.Errorf("unable to cast %#v to uint32: %s", i, err)
  534. case int:
  535. if s < 0 {
  536. return 0, fmt.Errorf("unable to cast negative value to uint32")
  537. }
  538. return uint32(s), nil
  539. case int64:
  540. if s < 0 {
  541. return 0, fmt.Errorf("unable to cast negative value to uint32")
  542. }
  543. return uint32(s), nil
  544. case int32:
  545. if s < 0 {
  546. return 0, fmt.Errorf("unable to cast negative value to uint32")
  547. }
  548. return uint32(s), nil
  549. case int16:
  550. if s < 0 {
  551. return 0, fmt.Errorf("unable to cast negative value to uint32")
  552. }
  553. return uint32(s), nil
  554. case int8:
  555. if s < 0 {
  556. return 0, fmt.Errorf("unable to cast negative value to uint32")
  557. }
  558. return uint32(s), nil
  559. case uint:
  560. return uint32(s), nil
  561. case uint64:
  562. return uint32(s), nil
  563. case uint32:
  564. return s, nil
  565. case uint16:
  566. return uint32(s), nil
  567. case uint8:
  568. return uint32(s), nil
  569. case float64:
  570. if s < 0 {
  571. return 0, fmt.Errorf("unable to cast negative value to uint32")
  572. }
  573. return uint32(s), nil
  574. case float32:
  575. if s < 0 {
  576. return 0, fmt.Errorf("unable to cast negative value to uint32")
  577. }
  578. return uint32(s), nil
  579. case bool:
  580. if s {
  581. return 1, nil
  582. }
  583. return 0, nil
  584. case nil:
  585. return 0, nil
  586. default:
  587. return 0, fmt.Errorf("unable to cast %#v of type %T to uint32", i, i)
  588. }
  589. }
  590. // ToUint16E casts an interface to a uint16 type.
  591. func ToUint16E(i interface{}) (uint16, error) {
  592. i = indirect(i)
  593. switch s := i.(type) {
  594. case string:
  595. v, err := strconv.ParseUint(s, 0, 16)
  596. if err == nil {
  597. return uint16(v), nil
  598. }
  599. return 0, fmt.Errorf("unable to cast %#v to uint16: %s", i, err)
  600. case int:
  601. if s < 0 {
  602. return 0, fmt.Errorf("unable to cast negative value to uint16")
  603. }
  604. return uint16(s), nil
  605. case int64:
  606. if s < 0 {
  607. return 0, fmt.Errorf("unable to cast negative value to uint16")
  608. }
  609. return uint16(s), nil
  610. case int32:
  611. if s < 0 {
  612. return 0, fmt.Errorf("unable to cast negative value to uint16")
  613. }
  614. return uint16(s), nil
  615. case int16:
  616. if s < 0 {
  617. return 0, fmt.Errorf("unable to cast negative value to uint16")
  618. }
  619. return uint16(s), nil
  620. case int8:
  621. if s < 0 {
  622. return 0, fmt.Errorf("unable to cast negative value to uint16")
  623. }
  624. return uint16(s), nil
  625. case uint:
  626. return uint16(s), nil
  627. case uint64:
  628. return uint16(s), nil
  629. case uint32:
  630. return uint16(s), nil
  631. case uint16:
  632. return s, nil
  633. case uint8:
  634. return uint16(s), nil
  635. case float64:
  636. if s < 0 {
  637. return 0, fmt.Errorf("unable to cast negative value to uint16")
  638. }
  639. return uint16(s), nil
  640. case float32:
  641. if s < 0 {
  642. return 0, fmt.Errorf("unable to cast negative value to uint16")
  643. }
  644. return uint16(s), nil
  645. case bool:
  646. if s {
  647. return 1, nil
  648. }
  649. return 0, nil
  650. case nil:
  651. return 0, nil
  652. default:
  653. return 0, fmt.Errorf("unable to cast %#v of type %T to uint16", i, i)
  654. }
  655. }
  656. // ToUint8E casts an interface to a uint type.
  657. func ToUint8E(i interface{}) (uint8, error) {
  658. i = indirect(i)
  659. switch s := i.(type) {
  660. case string:
  661. v, err := strconv.ParseUint(s, 0, 8)
  662. if err == nil {
  663. return uint8(v), nil
  664. }
  665. return 0, fmt.Errorf("unable to cast %#v to uint8: %s", i, err)
  666. case int:
  667. if s < 0 {
  668. return 0, fmt.Errorf("unable to cast negative value to uint8")
  669. }
  670. return uint8(s), nil
  671. case int64:
  672. if s < 0 {
  673. return 0, fmt.Errorf("unable to cast negative value to uint8")
  674. }
  675. return uint8(s), nil
  676. case int32:
  677. if s < 0 {
  678. return 0, fmt.Errorf("unable to cast negative value to uint8")
  679. }
  680. return uint8(s), nil
  681. case int16:
  682. if s < 0 {
  683. return 0, fmt.Errorf("unable to cast negative value to uint8")
  684. }
  685. return uint8(s), nil
  686. case int8:
  687. if s < 0 {
  688. return 0, fmt.Errorf("unable to cast negative value to uint8")
  689. }
  690. return uint8(s), nil
  691. case uint:
  692. return uint8(s), nil
  693. case uint64:
  694. return uint8(s), nil
  695. case uint32:
  696. return uint8(s), nil
  697. case uint16:
  698. return uint8(s), nil
  699. case uint8:
  700. return s, nil
  701. case float64:
  702. if s < 0 {
  703. return 0, fmt.Errorf("unable to cast negative value to uint8")
  704. }
  705. return uint8(s), nil
  706. case float32:
  707. if s < 0 {
  708. return 0, fmt.Errorf("unable to cast negative value to uint8")
  709. }
  710. return uint8(s), nil
  711. case bool:
  712. if s {
  713. return 1, nil
  714. }
  715. return 0, nil
  716. case nil:
  717. return 0, nil
  718. default:
  719. return 0, fmt.Errorf("unable to cast %#v of type %T to uint8", i, i)
  720. }
  721. }
  722. // From html/template/content.go
  723. // Copyright 2011 The Go Authors. All rights reserved.
  724. // indirect returns the value, after dereferencing as many times
  725. // as necessary to reach the base type (or nil).
  726. func indirect(a interface{}) interface{} {
  727. if a == nil {
  728. return nil
  729. }
  730. if t := reflect.TypeOf(a); t.Kind() != reflect.Ptr {
  731. // Avoid creating a reflect.Value if it's not a pointer.
  732. return a
  733. }
  734. v := reflect.ValueOf(a)
  735. for v.Kind() == reflect.Ptr && !v.IsNil() {
  736. v = v.Elem()
  737. }
  738. return v.Interface()
  739. }
  740. // From html/template/content.go
  741. // Copyright 2011 The Go Authors. All rights reserved.
  742. // indirectToStringerOrError returns the value, after dereferencing as many times
  743. // as necessary to reach the base type (or nil) or an implementation of fmt.Stringer
  744. // or error,
  745. func indirectToStringerOrError(a interface{}) interface{} {
  746. if a == nil {
  747. return nil
  748. }
  749. var errorType = reflect.TypeOf((*error)(nil)).Elem()
  750. var fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
  751. v := reflect.ValueOf(a)
  752. for !v.Type().Implements(fmtStringerType) && !v.Type().Implements(errorType) && v.Kind() == reflect.Ptr && !v.IsNil() {
  753. v = v.Elem()
  754. }
  755. return v.Interface()
  756. }
  757. // ToStringE casts an interface to a string type.
  758. func ToStringE(i interface{}) (string, error) {
  759. i = indirectToStringerOrError(i)
  760. switch s := i.(type) {
  761. case string:
  762. return s, nil
  763. case bool:
  764. return strconv.FormatBool(s), nil
  765. case float64:
  766. return strconv.FormatFloat(s, 'f', -1, 64), nil
  767. case float32:
  768. return strconv.FormatFloat(float64(s), 'f', -1, 32), nil
  769. case int:
  770. return strconv.Itoa(s), nil
  771. case int64:
  772. return strconv.FormatInt(s, 10), nil
  773. case int32:
  774. return strconv.Itoa(int(s)), nil
  775. case int16:
  776. return strconv.FormatInt(int64(s), 10), nil
  777. case int8:
  778. return strconv.FormatInt(int64(s), 10), nil
  779. case uint:
  780. return strconv.FormatInt(int64(s), 10), nil
  781. case uint64:
  782. return strconv.FormatInt(int64(s), 10), nil
  783. case uint32:
  784. return strconv.FormatInt(int64(s), 10), nil
  785. case uint16:
  786. return strconv.FormatInt(int64(s), 10), nil
  787. case uint8:
  788. return strconv.FormatInt(int64(s), 10), nil
  789. case []byte:
  790. return string(s), nil
  791. case template.HTML:
  792. return string(s), nil
  793. case template.URL:
  794. return string(s), nil
  795. case template.JS:
  796. return string(s), nil
  797. case template.CSS:
  798. return string(s), nil
  799. case template.HTMLAttr:
  800. return string(s), nil
  801. case nil:
  802. return "", nil
  803. case fmt.Stringer:
  804. return s.String(), nil
  805. case error:
  806. return s.Error(), nil
  807. default:
  808. return "", fmt.Errorf("unable to cast %#v of type %T to string", i, i)
  809. }
  810. }
  811. // ToStringMapStringE casts an interface to a map[string]string type.
  812. func ToStringMapStringE(i interface{}) (map[string]string, error) {
  813. var m = map[string]string{}
  814. switch v := i.(type) {
  815. case map[string]string:
  816. return v, nil
  817. case map[string]interface{}:
  818. for k, val := range v {
  819. m[ToString(k)] = ToString(val)
  820. }
  821. return m, nil
  822. case map[interface{}]string:
  823. for k, val := range v {
  824. m[ToString(k)] = ToString(val)
  825. }
  826. return m, nil
  827. case map[interface{}]interface{}:
  828. for k, val := range v {
  829. m[ToString(k)] = ToString(val)
  830. }
  831. return m, nil
  832. default:
  833. return m, fmt.Errorf("unable to cast %#v of type %T to map[string]string", i, i)
  834. }
  835. }
  836. // ToStringMapStringSliceE casts an interface to a map[string][]string type.
  837. func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) {
  838. var m = map[string][]string{}
  839. switch v := i.(type) {
  840. case map[string][]string:
  841. return v, nil
  842. case map[string][]interface{}:
  843. for k, val := range v {
  844. m[ToString(k)] = ToStringSlice(val)
  845. }
  846. return m, nil
  847. case map[string]string:
  848. for k, val := range v {
  849. m[ToString(k)] = []string{val}
  850. }
  851. case map[string]interface{}:
  852. for k, val := range v {
  853. switch vt := val.(type) {
  854. case []interface{}:
  855. m[ToString(k)] = ToStringSlice(vt)
  856. case []string:
  857. m[ToString(k)] = vt
  858. default:
  859. m[ToString(k)] = []string{ToString(val)}
  860. }
  861. }
  862. return m, nil
  863. case map[interface{}][]string:
  864. for k, val := range v {
  865. m[ToString(k)] = ToStringSlice(val)
  866. }
  867. return m, nil
  868. case map[interface{}]string:
  869. for k, val := range v {
  870. m[ToString(k)] = ToStringSlice(val)
  871. }
  872. return m, nil
  873. case map[interface{}][]interface{}:
  874. for k, val := range v {
  875. m[ToString(k)] = ToStringSlice(val)
  876. }
  877. return m, nil
  878. case map[interface{}]interface{}:
  879. for k, val := range v {
  880. key, err := ToStringE(k)
  881. if err != nil {
  882. return m, fmt.Errorf("unable to cast %#v of type %T to map[string][]string", i, i)
  883. }
  884. value, err := ToStringSliceE(val)
  885. if err != nil {
  886. return m, fmt.Errorf("unable to cast %#v of type %T to map[string][]string", i, i)
  887. }
  888. m[key] = value
  889. }
  890. default:
  891. return m, fmt.Errorf("unable to cast %#v of type %T to map[string][]string", i, i)
  892. }
  893. return m, nil
  894. }
  895. // ToStringMapBoolE casts an interface to a map[string]bool type.
  896. func ToStringMapBoolE(i interface{}) (map[string]bool, error) {
  897. var m = map[string]bool{}
  898. switch v := i.(type) {
  899. case map[interface{}]interface{}:
  900. for k, val := range v {
  901. m[ToString(k)] = ToBool(val)
  902. }
  903. return m, nil
  904. case map[string]interface{}:
  905. for k, val := range v {
  906. m[ToString(k)] = ToBool(val)
  907. }
  908. return m, nil
  909. case map[string]bool:
  910. return v, nil
  911. default:
  912. return m, fmt.Errorf("unable to cast %#v of type %T to map[string]bool", i, i)
  913. }
  914. }
  915. // ToStringMapE casts an interface to a map[string]interface{} type.
  916. func ToStringMapE(i interface{}) (map[string]interface{}, error) {
  917. var m = map[string]interface{}{}
  918. switch v := i.(type) {
  919. case map[interface{}]interface{}:
  920. for k, val := range v {
  921. m[ToString(k)] = val
  922. }
  923. return m, nil
  924. case map[string]interface{}:
  925. return v, nil
  926. default:
  927. return m, fmt.Errorf("unable to cast %#v of type %T to map[string]interface{}", i, i)
  928. }
  929. }
  930. // ToSliceE casts an interface to a []interface{} type.
  931. func ToSliceE(i interface{}) ([]interface{}, error) {
  932. var s []interface{}
  933. switch v := i.(type) {
  934. case []interface{}:
  935. return append(s, v...), nil
  936. case []map[string]interface{}:
  937. for _, u := range v {
  938. s = append(s, u)
  939. }
  940. return s, nil
  941. default:
  942. return s, fmt.Errorf("unable to cast %#v of type %T to []interface{}", i, i)
  943. }
  944. }
  945. // ToBoolSliceE casts an interface to a []bool type.
  946. func ToBoolSliceE(i interface{}) ([]bool, error) {
  947. if i == nil {
  948. return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i)
  949. }
  950. switch v := i.(type) {
  951. case []bool:
  952. return v, nil
  953. }
  954. kind := reflect.TypeOf(i).Kind()
  955. switch kind {
  956. case reflect.Slice, reflect.Array:
  957. s := reflect.ValueOf(i)
  958. a := make([]bool, s.Len())
  959. for j := 0; j < s.Len(); j++ {
  960. val, err := ToBoolE(s.Index(j).Interface())
  961. if err != nil {
  962. return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i)
  963. }
  964. a[j] = val
  965. }
  966. return a, nil
  967. default:
  968. return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i)
  969. }
  970. }
  971. // ToStringSliceE casts an interface to a []string type.
  972. func ToStringSliceE(i interface{}) ([]string, error) {
  973. var a []string
  974. switch v := i.(type) {
  975. case []interface{}:
  976. for _, u := range v {
  977. a = append(a, ToString(u))
  978. }
  979. return a, nil
  980. case []string:
  981. return v, nil
  982. case string:
  983. return strings.Fields(v), nil
  984. case interface{}:
  985. str, err := ToStringE(v)
  986. if err != nil {
  987. return a, fmt.Errorf("unable to cast %#v of type %T to []string", i, i)
  988. }
  989. return []string{str}, nil
  990. default:
  991. return a, fmt.Errorf("unable to cast %#v of type %T to []string", i, i)
  992. }
  993. }
  994. // ToIntSliceE casts an interface to a []int type.
  995. func ToIntSliceE(i interface{}) ([]int, error) {
  996. if i == nil {
  997. return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i)
  998. }
  999. switch v := i.(type) {
  1000. case []int:
  1001. return v, nil
  1002. }
  1003. kind := reflect.TypeOf(i).Kind()
  1004. switch kind {
  1005. case reflect.Slice, reflect.Array:
  1006. s := reflect.ValueOf(i)
  1007. a := make([]int, s.Len())
  1008. for j := 0; j < s.Len(); j++ {
  1009. val, err := ToIntE(s.Index(j).Interface())
  1010. if err != nil {
  1011. return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i)
  1012. }
  1013. a[j] = val
  1014. }
  1015. return a, nil
  1016. default:
  1017. return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i)
  1018. }
  1019. }
  1020. // StringToDate attempts to parse a string into a time.Time type using a
  1021. // predefined list of formats. If no suitable format is found, an error is
  1022. // returned.
  1023. func StringToDate(s string) (time.Time, error) {
  1024. return parseDateWith(s, []string{
  1025. time.RFC3339,
  1026. "2006-01-02T15:04:05", // iso8601 without timezone
  1027. time.RFC1123Z,
  1028. time.RFC1123,
  1029. time.RFC822Z,
  1030. time.RFC822,
  1031. time.RFC850,
  1032. time.ANSIC,
  1033. time.UnixDate,
  1034. time.RubyDate,
  1035. "2006-01-02 15:04:05.999999999 -0700 MST", // Time.String()
  1036. "2006-01-02",
  1037. "02 Jan 2006",
  1038. "2006-01-02 15:04:05 -07:00",
  1039. "2006-01-02 15:04:05 -0700",
  1040. "2006-01-02 15:04:05Z07:00", // RFC3339 without T
  1041. "2006-01-02 15:04:05",
  1042. time.Kitchen,
  1043. time.Stamp,
  1044. time.StampMilli,
  1045. time.StampMicro,
  1046. time.StampNano,
  1047. })
  1048. }
  1049. func parseDateWith(s string, dates []string) (d time.Time, e error) {
  1050. for _, dateType := range dates {
  1051. if d, e = time.Parse(dateType, s); e == nil {
  1052. return
  1053. }
  1054. }
  1055. return d, fmt.Errorf("unable to parse date: %s", s)
  1056. }