diff --git a/viper.go b/viper.go index 7de2e78..5bf469a 100644 --- a/viper.go +++ b/viper.go @@ -956,13 +956,25 @@ func (v *Viper) Sub(key string) *Viper { return nil } - if reflect.TypeOf(data).Kind() == reflect.Map { + switch reflect.TypeOf(data).Kind() { + case reflect.Map: subv.parents = append(v.parents, strings.ToLower(key)) subv.automaticEnvApplied = v.automaticEnvApplied subv.envPrefix = v.envPrefix subv.envKeyReplacer = v.envKeyReplacer subv.config = cast.ToStringMap(data) return subv + case reflect.Slice, reflect.Array: + subv.parents = append(v.parents, strings.ToLower(key)) + subv.automaticEnvApplied = v.automaticEnvApplied + subv.envPrefix = v.envPrefix + subv.envKeyReplacer = v.envKeyReplacer + dataValue := reflect.ValueOf(data) + subv.config = make(map[string]interface{}, dataValue.Len()) + for x := 0; x < dataValue.Len(); x++ { + subv.config[cast.ToString(x)] = dataValue.Index(x).Interface() + } + return subv } return nil } diff --git a/viper_test.go b/viper_test.go index 0e416e7..275f460 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1536,6 +1536,10 @@ func TestSub(t *testing.T) { subv = v.Sub("missing.key") assert.Equal(t, (*Viper)(nil), subv) + + subv = v.Sub("hobbies") + assert.Equal(t, v.Get("hobbies.0"), subv.Get("0")) + subv = v.Sub("clothing") assert.Equal(t, []string{"clothing"}, subv.parents)