diff --git a/pkg/maputil/maputil.go b/pkg/maputil/maputil.go index 042bedb9..52457e03 100644 --- a/pkg/maputil/maputil.go +++ b/pkg/maputil/maputil.go @@ -103,7 +103,7 @@ func (a indexedKeyArg) getArray(m map[string]interface{}) []interface{} { case []interface{}: if len(t) <= a.index { t2 := make([]interface{}, a.index+1) - copy(t, t2) + copy(t2, t) t = t2 } return t diff --git a/pkg/maputil/maputil_test.go b/pkg/maputil/maputil_test.go index 9c80fd06..da2568a8 100644 --- a/pkg/maputil/maputil_test.go +++ b/pkg/maputil/maputil_test.go @@ -1,6 +1,10 @@ package maputil -import "testing" +import ( + "reflect" + "strings" + "testing" +) func TestMapUtil_StrKeys(t *testing.T) { m := map[string]interface{}{ @@ -88,6 +92,48 @@ func TestMapUtil_IndexedKeyArg(t *testing.T) { } } +func TestMapUtil_IndexedKeyArg2(t *testing.T) { + cases := []struct { + name string + stateValuesSet []string + want map[string]interface{} + }{ + { + name: "IndexedKeyArg", + stateValuesSet: []string{"myvalues[0]=HELLO,myvalues[1]=HELMFILE"}, + want: map[string]interface{}{"myvalues": []interface{}{"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"}}, + }, + { + 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"}}, + }, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + set := map[string]interface{}{} + for i := range c.stateValuesSet { + ops := strings.Split(c.stateValuesSet[i], ",") + for j := range ops { + op := strings.SplitN(ops[j], "=", 2) + k := ParseKey(op[0]) + v := op[1] + + Set(set, k, v) + } + } + if !reflect.DeepEqual(set, c.want) { + t.Errorf("expected set %v, got %v", c.want, set) + } + }) + } +} + type parseKeyTc struct { key string result map[int]string