flatten defaults to ensure env var load; expand flat settings to ensure override precedence [Fixes #71] [Fixes #160] #164
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.
There are some issues with nested settings precedence, due to some settings arriving from a flat map (flags, env vars) and some settings arriving from a nested map (existing structs, config files).
Right now
AutomaticEnv
only notices env vars if the flat path has been declared as a key. The best place to ensure a key is declared is in the defaults. Therefore we create a flatmap when setting the defaults.However, if any flat keys are unexpanded when
Unmarshal
ing to a Struct or Map, the flat keys will be ignored in favor of the existing expanded structure. Therefore we expand all keys before unmarshaling to the final config.This should solve all of the issues in #160 but we probably need a test for nested config and precedence, as proposed in #163.
Also related: #145, since with this change
Unmarshal
successfully merges config from env vars, pflags, defaults, and existing structures. I used this change to implement Viper config in ebay/fabio.