-
-
Notifications
You must be signed in to change notification settings - Fork 271
fix: override variables in struct by non-default values #366
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?
Conversation
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would return a struct then
| 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) { |
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.
Done
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.
|
|
||
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
#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.