mirror of
https://github.com/spf13/viper
synced 2024-11-04 20:27:02 +00:00
Preserve envPrefix in Sub
This commit is contained in:
parent
3f4449054d
commit
3970ad177e
2 changed files with 8 additions and 2 deletions
1
viper.go
1
viper.go
|
@ -952,6 +952,7 @@ func (v *Viper) Sub(key string) *Viper {
|
||||||
if reflect.TypeOf(data).Kind() == reflect.Map {
|
if reflect.TypeOf(data).Kind() == reflect.Map {
|
||||||
subv.parents = append(v.parents, strings.ToLower(key))
|
subv.parents = append(v.parents, strings.ToLower(key))
|
||||||
subv.automaticEnvApplied = v.automaticEnvApplied
|
subv.automaticEnvApplied = v.automaticEnvApplied
|
||||||
|
subv.envPrefix = v.envPrefix
|
||||||
subv.envKeyReplacer = v.envKeyReplacer
|
subv.envKeyReplacer = v.envKeyReplacer
|
||||||
subv.config = cast.ToStringMap(data)
|
subv.config = cast.ToStringMap(data)
|
||||||
return subv
|
return subv
|
||||||
|
|
|
@ -738,12 +738,17 @@ func TestEnvSubConfig(t *testing.T) {
|
||||||
|
|
||||||
v.AutomaticEnv()
|
v.AutomaticEnv()
|
||||||
|
|
||||||
replacer := strings.NewReplacer(".", "_")
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||||
v.SetEnvKeyReplacer(replacer)
|
|
||||||
|
|
||||||
testutil.Setenv(t, "CLOTHING_PANTS_SIZE", "small")
|
testutil.Setenv(t, "CLOTHING_PANTS_SIZE", "small")
|
||||||
subv := v.Sub("clothing").Sub("pants")
|
subv := v.Sub("clothing").Sub("pants")
|
||||||
assert.Equal(t, "small", subv.Get("size"))
|
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) {
|
func TestAllKeys(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue