diff options
| author | Jirka Daněk <dnk@mail.muni.cz> | 2016-01-18 16:26:05 +0100 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2016-10-05 20:28:59 +0000 |
| commit | b9fd510cd00b6aa26e2ea7001a07b90ebf97d2ed (patch) | |
| tree | 6b321a11850520fbb60d0f6048e3ef24808d2850 /src/encoding/json/decode_test.go | |
| parent | cb986def671cd244e4682a476cff51c4cff2d8f8 (diff) | |
| download | go-b9fd510cd00b6aa26e2ea7001a07b90ebf97d2ed.tar.xz | |
encoding/json: add struct and field name to UnmarshalTypeError message
The UnmarshalTypeError has two new fields Struct and Field,
used when constructing the error message.
Fixes #6716.
Change-Id: I67da171480a9491960b3ae81893770644180f848
Reviewed-on: https://go-review.googlesource.com/18692
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/encoding/json/decode_test.go')
| -rw-r--r-- | src/encoding/json/decode_test.go | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/src/encoding/json/decode_test.go b/src/encoding/json/decode_test.go index 04fbd7524d..37dbfeb5f3 100644 --- a/src/encoding/json/decode_test.go +++ b/src/encoding/json/decode_test.go @@ -33,6 +33,11 @@ type V struct { F1 interface{} F2 int32 F3 Number + F4 *VOuter +} + +type VOuter struct { + V V } // ifaceNumAsFloat64/ifaceNumAsNumber are used to test unmarshaling with and @@ -389,7 +394,7 @@ var unmarshalTests = []unmarshalTest{ {in: `"g-clef: \uD834\uDD1E"`, ptr: new(string), out: "g-clef: \U0001D11E"}, {in: `"invalid: \uD834x\uDD1E"`, ptr: new(string), out: "invalid: \uFFFDx\uFFFD"}, {in: "null", ptr: new(interface{}), out: nil}, - {in: `{"X": [1,2,3], "Y": 4}`, ptr: new(T), out: T{Y: 4}, err: &UnmarshalTypeError{"array", reflect.TypeOf(""), 7}}, + {in: `{"X": [1,2,3], "Y": 4}`, ptr: new(T), out: T{Y: 4}, err: &UnmarshalTypeError{"array", reflect.TypeOf(""), 7, "T", "X"}}, {in: `{"x": 1}`, ptr: new(tx), out: tx{}}, {in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: float64(1), F2: int32(2), F3: Number("3")}}, {in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: Number("1"), F2: int32(2), F3: Number("3")}, useNumber: true}, @@ -504,22 +509,22 @@ var unmarshalTests = []unmarshalTest{ { in: `{"abc":"abc"}`, ptr: new(map[int]string), - err: &UnmarshalTypeError{"number abc", reflect.TypeOf(0), 2}, + err: &UnmarshalTypeError{Value: "number abc", Type: reflect.TypeOf(0), Offset: 2}, }, { in: `{"256":"abc"}`, ptr: new(map[uint8]string), - err: &UnmarshalTypeError{"number 256", reflect.TypeOf(uint8(0)), 2}, + err: &UnmarshalTypeError{Value: "number 256", Type: reflect.TypeOf(uint8(0)), Offset: 2}, }, { in: `{"128":"abc"}`, ptr: new(map[int8]string), - err: &UnmarshalTypeError{"number 128", reflect.TypeOf(int8(0)), 2}, + err: &UnmarshalTypeError{Value: "number 128", Type: reflect.TypeOf(int8(0)), Offset: 2}, }, { in: `{"-1":"abc"}`, ptr: new(map[uint8]string), - err: &UnmarshalTypeError{"number -1", reflect.TypeOf(uint8(0)), 2}, + err: &UnmarshalTypeError{Value: "number -1", Type: reflect.TypeOf(uint8(0)), Offset: 2}, }, // Map keys can be encoding.TextUnmarshalers. @@ -653,12 +658,12 @@ var unmarshalTests = []unmarshalTest{ { in: `{"2009-11-10T23:00:00Z": "hello world"}`, ptr: &map[Point]string{}, - err: &UnmarshalTypeError{"object", reflect.TypeOf(map[Point]string{}), 1}, + err: &UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(map[Point]string{}), Offset: 1}, }, { in: `{"asdf": "hello world"}`, ptr: &map[unmarshaler]string{}, - err: &UnmarshalTypeError{"object", reflect.TypeOf(map[unmarshaler]string{}), 1}, + err: &UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(map[unmarshaler]string{}), Offset: 1}, }, // related to issue 13783. @@ -750,6 +755,29 @@ var unmarshalTests = []unmarshalTest{ {in: `999999999999999900000`, ptr: new(float64), out: 999999999999999900000.0, golden: true}, {in: `9007199254740992`, ptr: new(float64), out: 9007199254740992.0, golden: true}, {in: `9007199254740993`, ptr: new(float64), out: 9007199254740992.0, golden: false}, + + { + in: `{"V": {"F2": "hello"}}`, + ptr: new(VOuter), + err: &UnmarshalTypeError{ + Value: "string", + Struct: "V", + Field: "F2", + Type: reflect.TypeOf(int32(0)), + Offset: 20, + }, + }, + { + in: `{"V": {"F4": {}, "F2": "hello"}}`, + ptr: new(VOuter), + err: &UnmarshalTypeError{ + Value: "string", + Struct: "V", + Field: "F2", + Type: reflect.TypeOf(int32(0)), + Offset: 30, + }, + }, } func TestMarshal(t *testing.T) { |
