Adjust ToStringKey use unpack params, fix pass []any as any in variadic function (#5500)

* fix pass []any as any in variadic function

* add .vscode to gitignore
This commit is contained in:
alingse 2022-07-14 20:05:22 +08:00 committed by GitHub
parent 4d40e34734
commit 099813bf11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 3 deletions

3
.gitignore vendored
View File

@ -3,4 +3,5 @@ documents
coverage.txt
_book
.idea
vendor
vendor
.vscode

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)
}
}
}