mirror of https://github.com/spf13/viper.git
parent
382f87b929
commit
670c42a85b
4
viper.go
4
viper.go
|
@ -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
|
||||||
|
|
|
@ -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(`
|
||||||
|
|
Loading…
Reference in New Issue