Skip to content
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: RawContent field type #329

Open
immesys opened this issue Mar 15, 2022 · 1 comment
Open

feature: RawContent field type #329

immesys opened this issue Mar 15, 2022 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@immesys
Copy link
Contributor

immesys commented Mar 15, 2022

Is your feature request related to a problem? Please describe.
A common pattern I have in my application is needing to compute the hash of the encoding of a struct field (for verifying a signature). It's annoying to use RawMessage because I do want the struct deserialized, and it's often fairly recursive, so there's a lot of boring added code to manually unmarshal the RawMessages manually for multiple layers.

Describe the solution you'd like
Go's ASN1 library has a neat solution for this: if the struct's first field is of type asn1.RawContent, then the bytes that are deserialized for the rest of the struct are present there in raw form. But it does not prevent the rest of the struct from being decoded, so you get the best of both worlds.

Describe alternatives you've considered
Other than the RawMessages approach, I considered a solution using a known name of field (like how the _ field is special) but I figured the odds of accidentally picking a name that people legitimately use is high. Thus the approach of introducing a new type and letting the user use any field name is safer.

Additional context
Here's an example of where this pattern occurs. I admit it looks contrived, but there are hundreds of occurrences of the asn1.RawContent being used this way on github, and CBOR targets similar use cases.

type Message struct {
	ToBeSigned struct {
		Raw cbor.RawContent // Allows for the outer signature to be verified
		Sender string
		Recipient string
		AEADEncryptedPayload []byte
		AdditionalAuthenticatedData struct {
			Raw cbor.RawContent // Allows this struct to be checked as part of the AEAD easily
			Foo string // But it's also handily unpacked automatically
			Bar string
		}
	}
	Signature []byte
}

I'm willing to do the work to implement this and submit a PR, if it's something you think you'd want.

NOTE: I'm new to this code base, and CBOR, so if there is already a better way, please let me know :-)

@fxamacker
Copy link
Owner

@immesys Thanks for suggesting this feature, I think it's a useful feature to add. 👍

I can implement this feature if you'd like, or you can open a PR if you're OK with the contributing guideline and pull request template. Anonymous code contributions are not accepted, so contributors would need to use their real name, etc.

@fxamacker fxamacker added this to the v2.5.0 milestone May 9, 2022
@fxamacker fxamacker self-assigned this May 30, 2022
@fxamacker fxamacker added the enhancement New feature or request label May 30, 2022
@fxamacker fxamacker modified the milestones: v2.5.0, v2.6.0 Jan 2, 2023
@fxamacker fxamacker modified the milestones: v2.6.0, v2.7.0 Feb 12, 2024
@fxamacker fxamacker removed this from the v2.7.0 milestone Sep 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants