mirror of
https://github.com/spf13/viper
synced 2024-12-22 19:47:01 +00:00
fix: Viper.Sub() Arrays not supported
This commit is contained in:
parent
5247643f02
commit
d6b9754a87
2 changed files with 17 additions and 1 deletions
15
viper.go
15
viper.go
|
@ -939,9 +939,22 @@ func (v *Viper) Sub(key string) *Viper {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if reflect.TypeOf(data).Kind() == reflect.Map {
|
//if t := reflect.TypeOf(data).Kind(); t == reflect.Map || t == reflect.Slice {
|
||||||
|
// // https://github.com/spf13/cast/pull/148
|
||||||
|
// subv.config = cast.ToStringMap(data)
|
||||||
|
// return subv
|
||||||
|
//}
|
||||||
|
switch reflect.TypeOf(data).Kind() {
|
||||||
|
case reflect.Map:
|
||||||
subv.config = cast.ToStringMap(data)
|
subv.config = cast.ToStringMap(data)
|
||||||
return subv
|
return subv
|
||||||
|
case reflect.Slice, reflect.Array:
|
||||||
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1500,6 +1500,9 @@ func TestSub(t *testing.T) {
|
||||||
|
|
||||||
subv = v.Sub("missing.key")
|
subv = v.Sub("missing.key")
|
||||||
assert.Equal(t, (*Viper)(nil), subv)
|
assert.Equal(t, (*Viper)(nil), subv)
|
||||||
|
|
||||||
|
subv = v.Sub("hobbies")
|
||||||
|
assert.Equal(t, v.Get("hobbies.0"), subv.Get("0"))
|
||||||
}
|
}
|
||||||
|
|
||||||
var hclWriteExpected = []byte(`"foos" = {
|
var hclWriteExpected = []byte(`"foos" = {
|
||||||
|
|
Loading…
Reference in a new issue