Prevent shadowning of keys when a nested key's value is nil. - viper - [fork] go viper port for 9front
(HTM) git clone git@git.drkhsh.at/viper.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 2f6a41490bc86fa9a8fb2fc03526a5795e904a17
(DIR) parent 16990631d4aa7e38f73dbbbf37fa13e67c648531
(HTM) Author: Max Wolter <max.wolter@etixgroup.com>
Date: Mon, 19 Sep 2016 19:37:56 +0200
Prevent shadowning of keys when a nested key's value is nil.
Diffstat:
M viper.go | 6 ++++--
M viper_test.go | 9 +++++++++
2 files changed, 13 insertions(+), 2 deletions(-)
---
(DIR) diff --git a/viper.go b/viper.go
@@ -802,8 +802,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
+ }
}
}
}
(DIR) diff --git a/viper_test.go b/viper_test.go
@@ -907,3 +907,12 @@ func TestSetConfigNameClearsFileCache(t *testing.T) {
SetConfigName("default")
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)
+}