From 87b94ba48604b6be709dc242d0261f3723384b74 Mon Sep 17 00:00:00 2001 From: James Sweet Date: Tue, 13 Oct 2015 18:31:32 -0400 Subject: [PATCH] Fixed #115: Added code in the find method to search for nested configuration parameters --- viper.go | 14 ++++++++++++++ viper_test.go | 9 +++++++++ 2 files changed, 23 insertions(+) diff --git a/viper.go b/viper.go index 5afed24..d9c5e67 100644 --- a/viper.go +++ b/viper.go @@ -657,6 +657,20 @@ func (v *Viper) find(key string) interface{} { return val } + // Test for nested config parameter + if strings.Contains(key, v.keyDelim) { + path := strings.Split(key, v.keyDelim) + + source := v.find(path[0]) + 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 + } + } + } + val, exists = v.kvstore[key] if exists { jww.TRACE.Println(key, "found in key/value store:", val) diff --git a/viper_test.go b/viper_test.go index 6b9db6e..d9aae00 100644 --- a/viper_test.go +++ b/viper_test.go @@ -199,6 +199,15 @@ func TestBasics(t *testing.T) { func TestDefault(t *testing.T) { SetDefault("age", 45) assert.Equal(t, 45, Get("age")) + + SetDefault("clothing.jacket", "slacks") + assert.Equal(t, "slacks", Get("clothing.jacket")) + + SetConfigType("yaml") + err := ReadConfig(bytes.NewBuffer(yamlExample)) + + assert.NoError(t, err) + assert.Equal(t, "leather", Get("clothing.jacket")) } func TestUnmarshalling(t *testing.T) {