-
Notifications
You must be signed in to change notification settings - Fork 164
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
copier支持类型转换 #200
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以看看我们在 issue 里面的讨论,试试泛型的思路,我和另外一位同学的思路都可以试试。你看看效果
3bb11c3
to
418153c
Compare
Codecov Report
@@ 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
... 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/reflect_copier.go
Outdated
// 说明当前节点是叶子节点, 直接拷贝 | ||
child.isLeaf = true | ||
} else if fieldSrcTyp == reflect.TypeOf(time.Time{}) { |
There was a problem hiding this comment.
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 的默认取值就是这个默认值。
There was a problem hiding this comment.
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吗?
f30ba02
to
d131cac
Compare
d131cac
to
9de3ddb
Compare
bean/copier/reflect_copier.go
Outdated
@@ -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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这边应该用 Copier 里面的 atomicTypes 字段。因为我将来是准备允许用户指定哪些字段不需要递归解析下去。
There was a problem hiding this 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 这个包里面,对应的接口定义你可以自由决定要不要丢下去。我感觉都差不多。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
记得格式化代码
格式化一下代码就可以了 |
我顺便整合了下copyDefaultOptions() |
No description provided.