From 81806461a83f722185725eeb266b39674c8e6457 Mon Sep 17 00:00:00 2001 From: yangheng Date: Wed, 6 Jun 2018 15:00:40 +0800 Subject: [PATCH 1/3] 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 { From 0648a0fafe721ee5b8dcaa3c3ee578edd57244a3 Mon Sep 17 00:00:00 2001 From: yangheng Date: Wed, 6 Jun 2018 15:03:55 +0800 Subject: [PATCH 2/3] remove --- .idea/vcs.xml | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 4ff42e3b4ba3f8720e565ea6bf9b75ff614c778d Mon Sep 17 00:00:00 2001 From: yangheng Date: Wed, 6 Jun 2018 15:07:10 +0800 Subject: [PATCH 3/3] add comment --- viper.go | 1 + 1 file changed, 1 insertion(+) diff --git a/viper.go b/viper.go index 27d7b33..8d52e1a 100644 --- a/viper.go +++ b/viper.go @@ -664,6 +664,7 @@ 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)