mirror of https://github.com/spf13/viper.git
Fix UnmarshalKey handling of time.Duration
* Failing test for key unmarshaling with nested structs containing time.Duration * Fix UnmarshalKey to use of defaultDecoderConfig
This commit is contained in:
parent
84f94806c6
commit
5d46e70da8
10
viper.go
10
viper.go
|
@ -719,7 +719,15 @@ func (v *Viper) GetSizeInBytes(key string) uint {
|
||||||
// UnmarshalKey takes a single key and unmarshals it into a Struct.
|
// UnmarshalKey takes a single key and unmarshals it into a Struct.
|
||||||
func UnmarshalKey(key string, rawVal interface{}) error { return v.UnmarshalKey(key, rawVal) }
|
func UnmarshalKey(key string, rawVal interface{}) error { return v.UnmarshalKey(key, rawVal) }
|
||||||
func (v *Viper) UnmarshalKey(key string, rawVal interface{}) error {
|
func (v *Viper) UnmarshalKey(key string, rawVal interface{}) error {
|
||||||
return mapstructure.Decode(v.Get(key), rawVal)
|
err := decode(v.Get(key), defaultDecoderConfig(rawVal))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
v.insensitiviseMaps()
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshal unmarshals the config into a Struct. Make sure that the tags
|
// Unmarshal unmarshals the config into a Struct. Make sure that the tags
|
||||||
|
|
|
@ -1102,6 +1102,35 @@ func TestCaseInsensitiveSet(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseNested(t *testing.T) {
|
||||||
|
type duration struct {
|
||||||
|
Delay time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
type item struct {
|
||||||
|
Name string
|
||||||
|
Delay time.Duration
|
||||||
|
Nested duration
|
||||||
|
}
|
||||||
|
|
||||||
|
config := `[[parent]]
|
||||||
|
delay="100ms"
|
||||||
|
[parent.nested]
|
||||||
|
delay="200ms"
|
||||||
|
`
|
||||||
|
initConfig("toml", config)
|
||||||
|
|
||||||
|
var items []item
|
||||||
|
err := v.UnmarshalKey("parent", &items)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to decode into struct, %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, 1, len(items))
|
||||||
|
assert.Equal(t, 100*time.Millisecond, items[0].Delay)
|
||||||
|
assert.Equal(t, 200*time.Millisecond, items[0].Nested.Delay)
|
||||||
|
}
|
||||||
|
|
||||||
func doTestCaseInsensitive(t *testing.T, typ, config string) {
|
func doTestCaseInsensitive(t *testing.T, typ, config string) {
|
||||||
initConfig(typ, config)
|
initConfig(typ, config)
|
||||||
Set("RfD", true)
|
Set("RfD", true)
|
||||||
|
|
Loading…
Reference in New Issue