Skip to content

Commit

Permalink
feat: partially revert #1387
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
sagikazarmark committed Jun 29, 2023
1 parent 961c396 commit cf14683
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
7 changes: 7 additions & 0 deletions internal/features/1387.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !viper_1387
// +build !viper_1387

package features

// Revert1387 reverts the behavior introduced in #1387.
var Revert1387 = false
7 changes: 7 additions & 0 deletions internal/features/1387_.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build viper_1387
// +build viper_1387

package features

// Revert1387 reverts the behavior introduced in #1387.
var Revert1387 = true
2 changes: 2 additions & 0 deletions internal/features/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package features allows toggling features in Viper based on build tags.
package features
8 changes: 6 additions & 2 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"unicode"

"github.com/spf13/cast"
"github.com/spf13/viper/internal/features"
)

// ConfigParseError denotes failing to parse configuration file.
Expand Down Expand Up @@ -79,8 +80,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
}
Expand Down
17 changes: 17 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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"))

Expand Down

0 comments on commit cf14683

Please sign in to comment.