hujson icon indicating copy to clipboard operation
hujson copied to clipboard

Add NewNames and Value.NormalizeNames

Open dsnet opened this issue 3 years ago • 4 comments

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.

dsnet avatar Sep 09 '21 23:09 dsnet

GitHub just pinged me about this. Do you still need a review here? Sitting since September 9th makes me think this wasn't needed?

bradfitz avatar Dec 13 '21 20:12 bradfitz

It's still helpful so that we can auto-format the ACL files to use consistent names. It's not critical.

dsnet avatar Dec 13 '21 20:12 dsnet

Talked to @josharian; he's taking this over from me.

bradfitz avatar Dec 13 '21 21:12 bradfitz

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?

josharian avatar Dec 13 '21 22:12 josharian