Skip to content

Commit

Permalink
fix: made InConfig process paths correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
VOvchinnikov authored and sagikazarmark committed Sep 16, 2021
1 parent 2062cd6 commit e606f74
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 5 additions & 3 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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"))
Expand Down

0 comments on commit e606f74

Please sign in to comment.