hujson
hujson copied to clipboard
Add NewNames and Value.NormalizeNames
The NewNames function produces a map of canonical names from a Go struct. It can be passed to Value.NormalizeNames to normalizes case-insensitive matches to a JSON object name to the canonical name.
GitHub just pinged me about this. Do you still need a review here? Sitting since September 9th makes me think this wasn't needed?
It's still helpful so that we can auto-format the ACL files to use consistent names. It's not critical.
Talked to @josharian; he's taking this over from me.
So far, I've looked just at the new API, not the implementation.
I think I understand the goal, but I'm not sure the API quite matches.
Consider:
type LL struct {
S string
Next *LL `json:"ref"`
}
I don't think there's a way to write a Names
map that does the right thing for LL.
It seems like we want a mapping per type, but there's nothing good to use as a key. reflect.Type
, any
, and reflect.Value
would all be annoying when used with JSON with anonymous struct types, which are reasonably common.
Maybe instead the "Names" type should be a single any
value, and do the name mapping on the fly as needed? Then you lose the ability to provide your own independent of struct fields, but maybe that's a feature? You also lose the ability to do that computation once and then re-use it, but...oh well?
Maybe instead the "Names" type should embrace this shortcoming and be a single flat map. And if you need to rename keys differently based on context, oh well?