fix(gutil): panic when field is []byte(BINARY in mysql) #2957
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Before
After