mirror of
https://github.com/spf13/viper
synced 2024-12-23 03:57:01 +00:00
remove parents to use env prefix instead to avoid mixing both concepts
This commit is contained in:
parent
b77f4c1928
commit
c7097bbd11
2 changed files with 4 additions and 10 deletions
8
viper.go
8
viper.go
|
@ -205,7 +205,6 @@ type Viper struct {
|
||||||
envKeyReplacer StringReplacer
|
envKeyReplacer StringReplacer
|
||||||
allowEmptyEnv bool
|
allowEmptyEnv bool
|
||||||
|
|
||||||
parents []string
|
|
||||||
config map[string]interface{}
|
config map[string]interface{}
|
||||||
override map[string]interface{}
|
override map[string]interface{}
|
||||||
defaults map[string]interface{}
|
defaults map[string]interface{}
|
||||||
|
@ -232,7 +231,6 @@ func New() *Viper {
|
||||||
v.configPermissions = os.FileMode(0o644)
|
v.configPermissions = os.FileMode(0o644)
|
||||||
v.fs = afero.NewOsFs()
|
v.fs = afero.NewOsFs()
|
||||||
v.config = make(map[string]interface{})
|
v.config = make(map[string]interface{})
|
||||||
v.parents = []string{}
|
|
||||||
v.override = make(map[string]interface{})
|
v.override = make(map[string]interface{})
|
||||||
v.defaults = make(map[string]interface{})
|
v.defaults = make(map[string]interface{})
|
||||||
v.kvstore = make(map[string]interface{})
|
v.kvstore = make(map[string]interface{})
|
||||||
|
@ -956,9 +954,8 @@ 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.automaticEnvApplied = v.automaticEnvApplied
|
subv.automaticEnvApplied = v.automaticEnvApplied
|
||||||
subv.envPrefix = v.envPrefix
|
subv.envPrefix = v.mergeWithEnvPrefix(key)
|
||||||
subv.envKeyReplacer = v.envKeyReplacer
|
subv.envKeyReplacer = v.envKeyReplacer
|
||||||
subv.config = cast.ToStringMap(data)
|
subv.config = cast.ToStringMap(data)
|
||||||
return subv
|
return subv
|
||||||
|
@ -1307,10 +1304,9 @@ func (v *Viper) find(lcaseKey string, flagDefault bool) interface{} {
|
||||||
|
|
||||||
// Env override next
|
// Env override next
|
||||||
if v.automaticEnvApplied {
|
if v.automaticEnvApplied {
|
||||||
envKey := strings.Join(append(v.parents, lcaseKey), ".")
|
|
||||||
// even if it hasn't been registered, if automaticEnv is used,
|
// even if it hasn't been registered, if automaticEnv is used,
|
||||||
// check any Get request
|
// check any Get request
|
||||||
if val, ok := v.getEnv(v.mergeWithEnvPrefix(envKey)); ok {
|
if val, ok := v.getEnv(v.mergeWithEnvPrefix(lcaseKey)); ok {
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
if nested && v.isPathShadowedInAutoEnv(path) != "" {
|
if nested && v.isPathShadowedInAutoEnv(path) != "" {
|
||||||
|
|
|
@ -1604,12 +1604,10 @@ func TestSub(t *testing.T) {
|
||||||
assert.Equal(t, (*Viper)(nil), subv)
|
assert.Equal(t, (*Viper)(nil), subv)
|
||||||
|
|
||||||
subv = v.Sub("clothing")
|
subv = v.Sub("clothing")
|
||||||
assert.Equal(t, subv.parents[0], "clothing")
|
assert.Equal(t, subv.envPrefix, "CLOTHING")
|
||||||
|
|
||||||
subv = v.Sub("clothing").Sub("pants")
|
subv = v.Sub("clothing").Sub("pants")
|
||||||
assert.Equal(t, len(subv.parents), 2)
|
assert.Equal(t, subv.envPrefix, "CLOTHING_PANTS")
|
||||||
assert.Equal(t, subv.parents[0], "clothing")
|
|
||||||
assert.Equal(t, subv.parents[1], "pants")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var hclWriteExpected = []byte(`"foos" = {
|
var hclWriteExpected = []byte(`"foos" = {
|
||||||
|
|
Loading…
Reference in a new issue