Check for nil in viper.Sub

Fixes #191
This commit is contained in:
Cameron Moore 2016-10-05 18:22:39 -05:00 committed by Bjørn Erik Pedersen
parent 382f87b929
commit 670c42a85b
2 changed files with 7 additions and 0 deletions

View file

@ -532,6 +532,10 @@ func Sub(key string) *Viper { return v.Sub(key) }
func (v *Viper) Sub(key string) *Viper { func (v *Viper) Sub(key string) *Viper {
subv := New() subv := New()
data := v.Get(key) data := v.Get(key)
if data == nil {
return nil
}
if reflect.TypeOf(data).Kind() == reflect.Map { if reflect.TypeOf(data).Kind() == reflect.Map {
subv.config = cast.ToStringMap(data) subv.config = cast.ToStringMap(data)
return subv return subv

View file

@ -760,6 +760,9 @@ func TestSub(t *testing.T) {
subv = v.Sub("clothing.pants.size") subv = v.Sub("clothing.pants.size")
assert.Equal(t, subv, (*Viper)(nil)) assert.Equal(t, subv, (*Viper)(nil))
subv = v.Sub("missing.key")
assert.Equal(t, subv, (*Viper)(nil))
} }
var yamlMergeExampleTgt = []byte(` var yamlMergeExampleTgt = []byte(`