From 03d0bb47ee79c51794eb9fff9fc7c55014273906 Mon Sep 17 00:00:00 2001 From: Alex Bice Date: Fri, 4 Mar 2016 19:09:45 -0700 Subject: [PATCH] Prevent shadowning of keys when a nested key's value is nil. --- viper.go | 6 ++++-- viper_test.go | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/viper.go b/viper.go index 7a49a0a..d701632 100644 --- a/viper.go +++ b/viper.go @@ -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 + } } } } diff --git a/viper_test.go b/viper_test.go index 858caff..8a6d535 100644 --- a/viper_test.go +++ b/viper_test.go @@ -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) +}