Skip to content

Commit c7097bb

Browse files
committed
remove parents to use env prefix instead to avoid mixing both concepts
1 parent b77f4c1 commit c7097bb

File tree

2 files changed

+4
-10
lines changed

2 files changed

+4
-10
lines changed

viper.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ type Viper struct {
205205
envKeyReplacer StringReplacer
206206
allowEmptyEnv bool
207207

208-
parents []string
209208
config map[string]interface{}
210209
override map[string]interface{}
211210
defaults map[string]interface{}
@@ -232,7 +231,6 @@ func New() *Viper {
232231
v.configPermissions = os.FileMode(0o644)
233232
v.fs = afero.NewOsFs()
234233
v.config = make(map[string]interface{})
235-
v.parents = []string{}
236234
v.override = make(map[string]interface{})
237235
v.defaults = make(map[string]interface{})
238236
v.kvstore = make(map[string]interface{})
@@ -956,9 +954,8 @@ func (v *Viper) Sub(key string) *Viper {
956954
}
957955

958956
if reflect.TypeOf(data).Kind() == reflect.Map {
959-
subv.parents = append(v.parents, strings.ToLower(key))
960957
subv.automaticEnvApplied = v.automaticEnvApplied
961-
subv.envPrefix = v.envPrefix
958+
subv.envPrefix = v.mergeWithEnvPrefix(key)
962959
subv.envKeyReplacer = v.envKeyReplacer
963960
subv.config = cast.ToStringMap(data)
964961
return subv
@@ -1307,10 +1304,9 @@ func (v *Viper) find(lcaseKey string, flagDefault bool) interface{} {
13071304

13081305
// Env override next
13091306
if v.automaticEnvApplied {
1310-
envKey := strings.Join(append(v.parents, lcaseKey), ".")
13111307
// even if it hasn't been registered, if automaticEnv is used,
13121308
// check any Get request
1313-
if val, ok := v.getEnv(v.mergeWithEnvPrefix(envKey)); ok {
1309+
if val, ok := v.getEnv(v.mergeWithEnvPrefix(lcaseKey)); ok {
13141310
return val
13151311
}
13161312
if nested && v.isPathShadowedInAutoEnv(path) != "" {

viper_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -1604,12 +1604,10 @@ func TestSub(t *testing.T) {
16041604
assert.Equal(t, (*Viper)(nil), subv)
16051605

16061606
subv = v.Sub("clothing")
1607-
assert.Equal(t, subv.parents[0], "clothing")
1607+
assert.Equal(t, subv.envPrefix, "CLOTHING")
16081608

16091609
subv = v.Sub("clothing").Sub("pants")
1610-
assert.Equal(t, len(subv.parents), 2)
1611-
assert.Equal(t, subv.parents[0], "clothing")
1612-
assert.Equal(t, subv.parents[1], "pants")
1610+
assert.Equal(t, subv.envPrefix, "CLOTHING_PANTS")
16131611
}
16141612

16151613
var hclWriteExpected = []byte(`"foos" = {

0 commit comments

Comments
 (0)