mirror of
https://github.com/spf13/viper
synced 2025-01-03 01:06:39 +00:00
fix isPathShadowedInFlatMap type cast bug (#1585)
* fix isPathShadowedInFlatMap type cast bug * Add "IsPathShadowedInFlatMap" method unit test * fix: typo * add an unit test for flag shadow --------- Co-authored-by: Márk Sági-Kazár <sagikazarmark@users.noreply.github.com>
This commit is contained in:
parent
36a38682ba
commit
f7363633d1
2 changed files with 51 additions and 4 deletions
10
viper.go
10
viper.go
|
@ -827,10 +827,12 @@ func (v *Viper) isPathShadowedInDeepMap(path []string, m map[string]any) string
|
||||||
// "foo.bar.baz" in a lower-priority map
|
// "foo.bar.baz" in a lower-priority map
|
||||||
func (v *Viper) isPathShadowedInFlatMap(path []string, mi any) string {
|
func (v *Viper) isPathShadowedInFlatMap(path []string, mi any) string {
|
||||||
// unify input map
|
// unify input map
|
||||||
var m map[string]any
|
var m map[string]interface{}
|
||||||
switch mi.(type) {
|
switch miv := mi.(type) {
|
||||||
case map[string]string, map[string]FlagValue:
|
case map[string]string:
|
||||||
m = cast.ToStringMap(mi)
|
m = castMapStringToMapInterface(miv)
|
||||||
|
case map[string]FlagValue:
|
||||||
|
m = castMapFlagToMapInterface(miv)
|
||||||
default:
|
default:
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
|
@ -2575,6 +2575,51 @@ func TestSliceIndexAccess(t *testing.T) {
|
||||||
assert.Equal(t, "Static", v.GetString("tv.0.episodes.1.2"))
|
assert.Equal(t, "Static", v.GetString("tv.0.episodes.1.2"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsPathShadowedInFlatMap(t *testing.T) {
|
||||||
|
v := New()
|
||||||
|
|
||||||
|
stringMap := map[string]string{
|
||||||
|
"foo": "value",
|
||||||
|
}
|
||||||
|
|
||||||
|
flagMap := map[string]FlagValue{
|
||||||
|
"foo": pflagValue{},
|
||||||
|
}
|
||||||
|
|
||||||
|
path1 := []string{"foo", "bar"}
|
||||||
|
expected1 := "foo"
|
||||||
|
|
||||||
|
// "foo.bar" should shadowed by "foo"
|
||||||
|
assert.Equal(t, expected1, v.isPathShadowedInFlatMap(path1, stringMap))
|
||||||
|
assert.Equal(t, expected1, v.isPathShadowedInFlatMap(path1, flagMap))
|
||||||
|
|
||||||
|
path2 := []string{"bar", "foo"}
|
||||||
|
expected2 := ""
|
||||||
|
|
||||||
|
// "bar.foo" should not shadowed by "foo"
|
||||||
|
assert.Equal(t, expected2, v.isPathShadowedInFlatMap(path2, stringMap))
|
||||||
|
assert.Equal(t, expected2, v.isPathShadowedInFlatMap(path2, flagMap))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFlagShadow(t *testing.T) {
|
||||||
|
v := New()
|
||||||
|
|
||||||
|
v.SetDefault("foo.bar1.bar2", "default")
|
||||||
|
|
||||||
|
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
|
||||||
|
flags.String("foo.bar1", "shadowed", "")
|
||||||
|
flags.VisitAll(func(flag *pflag.Flag) {
|
||||||
|
flag.Changed = true
|
||||||
|
})
|
||||||
|
|
||||||
|
v.BindPFlags(flags)
|
||||||
|
|
||||||
|
assert.Equal(t, "shadowed", v.GetString("foo.bar1"))
|
||||||
|
// the default "foo.bar1.bar2" value should shadowed by flag "foo.bar1" value
|
||||||
|
// and should return an empty string
|
||||||
|
assert.Equal(t, "", v.GetString("foo.bar1.bar2"))
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkGetBool(b *testing.B) {
|
func BenchmarkGetBool(b *testing.B) {
|
||||||
key := "BenchmarkGetBool"
|
key := "BenchmarkGetBool"
|
||||||
v = New()
|
v = New()
|
||||||
|
|
Loading…
Reference in a new issue