Skip to content
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

enhancement: Expand with default values #285

Merged
merged 5 commits into from
Oct 27, 2023

Conversation

gabrielcipriano
Copy link
Contributor

@gabrielcipriano gabrielcipriano commented Oct 4, 2023

resolves #279 (and resolves #250 (?))

Approach:

To capture the default values when expanding envs, we need to somehow have access to the previously resolved variables.

So, the get() function needs an auxiliar data structure (map[string]string) that will keep track of the resolved raw strings. In that way, we can take advange of os.Expand(s string, mapping func(string) string) to provide our own mapping function that returns the resolved string value for a given key.

To avoid passing this auxiliar data structure as a parameter down the function calls, I added it as an unexported field of the Options struct, let's say, using opts as if it were a ctx.

I'm not really sure if it's the best way of doing this, any suggestions will be more than appreciated!

edit: typo

env.go Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Oct 26, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (13ac655) 100.00% compared to head (e2f4a5a) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #285   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         3           
  Lines          464       477   +13     
=========================================
+ Hits           464       477   +13     
Files Coverage Δ
env.go 100.00% <100.00%> (ø)

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

@caarlos0
Copy link
Owner

Made a small note only.

Could you also note this new feature somewhere in the readme?

Thanks for the PR 🙏

@gabrielcipriano
Copy link
Contributor Author

Made a small note only.

Could you also note this new feature somewhere in the readme?

Thanks for the PR 🙏

Sure!
I'll be updating the readme soon

@gabrielcipriano
Copy link
Contributor Author

gabrielcipriano commented Oct 27, 2023

I added an example covering common cases (using envDefault, and envDefault being overrided)

@caarlos0 caarlos0 merged commit 3f90e4a into caarlos0:main Oct 27, 2023
12 checks passed
@caarlos0
Copy link
Owner

amazing, thank you!

BorzdeG pushed a commit to BorzdeG/env that referenced this pull request Aug 24, 2024
* dx: getOr return redability

* feat: expand with defaults

* fix: rm unnecessary guard

* docs: update readme - expand with envdefault

* chore: rm readme extra spaces

---------

Co-authored-by: Gabriel F Cipriano <[email protected]>
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.

expand from default values Proposal: envExpand also expand from self
2 participants