mirror of https://github.com/spf13/viper.git
Fixed #115: Added code in the find method to search for nested configuration parameters
This commit is contained in:
parent
6a665317fd
commit
87b94ba486
14
viper.go
14
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)
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue