Skip to content

Commit 6daeb06

Browse files
fix: use Key instead of OwnKey in rawEnvVars to properly reference existing defaults in complex structs (#412)
1 parent 33faf6f commit 6daeb06

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ func get(fieldParams FieldParams, opts Options) (val string, err error) {
604604
val = os.Expand(val, opts.getRawEnv)
605605
}
606606

607-
opts.rawEnvVars[fieldParams.OwnKey] = val
607+
opts.rawEnvVars[fieldParams.Key] = val
608608

609609
if fieldParams.Unset {
610610
defer os.Unsetenv(fieldParams.Key)

env_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,12 @@ func TestParseExpandOption(t *testing.T) {
991991
ExpandKey string `env:"EXPAND_KEY"`
992992
CompoundKey string `env:"HOST_PORT,expand" envDefault:"${HOST}:${PORT}"`
993993
Default string `env:"DEFAULT,expand" envDefault:"def1"`
994+
Component struct {
995+
Port int `env:"PORT" envDefault:"5000"`
996+
Internal struct {
997+
Host string `env:"INTERNAL_HOST,expand" envDefault:"${HOST}:${COMPONENT_PORT}"`
998+
}
999+
} `envPrefix:"COMPONENT_"`
9941000
}
9951001

9961002
t.Setenv("HOST", "localhost")
@@ -1008,6 +1014,7 @@ func TestParseExpandOption(t *testing.T) {
10081014
isEqual(t, "qwerty12345", cfg.ExpandKey)
10091015
isEqual(t, "localhost:3000", cfg.CompoundKey)
10101016
isEqual(t, "def1", cfg.Default)
1017+
isEqual(t, "localhost:5000", cfg.Component.Internal.Host)
10111018
}
10121019

10131020
func TestParseExpandWithDefaultOption(t *testing.T) {

0 commit comments

Comments
 (0)