From 670c42a85b2a2215949acd943cb8f11add317e3f Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Wed, 5 Oct 2016 18:22:39 -0500 Subject: [PATCH] Check for nil in viper.Sub Fixes #191 --- viper.go | 4 ++++ viper_test.go | 3 +++ 2 files changed, 7 insertions(+) diff --git a/viper.go b/viper.go index 0fbd051..6964041 100644 --- a/viper.go +++ b/viper.go @@ -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 diff --git a/viper_test.go b/viper_test.go index 8558f92..72f695e 100644 --- a/viper_test.go +++ b/viper_test.go @@ -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(`