This commit is contained in:
Sergey Novichkov 2021-09-15 20:30:50 -04:00 committed by GitHub
commit 40675c5fb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View file

@ -1405,9 +1405,13 @@ func InConfig(key string) bool { return v.InConfig(key) }
func (v *Viper) InConfig(key string) bool {
// 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
}

View file

@ -1293,6 +1293,17 @@ func TestIsSet(t *testing.T) {
assert.True(t, v.IsSet("barbaz"))
}
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) {
root, config, cleanup := initDirs(t)
defer cleanup()