Skip to content

Options to set defaults for zero values only#325

Merged
caarlos0 merged 1 commit intocaarlos0:mainfrom
dnovikoff:set_default_for_zero_only
Feb 21, 2025
Merged

Options to set defaults for zero values only#325
caarlos0 merged 1 commit intocaarlos0:mainfrom
dnovikoff:set_default_for_zero_only

Conversation

@dnovikoff
Copy link
Copy Markdown
Contributor

I've encountered scenarious of loading config from file and then applying env vars.
Would be nice to ignore defaults for thouse values already set.

@dnovikoff dnovikoff force-pushed the set_default_for_zero_only branch 4 times, most recently from 6bb7931 to d7824e1 Compare August 24, 2024 16:43
@codecov
Copy link
Copy Markdown

codecov bot commented Aug 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (e55230b) to head (e8b732f).
Report is 11 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #325   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         3           
  Lines          626       622    -4     
=========================================
- Hits           626       622    -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dnovikoff dnovikoff force-pushed the set_default_for_zero_only branch 2 times, most recently from eacd4d3 to 4479add Compare October 14, 2024 09:15
@dnovikoff dnovikoff force-pushed the set_default_for_zero_only branch from 4479add to 1d07485 Compare October 22, 2024 13:01
Copy link
Copy Markdown
Owner

@caarlos0 caarlos0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, could you also add an example in example_test.go?

thanks!

@dnovikoff dnovikoff force-pushed the set_default_for_zero_only branch from 1d07485 to 58aa16b Compare February 20, 2025 20:13
@dnovikoff dnovikoff force-pushed the set_default_for_zero_only branch from 58aa16b to e8b732f Compare February 20, 2025 20:14
@dnovikoff
Copy link
Copy Markdown
Contributor Author

looks good, could you also add an example in example_test.go?

thanks!

I've added the example. Please check that it fits

@caarlos0
Copy link
Copy Markdown
Owner

looks good, thanks @dnovikoff

@caarlos0 caarlos0 merged commit 2be9960 into caarlos0:main Feb 21, 2025
17 checks passed
DmitriyMV added a commit to DmitriyMV/env that referenced this pull request Jul 21, 2025
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.
DmitriyMV added a commit to DmitriyMV/env that referenced this pull request Jul 21, 2025
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.
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