This commit is contained in:
Jeremy L 2023-09-25 15:35:56 +03:00 committed by GitHub
commit 42c5faccb7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 10 deletions

View file

@ -206,7 +206,6 @@ type Viper struct {
envKeyReplacer StringReplacer
allowEmptyEnv bool
parents []string
config map[string]interface{}
override map[string]interface{}
defaults map[string]interface{}
@ -233,7 +232,6 @@ func New() *Viper {
v.configPermissions = os.FileMode(0o644)
v.fs = afero.NewOsFs()
v.config = make(map[string]interface{})
v.parents = []string{}
v.override = make(map[string]interface{})
v.defaults = make(map[string]interface{})
v.kvstore = make(map[string]interface{})
@ -957,9 +955,8 @@ 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.envPrefix = v.mergeWithEnvPrefix(key)
subv.envKeyReplacer = v.envKeyReplacer
subv.config = cast.ToStringMap(data)
return subv
@ -1308,10 +1305,9 @@ func (v *Viper) find(lcaseKey string, flagDefault bool) interface{} {
// Env override next
if v.automaticEnvApplied {
envKey := strings.Join(append(v.parents, lcaseKey), ".")
// even if it hasn't been registered, if automaticEnv is used,
// check any Get request
if val, ok := v.getEnv(v.mergeWithEnvPrefix(envKey)); ok {
if val, ok := v.getEnv(v.mergeWithEnvPrefix(lcaseKey)); ok {
return val
}
if nested && v.isPathShadowedInAutoEnv(path) != "" {

View file

@ -1583,12 +1583,10 @@ func TestSub(t *testing.T) {
assert.Equal(t, (*Viper)(nil), subv)
subv = v.Sub("clothing")
assert.Equal(t, subv.parents[0], "clothing")
assert.Equal(t, subv.envPrefix, "CLOTHING")
subv = v.Sub("clothing").Sub("pants")
assert.Equal(t, len(subv.parents), 2)
assert.Equal(t, subv.parents[0], "clothing")
assert.Equal(t, subv.parents[1], "pants")
assert.Equal(t, subv.envPrefix, "CLOTHING_PANTS")
}
var hclWriteExpected = []byte(`"foos" = {