Skip to content

Commit 487df00

Browse files
authored
Merge pull request #213 from skyjerry/fix-uint64
Fix ToUint64 issue with string input exceeding maximum int64
2 parents 955c718 + 6e9731d commit 487df00

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

cast_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ func TestToUintE(t *testing.T) {
111111
func TestToUint64E(t *testing.T) {
112112
tests := createNumberTestSteps(uint64(0), uint64(1), uint64(8), uint64(0), uint64(8), uint64(8))
113113

114+
// Maximum value of uint64
115+
tests = append(tests,
116+
testStep{"18446744073709551615", uint64(18446744073709551615), false},
117+
testStep{"18446744073709551616", uint64(0), true},
118+
)
119+
114120
runNumberTest(
115121
qt.New(t),
116122
tests,

caste.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,12 +613,12 @@ func ToUint64E(i interface{}) (uint64, error) {
613613

614614
switch s := i.(type) {
615615
case string:
616-
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
616+
v, err := strconv.ParseUint(trimZeroDecimal(s), 0, 0)
617617
if err == nil {
618618
if v < 0 {
619619
return 0, errNegativeNotAllowed
620620
}
621-
return uint64(v), nil
621+
return v, nil
622622
}
623623
return 0, fmt.Errorf("unable to cast %#v of type %T to uint64", i, i)
624624
case json.Number:

0 commit comments

Comments
 (0)