kelseyhightower / envconfig

Golang library for managing configuration data from environment variables
MIT License
5.01k stars 377 forks source link

Support for slice of structs #170

Closed colega closed 2 years ago

colega commented 4 years ago

Solves https://github.com/kelseyhightower/envconfig/issues/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.

mohammadne commented 2 years ago

any progress ?

colega commented 2 years ago

Closing this since the repository seems unmaintained. You can find this change in my fork if needed.