diff --git a/viper.go b/viper.go index 2d5158cbd..dc0c6f8ca 100644 --- a/viper.go +++ b/viper.go @@ -1302,7 +1302,7 @@ func (v *Viper) find(lcaseKey string, flagDefault bool) any { } // K/V store next - val = v.searchMap(v.kvstore, path) + val = v.searchIndexableWithPathPrefixes(v.kvstore, path) if val != nil { return val } diff --git a/viper_test.go b/viper_test.go index 8b0232aee..5662547d2 100644 --- a/viper_test.go +++ b/viper_test.go @@ -2630,6 +2630,30 @@ func TestSliceIndexAccess(t *testing.T) { assert.Equal(t, "Static", v.GetString("tv.0.episodes.1.2")) } +func TestSliceIndexAccessOnKvstore(t *testing.T) { + v := New() + v.SetConfigType("yaml") + r := strings.NewReader(string(yamlDeepNestedSlices)) + + err := v.unmarshalReader(r, v.kvstore) + require.NoError(t, err) + + assert.Equal(t, "The Expanse", v.GetString("tv.0.title")) + assert.Equal(t, "February 1, 2017", v.GetString("tv.0.seasons.1.first_released")) + assert.Equal(t, "Static", v.GetString("tv.0.seasons.1.episodes.2.title")) + assert.Equal(t, "December 15, 2015", v.GetString("tv.0.seasons.0.episodes.1.air_date")) + + // Test nested keys with capital letters + assert.Equal(t, "The Expanse", v.GetString("tv.0.title_i18n.USA")) + assert.Equal(t, "エクスパンス -巨獣めざめる-", v.GetString("tv.0.title_i18n.Japan")) + + // Test for index out of bounds + assert.Equal(t, "", v.GetString("tv.0.seasons.2.first_released")) + + // Accessing multidimensional arrays + assert.Equal(t, "Static", v.GetString("tv.0.episodes.1.2")) +} + func TestIsPathShadowedInFlatMap(t *testing.T) { v := New()