caste.go 32 KB

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