Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gutil): panic when field is []byte(BINARY in mysql) #2957

Merged
merged 1 commit into from
Sep 18, 2023

Conversation

laushunyu
Copy link
Contributor

@laushunyu laushunyu commented Sep 12, 2023

Preview

在数据表定义时,我使用了 binary(32) 作为其主键,并使用 gf 生成了对应的 do 与 entity, 其主键类型为 []byte 。在数据关联查询过程中使用了 ListItemValuesUnique 导致了 panic。

Debug 发现当去重的 map 指定的 key 为 []byte 时,go 会认为其 not comparable 直接 panic, 考虑到 []byte 与 string 强转的内存拷贝问题,这里使用了 unsafe 来转换 StringHeader 与 SliceHeader 并将结果的 string 作为 key 进行去重操作 这里将 []byte 强转为 comparable 的 string 来进行 set key 操作

Testcase

func Test_ListItemValuesUnique_Binary_ID(t *testing.T) {
	gtest.C(t, func(t *gtest.T) {
		listMap := g.List{
			g.Map{"id": []byte{1}, "score": 100},
			g.Map{"id": []byte{2}, "score": 100},
			g.Map{"id": []byte{3}, "score": 100},
			g.Map{"id": []byte{4}, "score": 100},
			g.Map{"id": []byte{4}, "score": 100},
		}
		t.Assert(gutil.ListItemValuesUnique(listMap, "id"), g.Slice{[]byte{1}, []byte{2}, []byte{3}, []byte{4}})
		t.Assert(gutil.ListItemValuesUnique(listMap, "score"), g.Slice{100})
	})
}

Before

=== RUN   Test_ListItemValuesUnique_Binary_ID
runtime error: hash of unhashable type []uint8
1.  github.com/gogf/gf/v2/util/gutil.ListItemValuesUnique
    /home/macoo/gf/util/gutil/gutil_list.go:126
2.  github.com/gogf/gf/v2/util/gutil_test.Test_ListItemValuesUnique_Binary_ID.func1
    /home/macoo/gf/util/gutil/gutil_z_unit_list_test.go:259
3.  github.com/gogf/gf/v2/util/gutil_test.Test_ListItemValuesUnique_Binary_ID
    /home/macoo/gf/util/gutil/gutil_z_unit_list_test.go:251
--- FAIL: Test_ListItemValuesUnique_Binary_ID (0.00s)

After

=== RUN   Test_ListItemValuesUnique_Binary_ID
--- PASS: Test_ListItemValuesUnique_Binary_ID (0.00s)
PASS

util/gutil/gutil_list.go Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Sep 15, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: -0.04% ⚠️

Comparison is base (ef1e18d) 79.10% compared to head (e4fcc6b) 79.07%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2957      +/-   ##
==========================================
- Coverage   79.10%   79.07%   -0.04%     
==========================================
  Files         643      643              
  Lines       52749    52754       +5     
==========================================
- Hits        41727    41715      -12     
- Misses       8942     8953      +11     
- Partials     2080     2086       +6     
Flag Coverage Δ
go-1.18-386 ?
go-1.18-amd64 79.16% <100.00%> (+0.01%) ⬆️
go-1.19-386 79.02% <100.00%> (-0.04%) ⬇️
go-1.19-amd64 ?
go-1.20-386 ?
go-1.20-amd64 ?
go-1.21-386 79.11% <100.00%> (-0.05%) ⬇️
go-1.21-amd64 ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
util/gutil/gutil_list.go 93.81% <100.00%> (+0.33%) ⬆️

... and 5 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@gqcn gqcn merged commit 395df94 into gogf:master Sep 18, 2023
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants