This commit is contained in:
Profbis 2024-05-12 00:22:41 +02:00 committed by GitHub
commit 73b69092e9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -971,6 +971,25 @@ func (v *Viper) Sub(key string) *Viper {
return nil
}
//Sub list returns new Viper instance List representing a sub tree of this instance.
func SubList(key string) []*Viper { return v.SubList(key) }
func (v *Viper) SubList(key string) []*Viper {
data := v.Get(key)
if data == nil {
return nil
}
var vList []*Viper
if reflect.TypeOf(data).Kind() == reflect.Slice {
for _, item := range data.([]interface{}) {
subv := New()
subv.config = cast.ToStringMap(item)
vList = append(vList, subv)
}
return vList
}
return nil
}
// GetString returns the value associated with the key as a string.
func GetString(key string) string { return v.GetString(key) }