From deb8393798ec85ba421bdf370403e314bdeda486 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 8 Mar 2020 18:27:23 +0100 Subject: [PATCH 1/3] Add SubSlice to return slice of sub vipers --- viper.go | 22 ++++++++++++++++++++++ viper_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/viper.go b/viper.go index 7b12b36e9..5f036b0e0 100644 --- a/viper.go +++ b/viper.go @@ -787,6 +787,28 @@ func (v *Viper) Sub(key string) *Viper { return nil } +// SubSlice returns a slice of new Viper instances representing a sub tree of this instance. +// Sub is case-insensitive for a key. +func SubSlice(key string) []*Viper { return v.SubSlice(key) } +func (v *Viper) SubSlice(key string) []*Viper { + data := v.Get(key) + if data == nil { + return nil + } + + if reflect.TypeOf(data).Kind() == reflect.Slice { + var vList []*Viper + 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 { diff --git a/viper_test.go b/viper_test.go index b8ceccba5..456f633b9 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1253,6 +1253,30 @@ func TestSub(t *testing.T) { assert.Equal(t, (*Viper)(nil), subv) } +func TestSubSlice(t *testing.T) { + var yamlList = []byte(`map: + foo: bar +list: +- foo: 0 + bar: 0 +- foo: 1 + bar: 1 +`) + + v := New() + v.SetConfigType("yaml") + v.ReadConfig(bytes.NewBuffer(yamlList)) + + subvSlice := v.SubSlice("list") + for idx, subv := range subvSlice { + assert.Equal(t, subv.GetInt("foo"), idx) + assert.Equal(t, subv.GetInt("bar"), idx) + } + + subvSlice = v.SubSlice("map") + assert.Equal(t, ([]*Viper)(nil), subvSlice) +} + var hclWriteExpected = []byte(`"foos" = { "foo" = { "key" = 1 From 5f99818ac1c05c706b3111e8355fea03fb94cb5b Mon Sep 17 00:00:00 2001 From: andig Date: Wed, 14 Jan 2026 09:41:31 +0100 Subject: [PATCH 2/3] Fix lint --- viper.go | 3 +++ viper_test.go | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/viper.go b/viper.go index e46e507e1..e8694fba9 100644 --- a/viper.go +++ b/viper.go @@ -790,6 +790,9 @@ func (v *Viper) Sub(key string) *Viper { // SubSlice returns a slice of new Viper instances representing a sub tree of this instance. // Sub is case-insensitive for a key. func SubSlice(key string) []*Viper { return v.SubSlice(key) } + +// SubSlice returns a slice of new Viper instances representing a sub tree of this instance. +// Sub is case-insensitive for a key. func (v *Viper) SubSlice(key string) []*Viper { data := v.Get(key) if data == nil { diff --git a/viper_test.go b/viper_test.go index f43305ac1..43e6f3b13 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1747,7 +1747,7 @@ func TestSub(t *testing.T) { } func TestSubSlice(t *testing.T) { - var yamlList = []byte(`map: + yamlList := []byte(`map: foo: bar list: - foo: 0 From d2e5a7815526ceb0c6fd22a983cddf9a31640eee Mon Sep 17 00:00:00 2001 From: andig Date: Wed, 14 Jan 2026 09:43:06 +0100 Subject: [PATCH 3/3] Fix lint (2) --- viper_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/viper_test.go b/viper_test.go index 43e6f3b13..d67d56147 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1767,7 +1767,7 @@ list: } subvSlice = v.SubSlice("map") - assert.Equal(t, ([]*Viper)(nil), subvSlice) + assert.Equal(t, []*Viper(nil), subvSlice) } func TestSubWithKeyDelimiter(t *testing.T) {