-
Notifications
You must be signed in to change notification settings - Fork 157
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: ReflectCopier 支持忽略字段 #196
Conversation
bean/copier/reflect_copier.go
Outdated
@@ -154,7 +157,16 @@ func (r *ReflectCopier[Src, Dst]) Copy(src *Src) (*Dst, error) { | |||
// 2. 如果 Src 和 Dst 中匹配的字段,其类型是基本类型(及其指针)或者内置类型(及其指针),并且类型一样,则直接用 Src 的值 | |||
// 3. 如果 Src 和 Dst 中匹配的字段,其类型都是结构体,或者都是结构体指针,则会深入复制 | |||
// 4. 否则,忽略字段 | |||
func (r *ReflectCopier[Src, Dst]) CopyTo(src *Src, dst *Dst) error { | |||
func (r *ReflectCopier[Src, Dst]) CopyTo(src *Src, dst *Dst, opts ...Option) error { |
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.
我们的 bean 包里面有一个泛型的 Option,你可以直接使用,它里面有辅助方法帮你完成这个后面这个组装。
bean/copier/copy.go
Outdated
// options 执行复制操作时的可选配置 | ||
type options struct { | ||
// ignoreFields 执行复制操作时,需要忽略的字段 | ||
ignoreFields []string |
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.
我的设计有问题,这里你可以使用我们的 HashSet 结构。这样在查找的时候就不用遍历了
bean/copier/reflect_copier_test.go
Outdated
}, | ||
}, | ||
{ | ||
name: "simple struct with ignore 2", |
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.
改成忽略多个字段
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.
你的测试用例不要用 1,2,3 看不懂是测试什么场景,要具体一点。
bean/copier/reflect_copier_test.go
Outdated
}, | ||
}, | ||
{ | ||
name: "simple struct with ignore 3", |
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.
忽略全部字段
bean/copier/reflect_copier_test.go
Outdated
}, | ||
}, | ||
{ | ||
name: "组合 struct with ignore 2", |
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.
组合忽略内部字段
bean/copier/reflect_copier_test.go
Outdated
}, | ||
}, | ||
{ | ||
name: "组合 struct with ignore", |
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.
组合同名字段都被忽略
bean/copier/reflect_copier_test.go
Outdated
}, | ||
}, | ||
{ | ||
name: "复杂 Struct with ignore 4", |
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.
多层嵌套忽略内部多层嵌套结构体
bean/copier/reflect_copier_test.go
Outdated
}, | ||
}, | ||
{ | ||
name: "特殊类型 with ignore", |
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.
忽略切片
bean/copier/reflect_copier_test.go
Outdated
}, | ||
}, | ||
{ | ||
name: "特殊类型 with ignore 2", |
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.
忽略 map
bean/copier/reflect_copier_test.go
Outdated
}, | ||
}, | ||
{ | ||
name: "dst 有额外字段 with ignore 2", |
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.
忽略 Src 中不存在字段
1、用 ekit 中 bean/option/ 的 option 和 set/ 中的 mapset 重新修改了一下代码结构。mapset 使用延迟初始化的策略,当设置忽略的字段的时候,再去初始化。 2、重新给测试用例命名 |
格式化一下代码。执行 make check 就可以了。然后再把这个合并请求添加进去 .CHANGELOG 里面 |
Codecov Report
@@ Coverage Diff @@
## dev #196 +/- ##
==========================================
+ Coverage 95.90% 95.94% +0.04%
==========================================
Files 46 47 +1
Lines 2515 2542 +27
==========================================
+ Hits 2412 2439 +27
Misses 81 81
Partials 22 22
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
我直接采用了可行方案,引入 options 结构体。
需要忽略的字段需要有个地方存储,我想过直接把需要忽略的字段加在方法后面,但这显然不是个好主意。如果后续又有其他的附加功能,那这种设计就要继续在方法后面加参数。
引入 options 结构体后,在调用方法的时候,可以使用 option 设计模式,自主决定需要怎么修改 options 结构体的参数(触发哪些附加功能)。
忽略字段的逻辑,安排在了拷贝结点的过程中,如果发现结构体的属性名在需要忽略的字段里,就跳过这个属性的拷贝。
我把之前的差不多每个有效情况的测试用例都复制了一份,添加了一些忽略字段的场景。