Fixed #115: Added code in the find method to search for nested configuration parameters

This commit is contained in:
James Sweet 2015-10-13 18:31:32 -04:00
parent 1967d93db7
commit 3cb0b72a88
2 changed files with 23 additions and 0 deletions

View File

@ -657,6 +657,20 @@ func (v *Viper) find(key string) interface{} {
return val 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] val, exists = v.kvstore[key]
if exists { if exists {
jww.TRACE.Println(key, "found in key/value store:", val) jww.TRACE.Println(key, "found in key/value store:", val)

View File

@ -199,6 +199,15 @@ func TestBasics(t *testing.T) {
func TestDefault(t *testing.T) { func TestDefault(t *testing.T) {
SetDefault("age", 45) SetDefault("age", 45)
assert.Equal(t, 45, Get("age")) 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) { func TestUnmarshalling(t *testing.T) {