Skip to content

Commit

Permalink
Preserve envPrefix in Sub
Browse files Browse the repository at this point in the history
  • Loading branch information
TaylorOno committed Feb 19, 2023
1 parent 3357976 commit 2e4a65a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,7 @@ func (v *Viper) Sub(key string) *Viper {
if reflect.TypeOf(data).Kind() == reflect.Map {
subv.parents = append(v.parents, strings.ToLower(key))
subv.automaticEnvApplied = v.automaticEnvApplied
subv.envPrefix = v.envPrefix
subv.envKeyReplacer = v.envKeyReplacer
subv.config = cast.ToStringMap(data)
return subv
Expand Down
9 changes: 7 additions & 2 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,12 +738,17 @@ func TestEnvSubConfig(t *testing.T) {

v.AutomaticEnv()

replacer := strings.NewReplacer(".", "_")
v.SetEnvKeyReplacer(replacer)
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))

testutil.Setenv(t, "CLOTHING_PANTS_SIZE", "small")
subv := v.Sub("clothing").Sub("pants")
assert.Equal(t, "small", subv.Get("size"))

// again with EnvPrefix
v.SetEnvPrefix("foo") // will be uppercased automatically
subWithPrefix := v.Sub("clothing").Sub("pants")
testutil.Setenv(t, "FOO_CLOTHING_PANTS_SIZE", "large")
assert.Equal(t, "large", subWithPrefix.Get("size"))
}

func TestAllKeys(t *testing.T) {
Expand Down

0 comments on commit 2e4a65a

Please sign in to comment.