mirror of
https://github.com/spf13/viper
synced 2024-12-23 12:07:02 +00:00
add method subSlice to get an array/table/list of config options as slice of "sub-vipers"
This commit is contained in:
parent
32aa319187
commit
8e90aaafac
1 changed files with 23 additions and 0 deletions
23
viper.go
23
viper.go
|
@ -1782,3 +1782,26 @@ func (v *Viper) Debug() {
|
|||
fmt.Printf("Config:\n%#v\n", v.config)
|
||||
fmt.Printf("Defaults:\n%#v\n", v.defaults)
|
||||
}
|
||||
|
||||
func SubSlice(key string) (out []*Viper) { return v.SubSlice(key) }
|
||||
func (v *Viper) SubSlice(key string) (out []*Viper) {
|
||||
data := v.Get(key)
|
||||
|
||||
if data == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if reflect.TypeOf(data).Kind() != reflect.Slice {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, elem := range data.([]interface{}) {
|
||||
subv := New()
|
||||
if reflect.TypeOf(elem).Kind() == reflect.Map {
|
||||
subv.config = cast.ToStringMap(elem)
|
||||
out = append(out, subv)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue