mirror of https://github.com/spf13/cast.git
Fix uint parsing
This commit is contained in:
parent
2b0eb0f724
commit
eba453e696
25
caste.go
25
caste.go
|
@ -501,11 +501,8 @@ func ToUintE(i interface{}) (uint, error) {
|
||||||
|
|
||||||
switch s := i.(type) {
|
switch s := i.(type) {
|
||||||
case string:
|
case string:
|
||||||
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
|
v, err := strconv.ParseUint(trimZeroDecimal(s), 0, 0)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if v < 0 {
|
|
||||||
return 0, errNegativeNotAllowed
|
|
||||||
}
|
|
||||||
return uint(v), nil
|
return uint(v), nil
|
||||||
}
|
}
|
||||||
return 0, fmt.Errorf("unable to cast %#v of type %T to uint", i, i)
|
return 0, fmt.Errorf("unable to cast %#v of type %T to uint", i, i)
|
||||||
|
@ -577,11 +574,8 @@ func ToUint64E(i interface{}) (uint64, error) {
|
||||||
|
|
||||||
switch s := i.(type) {
|
switch s := i.(type) {
|
||||||
case string:
|
case string:
|
||||||
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
|
v, err := strconv.ParseUint(trimZeroDecimal(s), 0, 0)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if v < 0 {
|
|
||||||
return 0, errNegativeNotAllowed
|
|
||||||
}
|
|
||||||
return uint64(v), nil
|
return uint64(v), nil
|
||||||
}
|
}
|
||||||
return 0, fmt.Errorf("unable to cast %#v of type %T to uint64", i, i)
|
return 0, fmt.Errorf("unable to cast %#v of type %T to uint64", i, i)
|
||||||
|
@ -653,11 +647,8 @@ func ToUint32E(i interface{}) (uint32, error) {
|
||||||
|
|
||||||
switch s := i.(type) {
|
switch s := i.(type) {
|
||||||
case string:
|
case string:
|
||||||
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
|
v, err := strconv.ParseUint(trimZeroDecimal(s), 0, 0)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if v < 0 {
|
|
||||||
return 0, errNegativeNotAllowed
|
|
||||||
}
|
|
||||||
return uint32(v), nil
|
return uint32(v), nil
|
||||||
}
|
}
|
||||||
return 0, fmt.Errorf("unable to cast %#v of type %T to uint32", i, i)
|
return 0, fmt.Errorf("unable to cast %#v of type %T to uint32", i, i)
|
||||||
|
@ -729,11 +720,8 @@ func ToUint16E(i interface{}) (uint16, error) {
|
||||||
|
|
||||||
switch s := i.(type) {
|
switch s := i.(type) {
|
||||||
case string:
|
case string:
|
||||||
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
|
v, err := strconv.ParseUint(trimZeroDecimal(s), 0, 0)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if v < 0 {
|
|
||||||
return 0, errNegativeNotAllowed
|
|
||||||
}
|
|
||||||
return uint16(v), nil
|
return uint16(v), nil
|
||||||
}
|
}
|
||||||
return 0, fmt.Errorf("unable to cast %#v of type %T to uint16", i, i)
|
return 0, fmt.Errorf("unable to cast %#v of type %T to uint16", i, i)
|
||||||
|
@ -805,11 +793,8 @@ func ToUint8E(i interface{}) (uint8, error) {
|
||||||
|
|
||||||
switch s := i.(type) {
|
switch s := i.(type) {
|
||||||
case string:
|
case string:
|
||||||
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
|
v, err := strconv.ParseUint(trimZeroDecimal(s), 0, 0)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if v < 0 {
|
|
||||||
return 0, errNegativeNotAllowed
|
|
||||||
}
|
|
||||||
return uint8(v), nil
|
return uint8(v), nil
|
||||||
}
|
}
|
||||||
return 0, fmt.Errorf("unable to cast %#v of type %T to uint8", i, i)
|
return 0, fmt.Errorf("unable to cast %#v of type %T to uint8", i, i)
|
||||||
|
|
Loading…
Reference in New Issue