diff --git a/viper.go b/viper.go index 46b1a85ec..d7a3fadad 100644 --- a/viper.go +++ b/viper.go @@ -1404,11 +1404,13 @@ func (v *Viper) realKey(key string) string { func InConfig(key string) bool { return v.InConfig(key) } func (v *Viper) InConfig(key string) bool { + lcaseKey := strings.ToLower(key) + // if the requested key is an alias, then return the proper key - key = v.realKey(key) + lcaseKey = v.realKey(lcaseKey) + path := strings.Split(lcaseKey, v.keyDelim) - _, exists := v.config[key] - return exists + return v.searchIndexableWithPathPrefixes(v.config, path) != nil } // SetDefault sets the default value for this key. diff --git a/viper_test.go b/viper_test.go index 4192748cb..080d0c41a 100644 --- a/viper_test.go +++ b/viper_test.go @@ -382,7 +382,9 @@ func TestUnmarshaling(t *testing.T) { unmarshalReader(r, v.config) assert.True(t, InConfig("name")) + assert.True(t, InConfig("clothing.jacket")) assert.False(t, InConfig("state")) + assert.False(t, InConfig("clothing.hat")) assert.Equal(t, "steve", Get("name")) assert.Equal(t, []interface{}{"skateboarding", "snowboarding", "go"}, Get("hobbies")) assert.Equal(t, map[string]interface{}{"jacket": "leather", "trousers": "denim", "pants": map[string]interface{}{"size": "large"}}, Get("clothing")) @@ -1239,7 +1241,9 @@ func TestReadBufConfig(t *testing.T) { t.Log(v.AllKeys()) assert.True(t, v.InConfig("name")) + assert.True(t, v.InConfig("clothing.jacket")) assert.False(t, v.InConfig("state")) + assert.False(t, v.InConfig("clothing.hat")) assert.Equal(t, "steve", v.Get("name")) assert.Equal(t, []interface{}{"skateboarding", "snowboarding", "go"}, v.Get("hobbies")) assert.Equal(t, map[string]interface{}{"jacket": "leather", "trousers": "denim", "pants": map[string]interface{}{"size": "large"}}, v.Get("clothing"))