enhancement: Expand with default values#285
Merged
caarlos0 merged 5 commits intocaarlos0:mainfrom Oct 27, 2023
Merged
Conversation
gabrielcipriano
commented
Oct 4, 2023
Codecov ReportAll modified and coverable lines are covered by tests ✅
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
☔ View full report in Codecov by Sentry. |
Owner
|
Made a small note only. Could you also note this new feature somewhere in the readme? Thanks for the PR 🙏 |
Contributor
Author
Sure! |
Contributor
Author
|
I added an example covering common cases (using envDefault, and envDefault being overrided) |
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 <gabriel.cipriano@farme.com.br>
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.
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 ofos.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
optsas if it were actx.I'm not really sure if it's the best way of doing this, any suggestions will be more than appreciated!
edit: typo