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.
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
Changes from 1 commit
418153c
9de3ddb
51d63dc
5b4f2f0
b037f60
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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吗?
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.
options 在它初始化 ReflectCopier 的时候就创建一个出来。正常这个 Copier 都是会被复用的,所有不需要特别担心性能问题。
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 里面的 options 应该是整个 Copier 维度的 options,但是单词调用是可以被覆盖的。
所以也就是你在这里,要把 ReflectCopier 里面的 option 复制出来一份。为了规避内存逃逸的问题,ReflectCopier 中的 options 不要用指针。
在这里,你会把 ReflectCopier 里面的 options 和 opts 进行合并,合并后的结果才是你最终的结果。
举例来说:默认初始化 ReflectCopier 的时候,我用了 ConvertField("A", converter1),但是当我在调用 Copy 方法的时候,我用了 ConvertField("A". converter2) 和 ConvertField("B", converter3),那么最终生效的就是 A converter2, 和 B converter3。
当我再一次发起 Copy 调用的时候,我依旧用的是 A converter1,也就是 Copier 本身的配置。