flatten defaults to ensure all env vars are loaded; expand flat settings to ensure override precedence in merge

This commit is contained in:
Charles Phillips 2016-03-02 15:27:21 -08:00
parent c975dc1b4e
commit 23ffa6a2ea

View file

@ -37,6 +37,7 @@ import (
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"gopkg.in/fsnotify.v1" "gopkg.in/fsnotify.v1"
"github.com/doublerebel/bellows"
) )
var v *Viper 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. // on the fields of the structure are properly set.
func Unmarshal(rawVal interface{}) error { return v.Unmarshal(rawVal) } func Unmarshal(rawVal interface{}) error { return v.Unmarshal(rawVal) }
func (v *Viper) Unmarshal(rawVal interface{}) error { 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 { if err != nil {
return err return err
@ -908,8 +910,11 @@ func (v *Viper) InConfig(key string) bool {
func SetDefault(key string, value interface{}) { v.SetDefault(key, value) } func SetDefault(key string, value interface{}) { v.SetDefault(key, value) }
func (v *Viper) SetDefault(key string, value interface{}) { func (v *Viper) SetDefault(key string, value interface{}) {
// If alias passed in, then set the proper default // If alias passed in, then set the proper default
key = v.realKey(strings.ToLower(key)) flat := bellows.FlattenPrefixed(value, key)
v.defaults[key] = value 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. // Sets the value for the key in the override regiser.