mirror of
https://github.com/spf13/viper
synced 2024-11-16 18:07:02 +00:00
Add parallel functions/methods to unmarshal data and return mapstructure.Metadata
This was not added for UnmarshalExact, since all keys should be used and none left unused.
This commit is contained in:
parent
02eb49d4bd
commit
c254a5ba54
1 changed files with 42 additions and 0 deletions
42
viper.go
42
viper.go
|
@ -731,6 +731,27 @@ func (v *Viper) UnmarshalKey(key string, rawVal interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalKeyWithMeta performs UnmarshalKey and using an additional
|
||||
// parameter, also provides access to the mapstructure.Metadata.
|
||||
func UnmarshalKeyWithMeta(key string, rawVal interface{}) (mapstructure.Metadata, error) {
|
||||
return v.UnmarshalKeyWithMeta(key, rawVal)
|
||||
}
|
||||
func (v *Viper) UnmarshalKeyWithMeta(key string, rawVal interface{}) (mapstructure.Metadata, error) {
|
||||
var meta mapstructure.Metadata
|
||||
config := defaultDecoderConfig(rawVal)
|
||||
config.Metadata = &meta
|
||||
|
||||
err := decode(v.Get(key), config)
|
||||
|
||||
if err != nil {
|
||||
return meta, err
|
||||
}
|
||||
|
||||
v.insensitiviseMaps()
|
||||
|
||||
return meta, nil
|
||||
}
|
||||
|
||||
// Unmarshal unmarshals the config into a Struct. Make sure that the tags
|
||||
// on the fields of the structure are properly set.
|
||||
func Unmarshal(rawVal interface{}) error { return v.Unmarshal(rawVal) }
|
||||
|
@ -746,6 +767,27 @@ func (v *Viper) Unmarshal(rawVal interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalWithMeta performs Unmarshal and using an additional
|
||||
// parameter, also provides access to the mapstructure.Metadata.
|
||||
func UnmarshalWithMeta(rawVal interface{}) (mapstructure.Metadata, error) {
|
||||
return v.UnmarshalWithMeta(rawVal)
|
||||
}
|
||||
func (v *Viper) UnmarshalWithMeta(rawVal interface{}) (mapstructure.Metadata, error) {
|
||||
var meta mapstructure.Metadata
|
||||
config := defaultDecoderConfig(rawVal)
|
||||
config.Metadata = &meta
|
||||
|
||||
err := decode(v.AllSettings(), config)
|
||||
|
||||
if err != nil {
|
||||
return meta, err
|
||||
}
|
||||
|
||||
v.insensitiviseMaps()
|
||||
|
||||
return meta, nil
|
||||
}
|
||||
|
||||
// defaultDecoderConfig returns default mapsstructure.DecoderConfig with support
|
||||
// of time.Duration values
|
||||
func defaultDecoderConfig(output interface{}) *mapstructure.DecoderConfig {
|
||||
|
|
Loading…
Reference in a new issue