Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 8b0346e

Browse files
author
kevin.lin
committed
Add support for parsing json.Number to uint64
1 parent 5ac1f6a commit 8b0346e

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

mapstructure.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -684,16 +684,12 @@ func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) e
684684
}
685685
case dataType.PkgPath() == "encoding/json" && dataType.Name() == "Number":
686686
jn := data.(json.Number)
687-
i, err := jn.Int64()
687+
i, err := strconv.ParseUint(string(jn), 0, 64)
688688
if err != nil {
689689
return fmt.Errorf(
690690
"error decoding json.Number into %s: %s", name, err)
691691
}
692-
if i < 0 && !d.config.WeaklyTypedInput {
693-
return fmt.Errorf("cannot parse '%s', %d overflows uint",
694-
name, i)
695-
}
696-
val.SetUint(uint64(i))
692+
val.SetUint(i)
697693
default:
698694
return fmt.Errorf(
699695
"'%s' expected type '%s', got unconvertible type '%s', value: '%v'",

mapstructure_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Basic struct {
2424
Vdata interface{}
2525
VjsonInt int
2626
VjsonUint uint
27+
VjsonUint64 uint64
2728
VjsonFloat float64
2829
VjsonNumber json.Number
2930
}
@@ -224,6 +225,7 @@ func TestBasicTypes(t *testing.T) {
224225
"vdata": 42,
225226
"vjsonInt": json.Number("1234"),
226227
"vjsonUint": json.Number("1234"),
228+
"vjsonUint64": json.Number("9223372036854775809"), // 2^63 + 1
227229
"vjsonFloat": json.Number("1234.5"),
228230
"vjsonNumber": json.Number("1234.5"),
229231
}
@@ -287,6 +289,10 @@ func TestBasicTypes(t *testing.T) {
287289
t.Errorf("vjsonuint value should be 1234: %#v", result.VjsonUint)
288290
}
289291

292+
if result.VjsonUint64 != 9223372036854775809 {
293+
t.Errorf("vjsonuint64 value should be 9223372036854775809: %#v", result.VjsonUint64)
294+
}
295+
290296
if result.VjsonFloat != 1234.5 {
291297
t.Errorf("vjsonfloat value should be 1234.5: %#v", result.VjsonFloat)
292298
}

0 commit comments

Comments
 (0)