From 95e5a13116d0d8cf6af64ee5940c19797e48e6cf Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 24 Feb 2023 23:38:15 -0700 Subject: decode: Add a DecodeArray function --- decode_test.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'decode_test.go') 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) } -- cgit v1.2.3-2-g168b