Repository navigation
fix: allow env var to override non-zero struct values (#364) #365
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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())) { | ||
| return set(refField, refTypeField, value, opts.FuncMap) | ||
| } | ||
|
|
||
|
|
@@ -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( | ||
|
|
@@ -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 != "" { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Do not mark a fallback default as an environment value With |
||
| } | ||
|
|
||
| if fieldParams.Expand { | ||
| val = os.Expand(val, opts.getRawEnv) | ||
|
|
@@ -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) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -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. | ||
|
|
||
There was a problem hiding this comment.
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"andEnvironment: map[string]string{"PORT": "${MISSING}"}, expansion resolves to an empty string, butfromEnvremains true and this calls the integer parser with"", producingstrconv.ParseInt: parsing "": invalid syntax. Loading an empty readable file through an integer,filefield has the same regression. Both occur withSetDefaultsForZeroValuesOnlydisabled as well as enabled; the base skips empty resolved values and preserves the initialized field. Keepvalue != ""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.