From de8690d4e684403c85652a1a90ab4133635a4a64 Mon Sep 17 00:00:00 2001 From: gnolizuh Date: Tue, 19 Mar 2024 20:13:41 +0800 Subject: [PATCH] feat: optional read supported --- decode.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/decode.go b/decode.go index 1f5f5c9..62f3b1c 100644 --- a/decode.go +++ b/decode.go @@ -1383,6 +1383,11 @@ func (d *decodeState) unmarshal(v any, opts decOpts) error { return &InvalidUnmarshalError{reflect.TypeOf(v)} } + // Return io.EOF to upper layer if reader has zero length. + if d.reader.Len() == 0 { + return io.EOF + } + // We decode rv not rv.Elem because the Unmarshaler interface // test must be applied at the top level of the value. err := d.value(rv, opts)