@@ -18,6 +18,14 @@ import (
1818
1919var errNegativeNotAllowed = errors .New ("unable to cast negative value" )
2020
21+ type float64EProvider interface {
22+ Float64 () (float64 , error )
23+ }
24+
25+ type float64Provider interface {
26+ Float64 () float64
27+ }
28+
2129// ToTimeE casts an interface to a time.Time type.
2230func ToTimeE (i interface {}) (tim time.Time , err error ) {
2331 return ToTimeInDefaultLocationE (i , time .UTC )
@@ -77,11 +85,14 @@ func ToDurationE(i interface{}) (d time.Duration, err error) {
7785 d , err = time .ParseDuration (s + "ns" )
7886 }
7987 return
80- case json. Number :
88+ case float64EProvider :
8189 var v float64
8290 v , err = s .Float64 ()
8391 d = time .Duration (v )
8492 return
93+ case float64Provider :
94+ d = time .Duration (s .Float64 ())
95+ return
8596 default :
8697 err = fmt .Errorf ("unable to cast %#v of type %T to Duration" , i , i )
8798 return
@@ -174,12 +185,14 @@ func ToFloat64E(i interface{}) (float64, error) {
174185 return v , nil
175186 }
176187 return 0 , fmt .Errorf ("unable to cast %#v of type %T to float64" , i , i )
177- case json. Number :
188+ case float64EProvider :
178189 v , err := s .Float64 ()
179190 if err == nil {
180191 return v , nil
181192 }
182193 return 0 , fmt .Errorf ("unable to cast %#v of type %T to float64" , i , i )
194+ case float64Provider :
195+ return s .Float64 (), nil
183196 case bool :
184197 if s {
185198 return 1 , nil
@@ -230,12 +243,14 @@ func ToFloat32E(i interface{}) (float32, error) {
230243 return float32 (v ), nil
231244 }
232245 return 0 , fmt .Errorf ("unable to cast %#v of type %T to float32" , i , i )
233- case json. Number :
246+ case float64EProvider :
234247 v , err := s .Float64 ()
235248 if err == nil {
236249 return float32 (v ), nil
237250 }
238251 return 0 , fmt .Errorf ("unable to cast %#v of type %T to float32" , i , i )
252+ case float64Provider :
253+ return float32 (s .Float64 ()), nil
239254 case bool :
240255 if s {
241256 return 1 , nil
@@ -917,8 +932,8 @@ func indirectToStringerOrError(a interface{}) interface{} {
917932 return nil
918933 }
919934
920- var errorType = reflect .TypeOf ((* error )(nil )).Elem ()
921- var fmtStringerType = reflect .TypeOf ((* fmt .Stringer )(nil )).Elem ()
935+ errorType : = reflect .TypeOf ((* error )(nil )).Elem ()
936+ fmtStringerType : = reflect .TypeOf ((* fmt .Stringer )(nil )).Elem ()
922937
923938 v := reflect .ValueOf (a )
924939 for ! v .Type ().Implements (fmtStringerType ) && ! v .Type ().Implements (errorType ) && v .Kind () == reflect .Ptr && ! v .IsNil () {
@@ -987,7 +1002,7 @@ func ToStringE(i interface{}) (string, error) {
9871002
9881003// ToStringMapStringE casts an interface to a map[string]string type.
9891004func ToStringMapStringE (i interface {}) (map [string ]string , error ) {
990- var m = map [string ]string {}
1005+ m : = map [string ]string {}
9911006
9921007 switch v := i .(type ) {
9931008 case map [string ]string :
@@ -1017,7 +1032,7 @@ func ToStringMapStringE(i interface{}) (map[string]string, error) {
10171032
10181033// ToStringMapStringSliceE casts an interface to a map[string][]string type.
10191034func ToStringMapStringSliceE (i interface {}) (map [string ][]string , error ) {
1020- var m = map [string ][]string {}
1035+ m : = map [string ][]string {}
10211036
10221037 switch v := i .(type ) {
10231038 case map [string ][]string :
@@ -1081,7 +1096,7 @@ func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) {
10811096
10821097// ToStringMapBoolE casts an interface to a map[string]bool type.
10831098func ToStringMapBoolE (i interface {}) (map [string ]bool , error ) {
1084- var m = map [string ]bool {}
1099+ m : = map [string ]bool {}
10851100
10861101 switch v := i .(type ) {
10871102 case map [interface {}]interface {}:
@@ -1106,7 +1121,7 @@ func ToStringMapBoolE(i interface{}) (map[string]bool, error) {
11061121
11071122// ToStringMapE casts an interface to a map[string]interface{} type.
11081123func ToStringMapE (i interface {}) (map [string ]interface {}, error ) {
1109- var m = map [string ]interface {}{}
1124+ m : = map [string ]interface {}{}
11101125
11111126 switch v := i .(type ) {
11121127 case map [interface {}]interface {}:
@@ -1126,7 +1141,7 @@ func ToStringMapE(i interface{}) (map[string]interface{}, error) {
11261141
11271142// ToStringMapIntE casts an interface to a map[string]int{} type.
11281143func ToStringMapIntE (i interface {}) (map [string ]int , error ) {
1129- var m = map [string ]int {}
1144+ m : = map [string ]int {}
11301145 if i == nil {
11311146 return m , fmt .Errorf ("unable to cast %#v of type %T to map[string]int" , i , i )
11321147 }
@@ -1167,7 +1182,7 @@ func ToStringMapIntE(i interface{}) (map[string]int, error) {
11671182
11681183// ToStringMapInt64E casts an interface to a map[string]int64{} type.
11691184func ToStringMapInt64E (i interface {}) (map [string ]int64 , error ) {
1170- var m = map [string ]int64 {}
1185+ m : = map [string ]int64 {}
11711186 if i == nil {
11721187 return m , fmt .Errorf ("unable to cast %#v of type %T to map[string]int64" , i , i )
11731188 }
@@ -1404,38 +1419,35 @@ func (f timeFormat) hasTimezone() bool {
14041419 return f .typ >= timeFormatNumericTimezone && f .typ <= timeFormatNumericAndNamedTimezone
14051420}
14061421
1407- var (
1408- timeFormats = []timeFormat {
1409- // Keep common formats at the top.
1410- {"2006-01-02" , timeFormatNoTimezone },
1411- {time .RFC3339 , timeFormatNumericTimezone },
1412- {"2006-01-02T15:04:05" , timeFormatNoTimezone }, // iso8601 without timezone
1413- {time .RFC1123Z , timeFormatNumericTimezone },
1414- {time .RFC1123 , timeFormatNamedTimezone },
1415- {time .RFC822Z , timeFormatNumericTimezone },
1416- {time .RFC822 , timeFormatNamedTimezone },
1417- {time .RFC850 , timeFormatNamedTimezone },
1418- {"2006-01-02 15:04:05.999999999 -0700 MST" , timeFormatNumericAndNamedTimezone }, // Time.String()
1419- {"2006-01-02T15:04:05-0700" , timeFormatNumericTimezone }, // RFC3339 without timezone hh:mm colon
1420- {"2006-01-02 15:04:05Z0700" , timeFormatNumericTimezone }, // RFC3339 without T or timezone hh:mm colon
1421- {"2006-01-02 15:04:05" , timeFormatNoTimezone },
1422- {time .ANSIC , timeFormatNoTimezone },
1423- {time .UnixDate , timeFormatNamedTimezone },
1424- {time .RubyDate , timeFormatNumericTimezone },
1425- {"2006-01-02 15:04:05Z07:00" , timeFormatNumericTimezone },
1426- {"02 Jan 2006" , timeFormatNoTimezone },
1427- {"2006-01-02 15:04:05 -07:00" , timeFormatNumericTimezone },
1428- {"2006-01-02 15:04:05 -0700" , timeFormatNumericTimezone },
1429- {time .Kitchen , timeFormatTimeOnly },
1430- {time .Stamp , timeFormatTimeOnly },
1431- {time .StampMilli , timeFormatTimeOnly },
1432- {time .StampMicro , timeFormatTimeOnly },
1433- {time .StampNano , timeFormatTimeOnly },
1434- }
1435- )
1422+ var timeFormats = []timeFormat {
1423+ // Keep common formats at the top.
1424+ {"2006-01-02" , timeFormatNoTimezone },
1425+ {time .RFC3339 , timeFormatNumericTimezone },
1426+ {"2006-01-02T15:04:05" , timeFormatNoTimezone }, // iso8601 without timezone
1427+ {time .RFC1123Z , timeFormatNumericTimezone },
1428+ {time .RFC1123 , timeFormatNamedTimezone },
1429+ {time .RFC822Z , timeFormatNumericTimezone },
1430+ {time .RFC822 , timeFormatNamedTimezone },
1431+ {time .RFC850 , timeFormatNamedTimezone },
1432+ {"2006-01-02 15:04:05.999999999 -0700 MST" , timeFormatNumericAndNamedTimezone }, // Time.String()
1433+ {"2006-01-02T15:04:05-0700" , timeFormatNumericTimezone }, // RFC3339 without timezone hh:mm colon
1434+ {"2006-01-02 15:04:05Z0700" , timeFormatNumericTimezone }, // RFC3339 without T or timezone hh:mm colon
1435+ {"2006-01-02 15:04:05" , timeFormatNoTimezone },
1436+ {time .ANSIC , timeFormatNoTimezone },
1437+ {time .UnixDate , timeFormatNamedTimezone },
1438+ {time .RubyDate , timeFormatNumericTimezone },
1439+ {"2006-01-02 15:04:05Z07:00" , timeFormatNumericTimezone },
1440+ {"02 Jan 2006" , timeFormatNoTimezone },
1441+ {"2006-01-02 15:04:05 -07:00" , timeFormatNumericTimezone },
1442+ {"2006-01-02 15:04:05 -0700" , timeFormatNumericTimezone },
1443+ {time .Kitchen , timeFormatTimeOnly },
1444+ {time .Stamp , timeFormatTimeOnly },
1445+ {time .StampMilli , timeFormatTimeOnly },
1446+ {time .StampMicro , timeFormatTimeOnly },
1447+ {time .StampNano , timeFormatTimeOnly },
1448+ }
14361449
14371450func parseDateWith (s string , location * time.Location , formats []timeFormat ) (d time.Time , e error ) {
1438-
14391451 for _ , format := range formats {
14401452 if d , e = time .Parse (format .format , s ); e == nil {
14411453
0 commit comments