-
Notifications
You must be signed in to change notification settings - Fork 59
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: Options to support byte string map keys as struct field names #471
Comments
@benluddy Thanks for spotting this! 👍 I also initially preferred modifying Maybe new decoding option could be named Thanks again! |
Great, I'll open a PR today. On a related note, I realized after filing this issue that the behavior of the StringMode encode option similarly doesn't affect the CBOR type for struct fields. It would be impossible for a struct field name to be an invalid UTF-8 sequence, due to the syntax of Go identifiers, and it would be very strange for a field tag to be an invalid UTF-8 sequence (you'd have to go out of your way to do this with escape sequences in a tag), so I'm not concerned about producing invalid CBOR text strings. It does mean that validating decoders must validate all field names, and avoiding that overhead is one of the reasons a user would choose this option. Does a parallel encode option to set the struct field string type sound reasonable to you? It's not my use case, but I can imagine being unwilling/unable to validate/sanitize string values on the encoder side and still encoding struct field names (which we're practically sure are valid) as CBOR text strings. |
I included options for both encode and decode directions in #472. |
Closed by #472 |
Is your feature request related to a problem? Please describe.
I found a gap in the byte string options I contributed recently and I'm not sure whether it should be considered a bug in ByteStringToString or a separate feature request.
I need to support the case where a client encodes
map[string]interface{}{"f":1}
with the "String" option set to StringToByteString, transmits the CBOR payload to a server which decodes into a struct value with anint64
field and tagf
with "ByteStringToString" set to ByteStringToStringAllowed.Here's a test to illustrate:
Describe the solution you'd like
A decode option,
FieldNameByteString
(or similar), that controls whether byte string map keys are forbidden when decoding into a struct (the backwards-compatible default) or allowed. This is consistent with the existing MapKeyByteString and ByteStringToString options which make byte string decode behavior configurable per-destination-type.Describe alternatives you've considered
MapKeyByteString
is specified as controlling only decodes of CBOR maps with byte string keys into Go interface values.ByteStringToString
to also control decoding byte strings into struct field names, but on reflection I now feel that an option specific to struct destination types would be consistent with existing options.Additional context
The text was updated successfully, but these errors were encountered: