diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-25 00:58:05 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-25 00:58:05 -0700 |
commit | 03778c094d995791f6c3df08afeeb792f33f35a5 (patch) | |
tree | 31f9fd24375b1db02d47b7c80dcb02544c2a1582 /decode_test.go | |
parent | e3c62b8de808c29ad70c1cfb98b4d1b164c05745 (diff) | |
parent | 95e5a13116d0d8cf6af64ee5940c19797e48e6cf (diff) |
Merge branch 'lukeshu/decode-string'
Diffstat (limited to 'decode_test.go')
-rw-r--r-- | decode_test.go | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/decode_test.go b/decode_test.go index c224f3a..6d27347 100644 --- a/decode_test.go +++ b/decode_test.go @@ -82,16 +82,34 @@ func (o *testObj) DecodeJSON(r io.RuneScanner) error { ) } +type testStr string + +func (s *testStr) DecodeJSON(r io.RuneScanner) error { + var buf strings.Builder + if err := DecodeString(r, &buf); err != nil { + return err + } + *s = testStr(buf.String()) + return nil +} + +var ( + _ Decodable = (*testAry)(nil) + _ Decodable = (*testObj)(nil) + _ Decodable = (*testStr)(nil) +) + func TestDecodeTypeError(t *testing.T) { t.Parallel() type outType struct { First int Second testAry Third testObj + Fourth testStr } var out outType - err := NewDecoder(strings.NewReader(`{"First": 1.2, "Second": [3], "Third": {"a":4}}`)).Decode(&out) + err := NewDecoder(strings.NewReader(`{"First": 1.2, "Second": [3], "Third": {"a":4}, "Fourth": "foo\u000Abar"}`)).Decode(&out) assert.EqualError(t, err, `json: v["First"]: cannot decode JSON number 1.2 at input byte 9 into Go int: strconv.ParseInt: parsing "1.2": invalid syntax`) - assert.Equal(t, outType{First: 0, Second: testAry{3}, Third: testObj{"a": 4}}, out) + assert.Equal(t, outType{First: 0, Second: testAry{3}, Third: testObj{"a": 4}, Fourth: "foo\nbar"}, out) } |