forked from mirror/viper
Prevent shadowning of keys when a nested key's value is nil.
This commit is contained in:
parent
16990631d4
commit
2f6a41490b
6
viper.go
6
viper.go
|
@ -802,8 +802,10 @@ 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:])
|
||||||
jww.TRACE.Println(key, "found in nested config:", val)
|
if val != nil {
|
||||||
return val
|
jww.TRACE.Println(key, "found in nested config:", val)
|
||||||
|
return val
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -907,3 +907,12 @@ func TestSetConfigNameClearsFileCache(t *testing.T) {
|
||||||
SetConfigName("default")
|
SetConfigName("default")
|
||||||
assert.Empty(t, v.getConfigFile())
|
assert.Empty(t, v.getConfigFile())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 New Issue