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

flatten defaults to ensure env var load; expand flat settings to ensure override precedence [Fixes #71] [Fixes #160] #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

doublerebel
Copy link

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 Unmarshaling 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.

@CLAassistant
Copy link

CLAassistant commented Mar 2, 2016

CLA assistant check
All committers have signed the CLA.

@@ -908,8 +910,11 @@ func (v *Viper) InConfig(key string) bool {
func SetDefault(key string, value interface{}) { v.SetDefault(key, value) }
func (v *Viper) SetDefault(key string, value interface{}) {
// If alias passed in, then set the proper default
key = v.realKey(strings.ToLower(key))
v.defaults[key] = value
flat := bellows.FlattenPrefixed(value, key)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

harnash added a commit to harnash/watcher that referenced this pull request May 17, 2016
This code comes from spf13/viper#164
which by the time of this commit wasn't merged yet.
@awfm9
Copy link

awfm9 commented Sep 20, 2016

This seems like an important improvement, I'll try to take a look soon. In the meantime, if you could rebase it, it would be appreciated.

@awfm9 awfm9 self-assigned this Sep 20, 2016
@awfm9
Copy link

awfm9 commented Oct 12, 2016

I think this PR has a different approach to what was merged in #195 . It flattens everything, while we now create maps for everything. I wonder if correct behaviour would have been possible with your approach, because I think it would not have the same performance implications, though I doubt it.

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.

None yet

3 participants