Prevent shadowning of keys when a nested key's value is nil.

This commit is contained in:
Alex Bice 2016-03-04 19:09:45 -07:00
parent c975dc1b4e
commit 03d0bb47ee
2 changed files with 13 additions and 2 deletions

View file

@ -788,8 +788,10 @@ func (v *Viper) find(key string) interface{} {
if source != nil {
if reflect.TypeOf(source).Kind() == reflect.Map {
val := v.searchMap(cast.ToStringMap(source), path[1:])
jww.TRACE.Println(key, "found in nested config:", val)
return val
if val != nil {
jww.TRACE.Println(key, "found in nested config:", val)
return val
}
}
}
}

View file

@ -883,3 +883,12 @@ func TestUnmarshalingWithAliases(t *testing.T) {
assert.Equal(t, &C, &config{Id: 1, FirstName: "Steve", Surname: "Owen"})
}
func TestShadowedNestedValue(t *testing.T) {
polyester := "polyester"
initYAML()
SetDefault("clothing.shirt", polyester)
assert.Equal(t, GetString("clothing.jacket"), "leather")
assert.Equal(t, GetString("clothing.shirt"), polyester)
}