From 40800105e21d17618d6b87af240ae05d3f2c379c Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Thu, 29 Jun 2023 06:38:06 +0200 Subject: [PATCH] feat: partially revert #1387 1387 introduced a rather serious breaking change that's causing pain throughout the community. Even though it fixed a buggy behavior the previous one was still useful to a lot of people. This change introduces a way to revert back to that old behavior. Signed-off-by: Mark Sagi-Kazar --- internal/features/1387.go | 7 +++++++ internal/features/1387_.go | 7 +++++++ internal/features/doc.go | 2 ++ util.go | 9 +++++++-- viper_test.go | 17 +++++++++++++++++ 5 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 internal/features/1387.go create mode 100644 internal/features/1387_.go create mode 100644 internal/features/doc.go diff --git a/internal/features/1387.go b/internal/features/1387.go new file mode 100644 index 0000000..03271c2 --- /dev/null +++ b/internal/features/1387.go @@ -0,0 +1,7 @@ +//go:build !viper_1387 +// +build !viper_1387 + +package features + +// Revert1387 reverts the behavior introduced in #1387. +var Revert1387 = false diff --git a/internal/features/1387_.go b/internal/features/1387_.go new file mode 100644 index 0000000..c95435a --- /dev/null +++ b/internal/features/1387_.go @@ -0,0 +1,7 @@ +//go:build viper_1387 +// +build viper_1387 + +package features + +// Revert1387 reverts the behavior introduced in #1387. +var Revert1387 = true diff --git a/internal/features/doc.go b/internal/features/doc.go new file mode 100644 index 0000000..e92d472 --- /dev/null +++ b/internal/features/doc.go @@ -0,0 +1,2 @@ +// Package features allows toggling features in Viper based on build tags. +package features diff --git a/util.go b/util.go index 95009a1..e56af0b 100644 --- a/util.go +++ b/util.go @@ -19,6 +19,8 @@ import ( "unicode" "github.com/spf13/cast" + + "github.com/spf13/viper/internal/features" ) // ConfigParseError denotes failing to parse configuration file. @@ -79,8 +81,11 @@ func insensitiviseVal(val interface{}) interface{} { // nested map: recursively insensitivise insensitiviseMap(val.(map[string]interface{})) case []interface{}: - // nested array: recursively insensitivise - insensitiveArray(val.([]interface{})) + // deprecated, drop in Viper v2 + if !features.Revert1387 { + // nested array: recursively insensitivise + insensitiveArray(val.([]interface{})) + } } return val } diff --git a/viper_test.go b/viper_test.go index e0bfc57..b5380a6 100644 --- a/viper_test.go +++ b/viper_test.go @@ -31,6 +31,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/spf13/viper/internal/features" "github.com/spf13/viper/internal/testutil" ) @@ -2672,6 +2673,22 @@ func TestSliceIndexAccess(t *testing.T) { assert.Equal(t, "The Expanse", v.GetString("tv.0.title_i18n.USA")) assert.Equal(t, "エクスパンス -巨獣めざめる-", v.GetString("tv.0.title_i18n.Japan")) + var expectedtitlei18n map[string]any + + if features.Revert1387 { + expectedtitlei18n = map[string]any{ + "USA": "The Expanse", + "Japan": "エクスパンス -巨獣めざめる-", + } + } else { + expectedtitlei18n = map[string]any{ + "usa": "The Expanse", + "japan": "エクスパンス -巨獣めざめる-", + } + } + + assert.Equal(t, expectedtitlei18n, v.GetStringMap("tv.0.title_i18n")) + // Test for index out of bounds assert.Equal(t, "", v.GetString("tv.0.seasons.2.first_released"))