mirror of https://github.com/spf13/cast.git
Support some commonly used Float64 interfaces
This commit is contained in:
parent
6c5f3fcc57
commit
6e0c3abdfe
44
caste.go
44
caste.go
|
@ -18,6 +18,14 @@ import (
|
||||||
|
|
||||||
var errNegativeNotAllowed = errors.New("unable to cast negative value")
|
var errNegativeNotAllowed = errors.New("unable to cast negative value")
|
||||||
|
|
||||||
|
type float64EProvider interface {
|
||||||
|
Float64() (float64, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type float64Provider interface {
|
||||||
|
Float64() float64
|
||||||
|
}
|
||||||
|
|
||||||
// ToTimeE casts an interface to a time.Time type.
|
// ToTimeE casts an interface to a time.Time type.
|
||||||
func ToTimeE(i interface{}) (tim time.Time, err error) {
|
func ToTimeE(i interface{}) (tim time.Time, err error) {
|
||||||
return ToTimeInDefaultLocationE(i, time.UTC)
|
return ToTimeInDefaultLocationE(i, time.UTC)
|
||||||
|
@ -77,11 +85,14 @@ func ToDurationE(i interface{}) (d time.Duration, err error) {
|
||||||
d, err = time.ParseDuration(s + "ns")
|
d, err = time.ParseDuration(s + "ns")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
case json.Number:
|
case float64EProvider:
|
||||||
var v float64
|
var v float64
|
||||||
v, err = s.Float64()
|
v, err = s.Float64()
|
||||||
d = time.Duration(v)
|
d = time.Duration(v)
|
||||||
return
|
return
|
||||||
|
case float64Provider:
|
||||||
|
d = time.Duration(s.Float64())
|
||||||
|
return
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("unable to cast %#v of type %T to Duration", i, i)
|
err = fmt.Errorf("unable to cast %#v of type %T to Duration", i, i)
|
||||||
return
|
return
|
||||||
|
@ -174,12 +185,14 @@ func ToFloat64E(i interface{}) (float64, error) {
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
return 0, fmt.Errorf("unable to cast %#v of type %T to float64", i, i)
|
return 0, fmt.Errorf("unable to cast %#v of type %T to float64", i, i)
|
||||||
case json.Number:
|
case float64EProvider:
|
||||||
v, err := s.Float64()
|
v, err := s.Float64()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
return 0, fmt.Errorf("unable to cast %#v of type %T to float64", i, i)
|
return 0, fmt.Errorf("unable to cast %#v of type %T to float64", i, i)
|
||||||
|
case float64Provider:
|
||||||
|
return s.Float64(), nil
|
||||||
case bool:
|
case bool:
|
||||||
if s {
|
if s {
|
||||||
return 1, nil
|
return 1, nil
|
||||||
|
@ -230,12 +243,14 @@ func ToFloat32E(i interface{}) (float32, error) {
|
||||||
return float32(v), nil
|
return float32(v), nil
|
||||||
}
|
}
|
||||||
return 0, fmt.Errorf("unable to cast %#v of type %T to float32", i, i)
|
return 0, fmt.Errorf("unable to cast %#v of type %T to float32", i, i)
|
||||||
case json.Number:
|
case float64EProvider:
|
||||||
v, err := s.Float64()
|
v, err := s.Float64()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return float32(v), nil
|
return float32(v), nil
|
||||||
}
|
}
|
||||||
return 0, fmt.Errorf("unable to cast %#v of type %T to float32", i, i)
|
return 0, fmt.Errorf("unable to cast %#v of type %T to float32", i, i)
|
||||||
|
case float64Provider:
|
||||||
|
return float32(s.Float64()), nil
|
||||||
case bool:
|
case bool:
|
||||||
if s {
|
if s {
|
||||||
return 1, nil
|
return 1, nil
|
||||||
|
@ -917,8 +932,8 @@ func indirectToStringerOrError(a interface{}) interface{} {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var errorType = reflect.TypeOf((*error)(nil)).Elem()
|
errorType := reflect.TypeOf((*error)(nil)).Elem()
|
||||||
var fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
|
fmtStringerType := reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
|
||||||
|
|
||||||
v := reflect.ValueOf(a)
|
v := reflect.ValueOf(a)
|
||||||
for !v.Type().Implements(fmtStringerType) && !v.Type().Implements(errorType) && v.Kind() == reflect.Ptr && !v.IsNil() {
|
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) {
|
||||||
|
|
||||||
// ToStringMapStringE casts an interface to a map[string]string type.
|
// ToStringMapStringE casts an interface to a map[string]string type.
|
||||||
func ToStringMapStringE(i interface{}) (map[string]string, error) {
|
func ToStringMapStringE(i interface{}) (map[string]string, error) {
|
||||||
var m = map[string]string{}
|
m := map[string]string{}
|
||||||
|
|
||||||
switch v := i.(type) {
|
switch v := i.(type) {
|
||||||
case map[string]string:
|
case map[string]string:
|
||||||
|
@ -1017,7 +1032,7 @@ func ToStringMapStringE(i interface{}) (map[string]string, error) {
|
||||||
|
|
||||||
// ToStringMapStringSliceE casts an interface to a map[string][]string type.
|
// ToStringMapStringSliceE casts an interface to a map[string][]string type.
|
||||||
func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) {
|
func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) {
|
||||||
var m = map[string][]string{}
|
m := map[string][]string{}
|
||||||
|
|
||||||
switch v := i.(type) {
|
switch v := i.(type) {
|
||||||
case map[string][]string:
|
case map[string][]string:
|
||||||
|
@ -1081,7 +1096,7 @@ func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) {
|
||||||
|
|
||||||
// ToStringMapBoolE casts an interface to a map[string]bool type.
|
// ToStringMapBoolE casts an interface to a map[string]bool type.
|
||||||
func ToStringMapBoolE(i interface{}) (map[string]bool, error) {
|
func ToStringMapBoolE(i interface{}) (map[string]bool, error) {
|
||||||
var m = map[string]bool{}
|
m := map[string]bool{}
|
||||||
|
|
||||||
switch v := i.(type) {
|
switch v := i.(type) {
|
||||||
case map[interface{}]interface{}:
|
case map[interface{}]interface{}:
|
||||||
|
@ -1106,7 +1121,7 @@ func ToStringMapBoolE(i interface{}) (map[string]bool, error) {
|
||||||
|
|
||||||
// ToStringMapE casts an interface to a map[string]interface{} type.
|
// ToStringMapE casts an interface to a map[string]interface{} type.
|
||||||
func ToStringMapE(i interface{}) (map[string]interface{}, error) {
|
func ToStringMapE(i interface{}) (map[string]interface{}, error) {
|
||||||
var m = map[string]interface{}{}
|
m := map[string]interface{}{}
|
||||||
|
|
||||||
switch v := i.(type) {
|
switch v := i.(type) {
|
||||||
case map[interface{}]interface{}:
|
case map[interface{}]interface{}:
|
||||||
|
@ -1126,7 +1141,7 @@ func ToStringMapE(i interface{}) (map[string]interface{}, error) {
|
||||||
|
|
||||||
// ToStringMapIntE casts an interface to a map[string]int{} type.
|
// ToStringMapIntE casts an interface to a map[string]int{} type.
|
||||||
func ToStringMapIntE(i interface{}) (map[string]int, error) {
|
func ToStringMapIntE(i interface{}) (map[string]int, error) {
|
||||||
var m = map[string]int{}
|
m := map[string]int{}
|
||||||
if i == nil {
|
if i == nil {
|
||||||
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int", i, i)
|
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int", i, i)
|
||||||
}
|
}
|
||||||
|
@ -1167,7 +1182,7 @@ func ToStringMapIntE(i interface{}) (map[string]int, error) {
|
||||||
|
|
||||||
// ToStringMapInt64E casts an interface to a map[string]int64{} type.
|
// ToStringMapInt64E casts an interface to a map[string]int64{} type.
|
||||||
func ToStringMapInt64E(i interface{}) (map[string]int64, error) {
|
func ToStringMapInt64E(i interface{}) (map[string]int64, error) {
|
||||||
var m = map[string]int64{}
|
m := map[string]int64{}
|
||||||
if i == nil {
|
if i == nil {
|
||||||
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int64", i, i)
|
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int64", i, i)
|
||||||
}
|
}
|
||||||
|
@ -1404,8 +1419,7 @@ func (f timeFormat) hasTimezone() bool {
|
||||||
return f.typ >= timeFormatNumericTimezone && f.typ <= timeFormatNumericAndNamedTimezone
|
return f.typ >= timeFormatNumericTimezone && f.typ <= timeFormatNumericAndNamedTimezone
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var timeFormats = []timeFormat{
|
||||||
timeFormats = []timeFormat{
|
|
||||||
// Keep common formats at the top.
|
// Keep common formats at the top.
|
||||||
{"2006-01-02", timeFormatNoTimezone},
|
{"2006-01-02", timeFormatNoTimezone},
|
||||||
{time.RFC3339, timeFormatNumericTimezone},
|
{time.RFC3339, timeFormatNumericTimezone},
|
||||||
|
@ -1431,11 +1445,9 @@ var (
|
||||||
{time.StampMilli, timeFormatTimeOnly},
|
{time.StampMilli, timeFormatTimeOnly},
|
||||||
{time.StampMicro, timeFormatTimeOnly},
|
{time.StampMicro, timeFormatTimeOnly},
|
||||||
{time.StampNano, timeFormatTimeOnly},
|
{time.StampNano, timeFormatTimeOnly},
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
func parseDateWith(s string, location *time.Location, formats []timeFormat) (d time.Time, e error) {
|
func parseDateWith(s string, location *time.Location, formats []timeFormat) (d time.Time, e error) {
|
||||||
|
|
||||||
for _, format := range formats {
|
for _, format := range formats {
|
||||||
if d, e = time.Parse(format.format, s); e == nil {
|
if d, e = time.Parse(format.format, s); e == nil {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue