fix pass []any as any in variadic function

This commit is contained in:
alingse 2022-07-09 20:45:15 +08:00
parent b13d1757fa
commit 3fde0baf3e
2 changed files with 19 additions and 2 deletions

View File

@ -206,7 +206,7 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
}
}
cacheKey := utils.ToStringKey(relPrimaryValues)
cacheKey := utils.ToStringKey(relPrimaryValues...)
if len(relPrimaryValues) != len(rel.FieldSchema.PrimaryFields) || !identityMap[cacheKey] {
identityMap[cacheKey] = true
if isPtr {
@ -292,7 +292,7 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
}
}
cacheKey := utils.ToStringKey(relPrimaryValues)
cacheKey := utils.ToStringKey(relPrimaryValues...)
if len(relPrimaryValues) != len(rel.FieldSchema.PrimaryFields) || !identityMap[cacheKey] {
identityMap[cacheKey] = true
distinctElems = reflect.Append(distinctElems, elem)

View File

@ -12,3 +12,20 @@ func TestIsValidDBNameChar(t *testing.T) {
}
}
}
func TestToStringKey(t *testing.T) {
cases := []struct {
values []interface{}
key string
}{
{[]interface{}{"a"}, "a"},
{[]interface{}{1, 2, 3}, "1_2_3"},
{[]interface{}{[]interface{}{1, 2, 3}}, "[1 2 3]"},
{[]interface{}{[]interface{}{"1", "2", "3"}}, "[1 2 3]"},
}
for _, c := range cases {
if key := ToStringKey(c.values...); key != c.key {
t.Errorf("%v: expected %v, got %v", c.values, c.key, key)
}
}
}