http urls monitor.

caste.go 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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.FormatInt(int64(s), 10), nil
  784. case uint64:
  785. return strconv.FormatInt(int64(s), 10), nil
  786. case uint32:
  787. return strconv.FormatInt(int64(s), 10), nil
  788. case uint16:
  789. return strconv.FormatInt(int64(s), 10), nil
  790. case uint8:
  791. return strconv.FormatInt(int64(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. // ToSliceE casts an interface to a []interface{} type.
  946. func ToSliceE(i interface{}) ([]interface{}, error) {
  947. var s []interface{}
  948. switch v := i.(type) {
  949. case []interface{}:
  950. return append(s, v...), nil
  951. case []map[string]interface{}:
  952. for _, u := range v {
  953. s = append(s, u)
  954. }
  955. return s, nil
  956. default:
  957. return s, fmt.Errorf("unable to cast %#v of type %T to []interface{}", i, i)
  958. }
  959. }
  960. // ToBoolSliceE casts an interface to a []bool type.
  961. func ToBoolSliceE(i interface{}) ([]bool, error) {
  962. if i == nil {
  963. return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i)
  964. }
  965. switch v := i.(type) {
  966. case []bool:
  967. return v, nil
  968. }
  969. kind := reflect.TypeOf(i).Kind()
  970. switch kind {
  971. case reflect.Slice, reflect.Array:
  972. s := reflect.ValueOf(i)
  973. a := make([]bool, s.Len())
  974. for j := 0; j < s.Len(); j++ {
  975. val, err := ToBoolE(s.Index(j).Interface())
  976. if err != nil {
  977. return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i)
  978. }
  979. a[j] = val
  980. }
  981. return a, nil
  982. default:
  983. return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i)
  984. }
  985. }
  986. // ToStringSliceE casts an interface to a []string type.
  987. func ToStringSliceE(i interface{}) ([]string, error) {
  988. var a []string
  989. switch v := i.(type) {
  990. case []interface{}:
  991. for _, u := range v {
  992. a = append(a, ToString(u))
  993. }
  994. return a, nil
  995. case []string:
  996. return v, nil
  997. case string:
  998. return strings.Fields(v), nil
  999. case interface{}:
  1000. str, err := ToStringE(v)
  1001. if err != nil {
  1002. return a, fmt.Errorf("unable to cast %#v of type %T to []string", i, i)
  1003. }
  1004. return []string{str}, nil
  1005. default:
  1006. return a, fmt.Errorf("unable to cast %#v of type %T to []string", i, i)
  1007. }
  1008. }
  1009. // ToIntSliceE casts an interface to a []int type.
  1010. func ToIntSliceE(i interface{}) ([]int, error) {
  1011. if i == nil {
  1012. return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i)
  1013. }
  1014. switch v := i.(type) {
  1015. case []int:
  1016. return v, nil
  1017. }
  1018. kind := reflect.TypeOf(i).Kind()
  1019. switch kind {
  1020. case reflect.Slice, reflect.Array:
  1021. s := reflect.ValueOf(i)
  1022. a := make([]int, s.Len())
  1023. for j := 0; j < s.Len(); j++ {
  1024. val, err := ToIntE(s.Index(j).Interface())
  1025. if err != nil {
  1026. return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i)
  1027. }
  1028. a[j] = val
  1029. }
  1030. return a, nil
  1031. default:
  1032. return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i)
  1033. }
  1034. }
  1035. // ToDurationSliceE casts an interface to a []time.Duration type.
  1036. func ToDurationSliceE(i interface{}) ([]time.Duration, error) {
  1037. if i == nil {
  1038. return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", i, i)
  1039. }
  1040. switch v := i.(type) {
  1041. case []time.Duration:
  1042. return v, nil
  1043. }
  1044. kind := reflect.TypeOf(i).Kind()
  1045. switch kind {
  1046. case reflect.Slice, reflect.Array:
  1047. s := reflect.ValueOf(i)
  1048. a := make([]time.Duration, s.Len())
  1049. for j := 0; j < s.Len(); j++ {
  1050. val, err := ToDurationE(s.Index(j).Interface())
  1051. if err != nil {
  1052. return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", i, i)
  1053. }
  1054. a[j] = val
  1055. }
  1056. return a, nil
  1057. default:
  1058. return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", i, i)
  1059. }
  1060. }
  1061. // StringToDate attempts to parse a string into a time.Time type using a
  1062. // predefined list of formats. If no suitable format is found, an error is
  1063. // returned.
  1064. func StringToDate(s string) (time.Time, error) {
  1065. return parseDateWith(s, []string{
  1066. time.RFC3339,
  1067. "2006-01-02T15:04:05", // iso8601 without timezone
  1068. time.RFC1123Z,
  1069. time.RFC1123,
  1070. time.RFC822Z,
  1071. time.RFC822,
  1072. time.RFC850,
  1073. time.ANSIC,
  1074. time.UnixDate,
  1075. time.RubyDate,
  1076. "2006-01-02 15:04:05.999999999 -0700 MST", // Time.String()
  1077. "2006-01-02",
  1078. "02 Jan 2006",
  1079. "2006-01-02 15:04:05 -07:00",
  1080. "2006-01-02 15:04:05 -0700",
  1081. "2006-01-02 15:04:05Z07:00", // RFC3339 without T
  1082. "2006-01-02 15:04:05",
  1083. time.Kitchen,
  1084. time.Stamp,
  1085. time.StampMilli,
  1086. time.StampMicro,
  1087. time.StampNano,
  1088. })
  1089. }
  1090. func parseDateWith(s string, dates []string) (d time.Time, e error) {
  1091. for _, dateType := range dates {
  1092. if d, e = time.Parse(dateType, s); e == nil {
  1093. return
  1094. }
  1095. }
  1096. return d, fmt.Errorf("unable to parse date: %s", s)
  1097. }
  1098. // jsonStringToObject attempts to unmarshall a string as JSON into
  1099. // the object passed as pointer.
  1100. func jsonStringToObject(s string, v interface{}) error {
  1101. data := []byte(s)
  1102. return json.Unmarshal(data, v)
  1103. }