mirror of
https://github.com/helmfile/helmfile.git
synced 2026-10-17 21:56:08 +02:00
Replace interface{} by any (#901)
Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
+24
-24
@@ -5,10 +5,10 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func CastKeysToStrings(s interface{}) (map[string]interface{}, error) {
|
||||
new := map[string]interface{}{}
|
||||
func CastKeysToStrings(s any) (map[string]any, error) {
|
||||
new := map[string]any{}
|
||||
switch src := s.(type) {
|
||||
case map[interface{}]interface{}:
|
||||
case map[any]any:
|
||||
for k, v := range src {
|
||||
var strK string
|
||||
switch typedK := k.(type) {
|
||||
@@ -25,7 +25,7 @@ func CastKeysToStrings(s interface{}) (map[string]interface{}, error) {
|
||||
|
||||
new[strK] = castedV
|
||||
}
|
||||
case map[string]interface{}:
|
||||
case map[string]any:
|
||||
for k, v := range src {
|
||||
castedV, err := recursivelyStringifyMapKey(v)
|
||||
if err != nil {
|
||||
@@ -38,17 +38,17 @@ func CastKeysToStrings(s interface{}) (map[string]interface{}, error) {
|
||||
return new, nil
|
||||
}
|
||||
|
||||
func recursivelyStringifyMapKey(v interface{}) (interface{}, error) {
|
||||
var castedV interface{}
|
||||
func recursivelyStringifyMapKey(v any) (any, error) {
|
||||
var castedV any
|
||||
switch typedV := v.(type) {
|
||||
case map[interface{}]interface{}, map[string]interface{}:
|
||||
case map[any]any, map[string]any:
|
||||
tmp, err := CastKeysToStrings(typedV)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
castedV = tmp
|
||||
case []interface{}:
|
||||
a := []interface{}{}
|
||||
case []any:
|
||||
a := []any{}
|
||||
for i := range typedV {
|
||||
res, err := recursivelyStringifyMapKey(typedV[i])
|
||||
if err != nil {
|
||||
@@ -64,28 +64,28 @@ func recursivelyStringifyMapKey(v interface{}) (interface{}, error) {
|
||||
}
|
||||
|
||||
type arg interface {
|
||||
getMap(map[string]interface{}) map[string]interface{}
|
||||
set(map[string]interface{}, string)
|
||||
getMap(map[string]any) map[string]any
|
||||
set(map[string]any, string)
|
||||
}
|
||||
|
||||
type keyArg struct {
|
||||
key string
|
||||
}
|
||||
|
||||
func (a keyArg) getMap(m map[string]interface{}) map[string]interface{} {
|
||||
func (a keyArg) getMap(m map[string]any) map[string]any {
|
||||
_, ok := m[a.key]
|
||||
if !ok {
|
||||
m[a.key] = map[string]interface{}{}
|
||||
m[a.key] = map[string]any{}
|
||||
}
|
||||
switch t := m[a.key].(type) {
|
||||
case map[string]interface{}:
|
||||
case map[string]any:
|
||||
return t
|
||||
default:
|
||||
panic(fmt.Errorf("unexpected type: %v(%T)", t, t))
|
||||
}
|
||||
}
|
||||
|
||||
func (a keyArg) set(m map[string]interface{}, value string) {
|
||||
func (a keyArg) set(m map[string]any, value string) {
|
||||
m[a.key] = value
|
||||
}
|
||||
|
||||
@@ -94,15 +94,15 @@ type indexedKeyArg struct {
|
||||
index int
|
||||
}
|
||||
|
||||
func (a indexedKeyArg) getArray(m map[string]interface{}) []interface{} {
|
||||
func (a indexedKeyArg) getArray(m map[string]any) []any {
|
||||
_, ok := m[a.key]
|
||||
if !ok {
|
||||
m[a.key] = make([]interface{}, a.index+1)
|
||||
m[a.key] = make([]any, a.index+1)
|
||||
}
|
||||
switch t := m[a.key].(type) {
|
||||
case []interface{}:
|
||||
case []any:
|
||||
if len(t) <= a.index {
|
||||
t2 := make([]interface{}, a.index+1)
|
||||
t2 := make([]any, a.index+1)
|
||||
copy(t2, t)
|
||||
t = t2
|
||||
}
|
||||
@@ -112,20 +112,20 @@ func (a indexedKeyArg) getArray(m map[string]interface{}) []interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
func (a indexedKeyArg) getMap(m map[string]interface{}) map[string]interface{} {
|
||||
func (a indexedKeyArg) getMap(m map[string]any) map[string]any {
|
||||
t := a.getArray(m)
|
||||
if t[a.index] == nil {
|
||||
t[a.index] = map[string]interface{}{}
|
||||
t[a.index] = map[string]any{}
|
||||
}
|
||||
switch t := t[a.index].(type) {
|
||||
case map[string]interface{}:
|
||||
case map[string]any:
|
||||
return t
|
||||
default:
|
||||
panic(fmt.Errorf("unexpected type: %v(%T)", t, t))
|
||||
}
|
||||
}
|
||||
|
||||
func (a indexedKeyArg) set(m map[string]interface{}, value string) {
|
||||
func (a indexedKeyArg) set(m map[string]any, value string) {
|
||||
t := a.getArray(m)
|
||||
t[a.index] = value
|
||||
m[a.key] = t
|
||||
@@ -178,7 +178,7 @@ func ParseKey(key string) []string {
|
||||
return r
|
||||
}
|
||||
|
||||
func Set(m map[string]interface{}, key []string, value string) {
|
||||
func Set(m map[string]any, key []string, value string) {
|
||||
if len(key) == 0 {
|
||||
panic(fmt.Errorf("bug: unexpected length of key: %d", len(key)))
|
||||
}
|
||||
|
||||
+27
-27
@@ -7,11 +7,11 @@ import (
|
||||
)
|
||||
|
||||
func TestMapUtil_StrKeys(t *testing.T) {
|
||||
m := map[string]interface{}{
|
||||
"a": []interface{}{
|
||||
map[string]interface{}{
|
||||
"b": []interface{}{
|
||||
map[string]interface{}{
|
||||
m := map[string]any{
|
||||
"a": []any{
|
||||
map[string]any{
|
||||
"b": []any{
|
||||
map[string]any{
|
||||
"c": "C",
|
||||
},
|
||||
},
|
||||
@@ -24,10 +24,10 @@ func TestMapUtil_StrKeys(t *testing.T) {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
a := r["a"].([]interface{})
|
||||
a0 := a[0].(map[string]interface{})
|
||||
b := a0["b"].([]interface{})
|
||||
b0 := b[0].(map[string]interface{})
|
||||
a := r["a"].([]any)
|
||||
a0 := a[0].(map[string]any)
|
||||
b := a0["b"].([]any)
|
||||
b0 := b[0].(map[string]any)
|
||||
c := b0["c"]
|
||||
|
||||
if c != "C" {
|
||||
@@ -36,11 +36,11 @@ func TestMapUtil_StrKeys(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMapUtil_IFKeys(t *testing.T) {
|
||||
m := map[interface{}]interface{}{
|
||||
"a": []interface{}{
|
||||
map[interface{}]interface{}{
|
||||
"b": []interface{}{
|
||||
map[interface{}]interface{}{
|
||||
m := map[any]any{
|
||||
"a": []any{
|
||||
map[any]any{
|
||||
"b": []any{
|
||||
map[any]any{
|
||||
"c": "C",
|
||||
},
|
||||
},
|
||||
@@ -53,10 +53,10 @@ func TestMapUtil_IFKeys(t *testing.T) {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
a := r["a"].([]interface{})
|
||||
a0 := a[0].(map[string]interface{})
|
||||
b := a0["b"].([]interface{})
|
||||
b0 := b[0].(map[string]interface{})
|
||||
a := r["a"].([]any)
|
||||
a0 := a[0].(map[string]any)
|
||||
b := a0["b"].([]any)
|
||||
b0 := b[0].(map[string]any)
|
||||
c := b0["c"]
|
||||
|
||||
if c != "C" {
|
||||
@@ -65,13 +65,13 @@ func TestMapUtil_IFKeys(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMapUtil_KeyArg(t *testing.T) {
|
||||
m := map[string]interface{}{}
|
||||
m := map[string]any{}
|
||||
|
||||
key := []string{"a", "b", "c"}
|
||||
|
||||
Set(m, key, "C")
|
||||
|
||||
c := (((m["a"].(map[string]interface{}))["b"]).(map[string]interface{}))["c"]
|
||||
c := (((m["a"].(map[string]any))["b"]).(map[string]any))["c"]
|
||||
|
||||
if c != "C" {
|
||||
t.Errorf("unexpected c: expected=C, got=%s", c)
|
||||
@@ -79,13 +79,13 @@ func TestMapUtil_KeyArg(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMapUtil_IndexedKeyArg(t *testing.T) {
|
||||
m := map[string]interface{}{}
|
||||
m := map[string]any{}
|
||||
|
||||
key := []string{"a", "b[0]", "c"}
|
||||
|
||||
Set(m, key, "C")
|
||||
|
||||
c := (((m["a"].(map[string]interface{}))["b"].([]interface{}))[0].(map[string]interface{}))["c"]
|
||||
c := (((m["a"].(map[string]any))["b"].([]any))[0].(map[string]any))["c"]
|
||||
|
||||
if c != "C" {
|
||||
t.Errorf("unexpected c: expected=C, got=%s", c)
|
||||
@@ -96,27 +96,27 @@ func TestMapUtil_IndexedKeyArg2(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
stateValuesSet []string
|
||||
want map[string]interface{}
|
||||
want map[string]any
|
||||
}{
|
||||
{
|
||||
name: "IndexedKeyArg",
|
||||
stateValuesSet: []string{"myvalues[0]=HELLO,myvalues[1]=HELMFILE"},
|
||||
want: map[string]interface{}{"myvalues": []interface{}{"HELLO", "HELMFILE"}},
|
||||
want: map[string]any{"myvalues": []any{"HELLO", "HELMFILE"}},
|
||||
},
|
||||
{
|
||||
name: "two state value",
|
||||
stateValuesSet: []string{"myvalues[0]=HELLO,myvalues[1]=HELMFILE", "myvalues[2]=HELLO"},
|
||||
want: map[string]interface{}{"myvalues": []interface{}{"HELLO", "HELMFILE", "HELLO"}},
|
||||
want: map[string]any{"myvalues": []any{"HELLO", "HELMFILE", "HELLO"}},
|
||||
},
|
||||
{
|
||||
name: "different key",
|
||||
stateValuesSet: []string{"myvalues[0]=HELLO,key2[0]=HELMFILE", "myvalues[1]=HELLO2"},
|
||||
want: map[string]interface{}{"myvalues": []interface{}{"HELLO", "HELLO2"}, "key2": []interface{}{"HELMFILE"}},
|
||||
want: map[string]any{"myvalues": []any{"HELLO", "HELLO2"}, "key2": []any{"HELMFILE"}},
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
set := map[string]interface{}{}
|
||||
set := map[string]any{}
|
||||
for i := range c.stateValuesSet {
|
||||
ops := strings.Split(c.stateValuesSet[i], ",")
|
||||
for j := range ops {
|
||||
|
||||
Reference in New Issue
Block a user