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

Slice of structs #1

Merged
merged 6 commits into from
Apr 27, 2020
Merged

Slice of structs #1

merged 6 commits into from
Apr 27, 2020

Conversation

colega
Copy link
Owner

@colega colega commented Apr 27, 2020

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], like MYAPP_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 as map[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.

colega and others added 5 commits April 27, 2020 12:28
In order to implement slices of structs, we need to know all the
environment variables with a given pattern, and for that we need to read
them all. So let's start using that information for the current parsing
and make the future changes smaller.
Minor formatting change
Solves kelseyhightower#169

Enables population of a slice of structs, in the most expected way:

    MYAPP_SLICE_0_FOO=foo
    MYAPP_SLICE_0_BAR=bar
    MYAPP_SLICE_1_FOO=foo
    MYAPP_SLICE_1_BAR=bar

Would fill a slice of []struct{Foo, Bar string}

Usage printout also supports this slices, printing them as a single

    MYAPP_SLICE_[N]_FOO
    MYAPP_SLICE_[N]_BAR

Variables to be filled, which should be understood as slices with
numerical indexes.
Copy the provided spec before messing with it for the usage
@colega colega force-pushed the slice-of-structs branch from 6ffbc9e to 5a34a36 Compare April 27, 2020 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant