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.
Solves kelseyhightower#169
Adds support for slices of structs, useful for configurations in more complex apps, like sets of database replicas, etc.
This is something we've been performing with ad-hoc processing in our company's services and I decided to make a standard implementation here.
The configuration is done by defining variables like
MYAPP_SLICE_0_FOO
,MYAPP_SLICE_1_FOO
, etc.I had to cross some semantical boundaries, and
gatherInfo
now has to look into the actual defined variables to build the infos for those slice indexes that are defined. I think there's no other solution here.By doing that, very config parsing is now reading all the environment variables available, and thus we can get rid of the
os.Lookupenv
call.Usage also prints these variables, rendering them with a fake index
[N]
, likeMYAPP_SLICE_[N]_FOO
.I reformatted all the examples in the readme with
goimports
, otherwise it was hard to maintain them formatted (they were indented with spaces)Next step would be implementing also maps to structs, i.e., same thing but with simple strings as indexes, I didn't want to mix it here, plus it has some issues like not being able to support
map[string]struct
and having to define fields asmap[string]*struct
because map values are not addressable and won't be fillable with current approach, apart from obvious restrictinons on keys, which couldn't contain underscores, etc.