From 3970ad177ecc286c1e14b64c18449cb30f73d642 Mon Sep 17 00:00:00 2001 From: TaylorOno Date: Sun, 4 Apr 2021 13:16:36 -0700 Subject: [PATCH] Preserve envPrefix in Sub --- viper.go | 1 + viper_test.go | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/viper.go b/viper.go index 34f4273..fa6f3e3 100644 --- a/viper.go +++ b/viper.go @@ -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 diff --git a/viper_test.go b/viper_test.go index db5dfd9..b867337 100644 --- a/viper_test.go +++ b/viper_test.go @@ -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) {