mirror of https://github.com/spf13/cast.git
Merge pull request #213 from skyjerry/fix-uint64
Fix ToUint64 issue with string input exceeding maximum int64
This commit is contained in:
commit
487df00934
|
@ -111,6 +111,12 @@ func TestToUintE(t *testing.T) {
|
||||||
func TestToUint64E(t *testing.T) {
|
func TestToUint64E(t *testing.T) {
|
||||||
tests := createNumberTestSteps(uint64(0), uint64(1), uint64(8), uint64(0), uint64(8), uint64(8))
|
tests := createNumberTestSteps(uint64(0), uint64(1), uint64(8), uint64(0), uint64(8), uint64(8))
|
||||||
|
|
||||||
|
// Maximum value of uint64
|
||||||
|
tests = append(tests,
|
||||||
|
testStep{"18446744073709551615", uint64(18446744073709551615), false},
|
||||||
|
testStep{"18446744073709551616", uint64(0), true},
|
||||||
|
)
|
||||||
|
|
||||||
runNumberTest(
|
runNumberTest(
|
||||||
qt.New(t),
|
qt.New(t),
|
||||||
tests,
|
tests,
|
||||||
|
|
4
caste.go
4
caste.go
|
@ -613,12 +613,12 @@ 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 {
|
if v < 0 {
|
||||||
return 0, errNegativeNotAllowed
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint64(v), nil
|
return 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)
|
||||||
case json.Number:
|
case json.Number:
|
||||||
|
|
Loading…
Reference in New Issue