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 {
subv := New()
data := v.Get(key)
if data == nil {
return nil
}
if reflect.TypeOf(data).Kind() == reflect.Map {
subv.config = cast.ToStringMap(data)
return subv

View File

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