Add RawMessage.Type to report a value's CBOR major type - #805
Open
ChrisJr404 wants to merge 1 commit into
Open
Conversation
Inspecting the outer kind of a cbor.RawMessage previously required either decoding it into a Go value or hand-rolling a partial decoder over the initial byte. Add a Type method that returns the CBOR major type read from the initial byte, mirroring the way the first byte of a json.RawMessage can be inspected to branch on its kind, along with an exported Type enum and a String method that reuses the existing type descriptions. Type only looks at the initial byte and does not verify well-formedness, so it stays cheap; callers that need validation can still use Wellformed. Closes fxamacker#440. Signed-off-by: Chris (ChrisJr404) <11917633+ChrisJr404@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This adds a
Typemethod toRawMessageso callers can check the outer kind of a raw value without fully decoding it, which is what #440 asked for.The motivation is the same one from that issue: with
json.RawMessageyou can peek at the first byte to see whether you're looking at an object, array, string, etc., but doing the equivalent with acbor.RawMessagemeant writing a little partial decoder over the initial byte and remembering the CBOR major type encoding.RawMessage.Type()reads the major type from the initial byte and returns it as an exportedTypevalue, so branching on the kind is a one-liner:The exported
Typemirrors the major types in RFC 8949 Section 3.1, and itsString()reuses the descriptions the package already had internally so there's no second copy of that mapping to keep in sync.Type()only looks at the initial byte and deliberately does not check well-formedness, so it stays cheap and has no effect on anyone not calling it; callers that need validation can still useWellformed. It returns an error when the message is empty.PR Was Proposed and Welcomed in Currently Open Issue
Checklist (for code PR only, ignore for docs PR)
Certify the Developer's Certificate of Origin 1.1