You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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)
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.
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
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
The text was updated successfully, but these errors were encountered: