Skip to content
Open
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
17 changes: 10 additions & 7 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,12 +502,12 @@ func doParseSlice(ref reflect.Value, processField processFieldFn, opts Options)
}

func setField(refField reflect.Value, refTypeField reflect.StructField, opts Options, fieldParams FieldParams) error {
value, err := get(fieldParams, opts)
value, fromEnv, err := get(fieldParams, opts)
if err != nil {
return err
}

if value != "" && (!opts.SetDefaultsForZeroValuesOnly || refField.IsZero()) {
if fromEnv || (value != "" && (!opts.SetDefaultsForZeroValuesOnly || refField.IsZero())) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

[P2] Preserve the final nonempty-value guard

For an initialized integer field tagged env:"PORT,expand" and Environment: map[string]string{"PORT": "${MISSING}"}, expansion resolves to an empty string, but fromEnv remains true and this calls the integer parser with "", producing strconv.ParseInt: parsing "": invalid syntax. Loading an empty readable file through an integer ,file field has the same regression. Both occur with SetDefaultsForZeroValuesOnly disabled as well as enabled; the base skips empty resolved values and preserves the initialized field. Keep value != "" outside the override condition: value != "" && (fromEnv || !opts.SetDefaultsForZeroValuesOnly || refField.IsZero()). Add focused empty-expansion and empty-file tests under both option settings, asserting no error and preservation of the initialized integer.

return set(refField, refTypeField, value, opts.FuncMap)
}

Expand Down Expand Up @@ -593,7 +593,7 @@ func parseFieldParams(field reflect.StructField, opts Options) (FieldParams, err
return result, nil
}

func get(fieldParams FieldParams, opts Options) (val string, err error) {
func get(fieldParams FieldParams, opts Options) (val string, fromEnv bool, err error) {
var exists, isDefault bool

val, exists, isDefault = getOr(
Expand All @@ -602,6 +602,9 @@ func get(fieldParams FieldParams, opts Options) (val string, err error) {
fieldParams.HasDefaultValue,
opts.Environment,
)
if _, ok := opts.Environment[fieldParams.Key]; ok && val != "" {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think we can just directly check isDefault here.

fromEnv = true
Comment on lines +605 to +606

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

[P2] Do not mark a fallback default as an environment value

With Username initialized to "root", tags env:"USERNAME" envDefault:"admin", Environment: map[string]string{"USERNAME": ""}, and SetDefaultsForZeroValuesOnly: true, this now replaces "root" with "admin". getOr selects the default and returns isDefault=true, but this check sees the existing key and the nonempty selected default, sets fromEnv=true, and bypasses default preservation. Derive provenance from the existing exists/isDefault results rather than key presence alone. Add a regression test for an explicitly empty environment variable asserting no error and preservation of the initialized value.

}

if fieldParams.Expand {
val = os.Expand(val, opts.getRawEnv)
Expand All @@ -614,18 +617,18 @@ func get(fieldParams FieldParams, opts Options) (val string, err error) {
}

if fieldParams.Required && !exists && fieldParams.OwnKey != "" {
return "", newVarIsNotSetError(fieldParams.Key)
return "", false, newVarIsNotSetError(fieldParams.Key)
}

if fieldParams.NotEmpty && val == "" {
return "", newEmptyVarError(fieldParams.Key)
return "", false, newEmptyVarError(fieldParams.Key)
}

if fieldParams.LoadFile && val != "" {
filename := val
val, err = getFromFile(filename)
if err != nil {
return "", newLoadFileContentError(filename, fieldParams.Key, err)
return "", false, newLoadFileContentError(filename, fieldParams.Key, err)
}
}

Expand All @@ -634,7 +637,7 @@ func get(fieldParams FieldParams, opts Options) (val string, err error) {
opts.OnSet(fieldParams.Key, val, isDefault)
}
}
return val, err
return val, fromEnv, err
}

// split the env tag's key into the expected key and desired option, if any.
Expand Down
15 changes: 15 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2411,3 +2411,18 @@ func TestEnvBleed(t *testing.T) {
isEqual(t, "", cfg.Foo)
})
}

func TestParse_Bug364EnvVarOverridesStructValue(t *testing.T) {
t.Setenv("USERNAME", "user1")

type cfg struct {
Username string `env:"USERNAME" envDefault:"admin"`
}
c := cfg{Username: "root"}

isNoErr(t, ParseWithOptions(&c, Options{
SetDefaultsForZeroValuesOnly: true,
}))

isEqual(t, c.Username, "user1")
}