Skip to content

Conversation

@DmitriyMV
Copy link

#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.

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) {

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

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) {

Copy link
Author

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.
Comment on lines -599 to 613

val, exists, isDefault = getOr(
result.value, exists, result.isDefault = getOr(
fieldParams.Key,
fieldParams.DefaultValue,
fieldParams.HasDefaultValue,
opts.Environment,
)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants