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: Decode option to require case-sensitive field name matches #432

Closed
benluddy opened this issue Oct 31, 2023 · 4 comments
Closed
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@benluddy
Copy link
Contributor

Is your feature request related to a problem? Please describe.
I'm working on a project that serves and persists API resources in a JSON encoding. The existing JSON encoding is case-sensitive in matching JSON object keys to struct field names during decode. I would like to support CBOR as an alternative encoding with case-sensitive field matching.

Describe the solution you'd like
A new DecOption field that can disable fallback to case-insensitive field matching. The default would preserve the current behavior.

Additional context
It's particularly interesting when an input map has multiple case variations on a field tag. For example, if you're unmarshaling into:

struct {
  Foo string `json:"bar"`
}

The first matching key in the map wins. So "\xa2\x63bar\x65lower\x63BAR\x65UPPER" gives you "lower" and "\xa2\x63BAR\x65UPPER\x63bar\x65lower" gives you "UPPER". You can catch this if unknown field errors are enabled, but the map key that appears in the error also depends on where it appears in the input.

I'm happy to open a PR myself if you think this is a feature you'd accept.

@fxamacker
Copy link
Owner

Hi @benluddy, thanks for opening this issue! This sounds very useful, please feel free to open a PR. 👍

@fxamacker fxamacker added the enhancement New feature or request label Oct 31, 2023
@fxamacker fxamacker added this to the v2.6.0 milestone Nov 3, 2023
@fxamacker
Copy link
Owner

fxamacker commented Nov 3, 2023

Hey @benluddy, I can review your PR this weekend (probably Sunday) if it is available.

If you haven't started yet, I can try to implement this feature over the weekend and add it to next milestone for release.

Currently, this library and Go's encoding/json package use variants of case-insensitive field name matching, but I really like your suggestion! Thanks for opening this issue with details needed to make it easy to accept your suggestion! 👍

@benluddy
Copy link
Contributor Author

benluddy commented Nov 3, 2023

Thanks @fxamacker! I've just opened #433 for this.

@fxamacker
Copy link
Owner

Thanks again for opening this issue and PR! I updated my last comment (above) to be more precise.

This is closed by #433! 🎉

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