mirror of
https://github.com/spf13/viper
synced 2024-12-23 03:57:01 +00:00
Prevent shadowning of keys when a nested key's value is nil.
This commit is contained in:
parent
c975dc1b4e
commit
03d0bb47ee
2 changed files with 13 additions and 2 deletions
2
viper.go
2
viper.go
|
@ -788,11 +788,13 @@ func (v *Viper) find(key string) interface{} {
|
||||||
if source != nil {
|
if source != nil {
|
||||||
if reflect.TypeOf(source).Kind() == reflect.Map {
|
if reflect.TypeOf(source).Kind() == reflect.Map {
|
||||||
val := v.searchMap(cast.ToStringMap(source), path[1:])
|
val := v.searchMap(cast.ToStringMap(source), path[1:])
|
||||||
|
if val != nil {
|
||||||
jww.TRACE.Println(key, "found in nested config:", val)
|
jww.TRACE.Println(key, "found in nested config:", val)
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val, exists = v.kvstore[key]
|
val, exists = v.kvstore[key]
|
||||||
if exists {
|
if exists {
|
||||||
|
|
|
@ -883,3 +883,12 @@ func TestUnmarshalingWithAliases(t *testing.T) {
|
||||||
|
|
||||||
assert.Equal(t, &C, &config{Id: 1, FirstName: "Steve", Surname: "Owen"})
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue