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:
Miguel Eduardo Gil Biraud 2017-04-10 11:26:50 +02:00 committed by Bjørn Erik Pedersen
parent 84f94806c6
commit 5d46e70da8
2 changed files with 38 additions and 1 deletions

View file

@ -719,7 +719,15 @@ func (v *Viper) GetSizeInBytes(key string) uint {
// UnmarshalKey takes a single key and unmarshals it into a Struct.
func UnmarshalKey(key string, rawVal interface{}) error { return v.UnmarshalKey(key, rawVal) }
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

View file

@ -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) {
initConfig(typ, config)
Set("RfD", true)