fix: override variables in struct by non-default values#366
Open
DmitriyMV wants to merge 1 commit intocaarlos0:mainfrom
Open
fix: override variables in struct by non-default values#366DmitriyMV wants to merge 1 commit intocaarlos0:mainfrom
DmitriyMV wants to merge 1 commit intocaarlos0:mainfrom
Conversation
ccoVeille
reviewed
Jul 21, 2025
env.go
Outdated
|
|
||
| func get(fieldParams FieldParams, opts Options) (val string, err error) { | ||
| var exists, isDefault bool | ||
| func get(fieldParams FieldParams, opts Options) (val string, isDefault bool, err error) { |
There was a problem hiding this comment.
I would return a struct then
Suggested change
| func get(fieldParams FieldParams, opts Options) (val string, isDefault bool, err error) { | |
| type whatever struct { | |
| val string | |
| isDefault bool | |
| } | |
| func get(fieldParams FieldParams, opts Options) (whatever, err error) { |
caarlos0#325 addressed the problem with `Parse` functions changing already set fields with default values, but created a problem when you can't change already set fields with non-default values. For example this works correctly: 1. Initialize struct with default values in tags and set struct fields explicitly. 2. Loading empty config/empty env using Parse. 3. Struct fields will remain the same if they were set during step 1. But this does not: 1. Initialize struct with default values in tags and set struct fields explicitly. 2. Loading non-empty config or non-empty env using Parse. 3. Struct fields will remain the same if they were set during step 1 but should get the non-empty values from the step 2. This PR fixes that.
ccoVeille
reviewed
Jul 21, 2025
Comment on lines
599
to
613
|
|
||
| val, exists, isDefault = getOr( | ||
| result.value, exists, result.isDefault = getOr( | ||
| fieldParams.Key, | ||
| fieldParams.DefaultValue, | ||
| fieldParams.HasDefaultValue, | ||
| opts.Environment, | ||
| ) |
There was a problem hiding this comment.
Maybe exists could be a field in the struct and getOr would simply return a gotValue.
Note: I'm not a maintainer, just a random reviewer. Maybe it's worth for someone else feedback
Author
|
Any chance of this being merged? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#325 addressed the problem with
Parsefunctions changing already set fields with default values, but created a problem when you can't change already set fields with non-default values.For example this works correctly:
But this does not:
This PR fixes that.