-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: add support for handling CBOR enumerated alternative data items #508
Comments
A reasonable way to approach this might be a flag in EDIT: my understanding of how this works was wrong |
@agaffney thanks for opening this issue and suggesting an approach! To confirm my understanding, the suggested approach doesn't change API or default behavior. It adds a new opt-in flag that makes codec provide both CBOR tag number and content (instead of just content) to custom type's Unmarshal function. This sounds promising, I'll try to take a look this month at existing tag-related code to confirm if this is feasible. |
@agaffney Thanks again for opening this issue! I took a closer look at the current tag-related code. Currently, both CBOR tag number and its content are passed to the custom type's I'd like more info. Can you share some example code for your use case? For example,
|
@fxamacker I'm decoding into You say that both the CBOR tag number and its content are passed to the custom type's |
I see, thanks!
Hmm, in the following code example, https://go.dev/play/p/-zSz6NcTiWL type enum struct {
data []byte
}
func (e *enum) UnmarshalCBOR(data []byte) error {
// 👉 data contains entire CBOR tag (both number and content)
e.data = append(e.data[:0], data...)
return nil
}
func main() {
data, _ := hex.DecodeString("d87a42ff00") // 122(h'ff00')
tags := cbor.NewTagSet()
_ = tags.Add(
cbor.TagOptions{EncTag: cbor.EncTagRequired, DecTag: cbor.DecTagRequired},
reflect.TypeOf(enum{}),
122,
)
dm, _ := cbor.DecOptions{}.DecModeWithTags(tags)
var v interface{}
err := dm.Unmarshal(data, &v)
if err != nil {
fmt.Printf("failed to unmarshal data 0x%x: %s\n", data, err)
} else {
fmt.Printf("enum.data: %x\n", v.(enum).data)
}
// Output:
// enum.data: d87a42ff00
} |
@fxamacker it seems there may be more magic than I had thought. In your example, I can take |
I can now confirm that I can capture that tag number, but what I can't do is register multiple tag numbers with the same object. |
Is your feature request related to a problem? Please describe.
There doesn't seem to be a reasonable way to implement support for CBOR alternatives (as described in section 9.1 of https://datatracker.ietf.org/doc/draft-bormann-cbor-notable-tags/). There are 129 tag numbers that can be represented by 3 different types, but there's no way to use
TagSet
to map multiple tag numbers to the same type in a way that the tag number can be preserved for later reference.Describe the solution you'd like
I need to be able to support arbitrary CBOR alternative numbers without creating a separate type for each and every tag number. I don't really have a good suggestion for how to accomplish this without adding support directly into the library.
Describe alternatives you've considered
N/A
Additional context
N/A
The text was updated successfully, but these errors were encountered: