Fix getArray function copy error

Signed-off-by: xiaomudk <xiaomudk@gmail.com>
This commit is contained in:
xiaomudk 2022-11-21 10:14:34 +08:00
parent bdbfc9106d
commit a05ccab6c3
2 changed files with 48 additions and 2 deletions

View File

@ -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

View File

@ -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