From bb06d38c87df69c925bc8de23e4457020ecd30b5 Mon Sep 17 00:00:00 2001 From: Faye Amacker <33205765+fxamacker@users.noreply.github.com> Date: Sat, 6 May 2023 17:09:14 -0500 Subject: [PATCH] Add UnmarshalFirst to DecMode interface UnmarshalFirst parses the first CBOR data item using the decoding mode. Any remaining bytes are returned in rest. --- decode.go | 8 ++++++++ decode_test.go | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/decode.go b/decode.go index dbfc0692..c0ee14ab 100644 --- a/decode.go +++ b/decode.go @@ -536,6 +536,14 @@ type DecMode interface { // See the documentation for Unmarshal for details. Unmarshal(data []byte, v interface{}) error + // UnmarshalFirst parses the first CBOR data item into the value pointed to by v + // using the decoding mode. Any remaining bytes are returned in rest. + // + // If v is nil, not a pointer, or a nil pointer, UnmarshalFirst returns an error. + // + // See the documentation for Unmarshal for details. + UnmarshalFirst(data []byte, v interface{}) (rest []byte, err error) + // Valid checks whether data is a well-formed encoded CBOR data item and // that it complies with configurable restrictions such as MaxNestedLevels, // MaxArrayElements, MaxMapPairs, etc. diff --git a/decode_test.go b/decode_test.go index cc0e9837..ea3d84d4 100644 --- a/decode_test.go +++ b/decode_test.go @@ -6034,7 +6034,7 @@ func TestUnmarshalFirstInvalidItem(t *testing.T) { invalidCBOR := hexDecode("83FF20030102") var v interface{} rest, err := UnmarshalFirst(invalidCBOR, &v) - if rest != nil { + if rest != nil || err == nil { t.Errorf("UnmarshalFirst(0x%x) = (%x, %v), want (nil, err)", invalidCBOR, rest, err) } }