diff options
author | Luke Shumaker <lukeshu@datawire.io> | 2022-08-21 21:39:59 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@datawire.io> | 2022-08-21 21:39:59 -0600 |
commit | 325838f35ce90080aa6c892a998d960c06c1c144 (patch) | |
tree | 60e87ee9a622a0261b93ef1e47dbca632c588276 /decode.go | |
parent | d25456172946e5921747cd57fb04eb5b6da72fb6 (diff) |
Add tests for the actual usability of the Decodable and Encodable interfaces
Diffstat (limited to 'decode.go')
-rw-r--r-- | decode.go | 34 |
1 files changed, 26 insertions, 8 deletions
@@ -300,9 +300,13 @@ func (dec *Decoder) decode(val reflect.Value, nullOK bool) { case val.CanAddr() && reflect.PointerTo(typ).Implements(decodableType): t := dec.peekRuneType() obj := val.Addr().Interface().(Decodable) - if err := obj.DecodeJSON(dec.limitingScanner()); err != nil { + l := dec.limitingScanner() + if err := obj.DecodeJSON(l); err != nil { dec.panicType(t.jsonType(), typ, err) } + if _, _, err := l.ReadRune(); err != io.EOF { + dec.panicType(t.jsonType(), typ, fmt.Errorf("did not consume entire %s", t.jsonType())) + } case val.CanAddr() && reflect.PointerTo(typ).Implements(jsonUnmarshalerType): t := dec.peekRuneType() var buf bytes.Buffer @@ -724,7 +728,7 @@ func (dec *Decoder) decodeAny() any { } // DecodeObject is a helper function for implementing the Decoder interface. -func DecodeObject(r io.RuneScanner, decodeKey, decodeVal func() error) (err error) { +func DecodeObject(r io.RuneScanner, decodeKey, decodeVal func(io.RuneScanner) error) (err error) { defer func() { if r := recover(); r != nil { if de, ok := r.(decodeError); ok { @@ -738,13 +742,22 @@ func DecodeObject(r io.RuneScanner, decodeKey, decodeVal func() error) (err erro dec := NewDecoder(r) dec.decodeObject(nil, func() { - if err := decodeKey(); err != nil { + l := dec.limitingScanner() + if err := decodeKey(l); err != nil { dec.panicType("string", nil, err) } + if _, _, err := l.ReadRune(); err != io.EOF { + dec.panicType("string", nil, fmt.Errorf("did not consume entire string")) + } }, func() { - if err := decodeVal(); err != nil { - dec.panicType("", nil, err) + t := dec.peekRuneType() + l := dec.limitingScanner() + if err := decodeVal(l); err != nil { + dec.panicType(t.jsonType(), nil, err) + } + if _, _, err := l.ReadRune(); err != io.EOF { + dec.panicType(t.jsonType(), nil, fmt.Errorf("did not consume entire %s", t.jsonType())) } }) return @@ -778,7 +791,7 @@ func (dec *Decoder) decodeObject(gTyp reflect.Type, decodeKey, decodeVal func()) } // DecodeArray is a helper function for implementing the Decoder interface. -func DecodeArray(r io.RuneScanner, decodeMember func() error) (err error) { +func DecodeArray(r io.RuneScanner, decodeMember func(r io.RuneScanner) error) (err error) { defer func() { if r := recover(); r != nil { if de, ok := r.(decodeError); ok { @@ -791,8 +804,13 @@ func DecodeArray(r io.RuneScanner, decodeMember func() error) (err error) { }() dec := NewDecoder(r) dec.decodeArray(nil, func() { - if err := decodeMember(); err != nil { - dec.panicType("array", nil, err) + t := dec.peekRuneType() + l := dec.limitingScanner() + if err := decodeMember(l); err != nil { + dec.panicType(t.jsonType(), nil, err) + } + if _, _, err := l.ReadRune(); err != io.EOF { + dec.panicType(t.jsonType(), nil, fmt.Errorf("did not consume entire %s", t.jsonType())) } }) return |