caste.go 33 KB

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