From 81806461a83f722185725eeb266b39674c8e6457 Mon Sep 17 00:00:00 2001 From: yangheng Date: Wed, 6 Jun 2018 15:00:40 +0800 Subject: [PATCH] add sub viper list --- .idea/vcs.xml | 6 ++++++ viper.go | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/viper.go b/viper.go index 907a102..27d7b33 100644 --- a/viper.go +++ b/viper.go @@ -664,6 +664,24 @@ func (v *Viper) Sub(key string) *Viper { return nil } +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) } func (v *Viper) GetString(key string) string {