Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sub parent inheritance #1056

Merged
merged 2 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ type Viper struct {
envKeyReplacer StringReplacer
allowEmptyEnv bool

parents []string
config map[string]interface{}
override map[string]interface{}
defaults map[string]interface{}
Expand All @@ -232,6 +233,7 @@ 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{})
Expand Down Expand Up @@ -948,6 +950,10 @@ 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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

envPrefix must also be preserved

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise 1181: if val, ok := v.getEnv(v.mergeWithEnvPrefix(envKey)); ok { breaks

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in fd570e1 💯

subv.envPrefix = v.envPrefix
subv.envKeyReplacer = v.envKeyReplacer
subv.config = cast.ToStringMap(data)
return subv
}
Expand Down Expand Up @@ -1294,9 +1300,10 @@ 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(lcaseKey)); ok {
if val, ok := v.getEnv(v.mergeWithEnvPrefix(envKey)); ok {
return val
}
if nested && v.isPathShadowedInAutoEnv(path) != "" {
Expand Down
26 changes: 26 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,24 @@ func TestEnvKeyReplacer(t *testing.T) {
assert.Equal(t, "30s", v.Get("refresh-interval"))
}

func TestEnvSubConfig(t *testing.T) {
initYAML()

v.AutomaticEnv()

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) {
initConfigs()

Expand Down Expand Up @@ -1522,6 +1540,14 @@ func TestSub(t *testing.T) {

subv = v.Sub("missing.key")
assert.Equal(t, (*Viper)(nil), subv)

subv = v.Sub("clothing")
assert.Equal(t, subv.parents[0], "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")
}

var hclWriteExpected = []byte(`"foos" = {
Expand Down