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

copier支持类型转换 #200

Merged
merged 5 commits into from
Aug 10, 2023
Merged

Conversation

hookokoko
Copy link
Contributor

No description provided.

Copy link
Contributor

@flycash flycash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以看看我们在 issue 里面的讨论,试试泛型的思路,我和另外一位同学的思路都可以试试。你看看效果

@codecov
Copy link

codecov bot commented Aug 4, 2023

Codecov Report

Merging #200 (5b4f2f0) into dev (4a29bd6) will decrease coverage by 0.53%.
Report is 3 commits behind head on dev.
The diff coverage is 78.78%.

❗ Current head 5b4f2f0 differs from pull request most recent head b037f60. Consider uploading reports for the commit b037f60 to get more accurate results

@@            Coverage Diff             @@
##              dev     #200      +/-   ##
==========================================
- Coverage   95.94%   95.41%   -0.53%     
==========================================
  Files          47       50       +3     
  Lines        2538     2641     +103     
==========================================
+ Hits         2435     2520      +85     
- Misses         81       93      +12     
- Partials       22       28       +6     
Files Changed Coverage Δ
bean/copier/errors.go 100.00% <ø> (ø)
bean/copier/copy.go 82.35% <64.70%> (-17.65%) ⬇️
bean/copier/reflect_copier.go 88.46% <81.70%> (-4.78%) ⬇️

... and 5 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

bean/copier/converter.go Outdated Show resolved Hide resolved
bean/copier/copy.go Outdated Show resolved Hide resolved
bean/copier/copy.go Show resolved Hide resolved
bean/copier/errors.go Outdated Show resolved Hide resolved
// 说明当前节点是叶子节点, 直接拷贝
child.isLeaf = true
} else if fieldSrcTyp == reflect.TypeOf(time.Time{}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个地方加一个注释,因为也不仅仅是 time.Time,类似于 sql.NullXXX 的应该也是这么处理的。所以将来我们可能需要考虑允许用户指定什么样的类型是一个整体,不用递归下去。

然后引入一个 atomicTypes 字段,里面放着所有的类似于 time,Time 这种被看做整体的类型。同时有一个 defaultActomicTypes 作为包变量,atomicTypes 的默认取值就是这个默认值。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我现在的实现是在ReflectCopier结构体中加了一个atomicTypes,但是目前的实现看没有用到。直接就用defaultActomicTypes了。后续是要将ReflectCopier.atomicTypes允许用户修改,然后赋值给包变量defaultActomicTypes吗?

bean/copier/reflect_copier.go Outdated Show resolved Hide resolved
bean/copier/reflect_copier.go Outdated Show resolved Hide resolved
bean/copier/reflect_copier.go Show resolved Hide resolved
bean/copier/reflect_copier.go Outdated Show resolved Hide resolved
bean/copier/reflect_copier_test.go Show resolved Hide resolved
bean/copier/reflect_copier.go Show resolved Hide resolved
@@ -218,6 +283,15 @@ func (r *ReflectCopier[Src, Dst]) copyTreeNode(srcTyp reflect.Type, srcValue ref
return nil
}

func isAtomicType(typ reflect.Type) bool {
for _, dt := range defaultAtomicTypes {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这边应该用 Copier 里面的 atomicTypes 字段。因为我将来是准备允许用户指定哪些字段不需要递归解析下去。

bean/copier/reflect_copier.go Show resolved Hide resolved
Copy link
Contributor

@flycash flycash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

解决了那个并发问题,再把 time2string.go 丢到 copier/converter 这个包里面,对应的接口定义你可以自由决定要不要丢下去。我感觉都差不多。

bean/copier/reflect_copier.go Outdated Show resolved Hide resolved
Copy link
Contributor

@flycash flycash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

记得格式化代码

bean/copier/reflect_copier.go Show resolved Hide resolved
@hookokoko hookokoko requested a review from flycash August 9, 2023 02:45
@flycash
Copy link
Contributor

flycash commented Aug 9, 2023

格式化一下代码就可以了

flycash
flycash previously approved these changes Aug 9, 2023
@hookokoko
Copy link
Contributor Author

格式化一下代码就可以了

我顺便整合了下copyDefaultOptions()

@flycash flycash merged commit b656686 into ecodeclub:dev Aug 10, 2023
5 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.

2 participants