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

Allow for multiple tags #39

Open
kmuchmore opened this issue Aug 7, 2024 · 2 comments
Open

Allow for multiple tags #39

kmuchmore opened this issue Aug 7, 2024 · 2 comments

Comments

@kmuchmore
Copy link

I receive data from multiple sources and the key's of the data are not always the same.

For example, one source may provide a map similar to
{"LAT": 1, "LON": 2}

While another source may provide a map similar to
{"RLT_FOO": 1, "RLN_BAR": 2}

It would be very helpful if I could simply supply multiple tags for a single field.

For example

type Foo struct {
  Lat float64 `mapstructure:"LAT,RLT_FOO"`
  Lon float64 `mapstructure:"LON,RLN_BAR"`
}

The tags could be resolved in order and the first one that matches could be used.
This would avoid having to run a re-keying operation on maps before running through the mapstructure decoder.

I know there is the MatchName function but that only works if the variations are derivable from a base fieldName, if each field could have one of multiple unique keys it becomes a pain.

Also, if I'm just flat out missing a feature that would make this easy... please let me know.
Thanks

@sagikazarmark
Copy link
Member

To be completely honest with you, this seems like an edge case and the effort required to add support for this in a backwards compatible manner isn't worth it IMO.

What I would do in this case is one of the following:

  • If it's not too much effort, I'd keep different structs with common fields squashed
  • If that's too much effort, I'd write a custom decode hook for the struct to handle the different keys (here is an example)

Hope that helps!

@kmuchmore
Copy link
Author

I would have thought this was a pretty common occurrence

Through time an API response could go from

{"name":"foo", "addr":"bar", "phone":"888-888-8888"}
to
{"full_name":"foo", "address":"bar", "phone_number":"888-888-8888"}

Being able to quickly associate addr to address, name to full_name, and phone to phone_number seems useful.
I see this often when working with historical or archived data.

The other time I see this is when working with multiple services, one may respond with the same data but with their own flavor of keys.

We have mapping functions in place to fix these occurrences, so this isn't critical, just thought it would be a beneficial feature.
I understand the importance of backwards compatibility and appreciate the work put into maintaining this project.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants