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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/pflag"
"gopkg.in/fsnotify.v1"
"github.com/doublerebel/bellows"
)

var v *Viper
Expand Down Expand Up @@ -612,7 +613,8 @@ func (v *Viper) UnmarshalKey(key string, rawVal interface{}) error {
// on the fields of the structure are properly set.
func Unmarshal(rawVal interface{}) error { return v.Unmarshal(rawVal) }
func (v *Viper) Unmarshal(rawVal interface{}) error {
err := mapstructure.WeakDecode(v.AllSettings(), rawVal)
expanded := bellows.Expand(v.AllSettings())
err := mapstructure.WeakDecode(expanded, rawVal)

if err != nil {
return err
Expand Down Expand Up @@ -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.

for flatkey, flatval := range flat {
flatkey = v.realKey(strings.ToLower(flatkey))
v.defaults[flatkey] = flatval
}
}

// Sets the value for the key in the override regiser.
Expand Down