mirror of
https://github.com/spf13/viper
synced 2024-12-22 19:47:01 +00:00
Fixed #115: Added code in the find method to search for nested configuration parameters
This commit is contained in:
parent
6a665317fd
commit
87b94ba486
2 changed files with 23 additions and 0 deletions
14
viper.go
14
viper.go
|
@ -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)
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
Loading…
Reference in a new issue