Fix getArray function copy error
Signed-off-by: xiaomudk <xiaomudk@gmail.com>
This commit is contained in:
parent
bdbfc9106d
commit
a05ccab6c3
|
|
@ -103,7 +103,7 @@ func (a indexedKeyArg) getArray(m map[string]interface{}) []interface{} {
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
if len(t) <= a.index {
|
if len(t) <= a.index {
|
||||||
t2 := make([]interface{}, a.index+1)
|
t2 := make([]interface{}, a.index+1)
|
||||||
copy(t, t2)
|
copy(t2, t)
|
||||||
t = t2
|
t = t2
|
||||||
}
|
}
|
||||||
return t
|
return t
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,10 @@
|
||||||
package maputil
|
package maputil
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestMapUtil_StrKeys(t *testing.T) {
|
func TestMapUtil_StrKeys(t *testing.T) {
|
||||||
m := map[string]interface{}{
|
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 {
|
type parseKeyTc struct {
|
||||||
key string
|
key string
|
||||||
result map[int]string
|
result map[int]string
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue