mirror of
https://github.com/spf13/viper
synced 2024-12-23 12:07:02 +00:00
Added dep search to InConfig method
This commit is contained in:
parent
71509d2887
commit
6d59e31dc5
2 changed files with 17 additions and 2 deletions
8
viper.go
8
viper.go
|
@ -1195,9 +1195,13 @@ func (v *Viper) realKey(key string) string {
|
||||||
func InConfig(key string) bool { return v.InConfig(key) }
|
func InConfig(key string) bool { return v.InConfig(key) }
|
||||||
func (v *Viper) InConfig(key string) bool {
|
func (v *Viper) InConfig(key string) bool {
|
||||||
// if the requested key is an alias, then return the proper key
|
// if the requested key is an alias, then return the proper key
|
||||||
key = v.realKey(key)
|
key = v.realKey(strings.ToLower(key))
|
||||||
|
|
||||||
_, exists := v.config[key]
|
path := strings.Split(key, v.keyDelim)
|
||||||
|
lastKey := strings.ToLower(path[len(path)-1])
|
||||||
|
deepestMap := deepSearch(v.config, path[0:len(path)-1])
|
||||||
|
|
||||||
|
_, exists := deepestMap[lastKey]
|
||||||
return exists
|
return exists
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1031,6 +1031,17 @@ func TestIsSet(t *testing.T) {
|
||||||
assert.True(t, v.IsSet("helloworld"))
|
assert.True(t, v.IsSet("helloworld"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInConfig(t *testing.T) {
|
||||||
|
v := New()
|
||||||
|
v.SetConfigType("yaml")
|
||||||
|
v.ReadConfig(bytes.NewBuffer(yamlExample))
|
||||||
|
assert.True(t, v.InConfig("clothing.jacket"))
|
||||||
|
assert.False(t, v.InConfig("clothing.jackets"))
|
||||||
|
assert.False(t, v.InConfig("helloworld"))
|
||||||
|
v.Set("helloworld", "fubar")
|
||||||
|
assert.False(t, v.InConfig("helloworld"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestDirsSearch(t *testing.T) {
|
func TestDirsSearch(t *testing.T) {
|
||||||
|
|
||||||
root, config, cleanup := initDirs(t)
|
root, config, cleanup := initDirs(t)
|
||||||
|
|
Loading…
Reference in a new issue