From 23ffa6a2eaae6a4585e8a4f041ebc35b0b2a78c2 Mon Sep 17 00:00:00 2001 From: Charles Phillips Date: Wed, 2 Mar 2016 15:27:21 -0800 Subject: [PATCH] flatten defaults to ensure all env vars are loaded; expand flat settings to ensure override precedence in merge --- viper.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/viper.go b/viper.go index 7a49a0a..b68ab4f 100644 --- a/viper.go +++ b/viper.go @@ -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 @@ -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 @@ -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) + 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.