Repository navigation
Conversation
|
@caarlos0 hi! |
| fieldParams.HasDefaultValue, | ||
| opts.Environment, | ||
| ) | ||
| if _, ok := opts.Environment[fieldParams.Key]; ok && val != "" { |
There was a problem hiding this comment.
I think we can just directly check isDefault here.
caarlos0
left a comment
There was a problem hiding this comment.
Bot review by GitHub Copilot. Requesting changes for the regressions noted inline. Both target regressions were reproduced; base behavior was confirmed by source inspection, not execution. No local build was run.
| if _, ok := opts.Environment[fieldParams.Key]; ok && val != "" { | ||
| fromEnv = true |
There was a problem hiding this comment.
[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 value != "" && (!opts.SetDefaultsForZeroValuesOnly || refField.IsZero()) { | ||
| if fromEnv || (value != "" && (!opts.SetDefaultsForZeroValuesOnly || refField.IsZero())) { |
There was a problem hiding this comment.
[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.
Fixes #364
What this does
even when SetDefaultsForZeroValuesOnly is true.
Why
The current logic prevents environment values from being applied
when the struct field is non-zero, which is not the intended behavior.