stretchr / objx

Go package for dealing with maps, slices, JSON and other data.
MIT License
691 stars 75 forks source link

Unable to select map key with dot #89

Closed cretz closed 5 years ago

cretz commented 5 years ago

If I have a map where the keys may be domain names and the values are some object, how do I select a field on the value for key "google.com" via Get?

hanzei commented 5 years ago

Get("google.com")should work. Please provide a failing test case, if it doesn't.

cretz commented 5 years ago

I was more hoping Get("domains[google.com].hostname"). I can provide a test case if necessary. Basically, there is no way to escape special chars in the selector parser that I see.

hanzei commented 5 years ago

A test case would be great!

cretz commented 5 years ago

https://github.com/cretz/objx/commit/808954a2151189a03ff5ac68029bc89572ab4eff

hanzei commented 5 years ago

I see. What would be your suggestion to fix this? Escape with \.?

geseq commented 5 years ago

One option would be to make the . and [] interchangeable for maps. So rather than escape separately, [] could be used as the accessor.

That is:

Get("a.b") == Get("a[b]")
hanzei commented 5 years ago

I like this idea. Are you interested in implementing it?

geseq commented 5 years ago

Yeah, I'll work on it.