mirror of https://github.com/spf13/cast.git
simplify the code
This commit is contained in:
parent
4b4f3c28f6
commit
97cf7b2944
15
caste.go
15
caste.go
|
@ -849,19 +849,20 @@ func ToStringE(i interface{}) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// support type redefinition
|
// support type redefinition
|
||||||
switch reflect.TypeOf(i).Kind() {
|
v := reflect.ValueOf(i)
|
||||||
|
switch v.Kind() {
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
return reflect.ValueOf(i).String(), nil
|
return v.String(), nil
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
return strconv.FormatBool(reflect.ValueOf(i).Bool()), nil
|
return strconv.FormatBool(v.Bool()), nil
|
||||||
case reflect.Float32:
|
case reflect.Float32:
|
||||||
return strconv.FormatFloat(reflect.ValueOf(i).Float(), 'f', -1, 32), nil
|
return strconv.FormatFloat(v.Float(), 'f', -1, 32), nil
|
||||||
case reflect.Float64:
|
case reflect.Float64:
|
||||||
return strconv.FormatFloat(reflect.ValueOf(i).Float(), 'f', -1, 64), nil
|
return strconv.FormatFloat(v.Float(), 'f', -1, 64), nil
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
return strconv.FormatInt(reflect.ValueOf(i).Int(), 10), nil
|
return strconv.FormatInt(v.Int(), 10), nil
|
||||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||||
return strconv.FormatUint(reflect.ValueOf(i).Uint(), 10), nil
|
return strconv.FormatUint(v.Uint(), 10), nil
|
||||||
}
|
}
|
||||||
return "", fmt.Errorf("unable to cast %#v of type %T to string", i, i)
|
return "", fmt.Errorf("unable to cast %#v of type %T to string", i, i)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue