From e606f7496e1ea147c5af6c0140c82010432420b8 Mon Sep 17 00:00:00 2001 From: Vasily Ovchinnikov Date: Tue, 1 Jun 2021 16:13:11 +0200 Subject: [PATCH] fix: made `InConfig` process paths correctly --- viper.go | 10 ++++++---- viper_test.go | 4 ++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/viper.go b/viper.go index 46b1a85..d7a3fad 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 { - // if the requested key is an alias, then return the proper key - key = v.realKey(key) + lcaseKey := strings.ToLower(key) - _, exists := v.config[key] - return exists + // if the requested key is an alias, then return the proper key + lcaseKey = v.realKey(lcaseKey) + path := strings.Split(lcaseKey, v.keyDelim) + + 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 4192748..080d0c4 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"))