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: ReflectCopier 支持忽略字段 #196

Merged
merged 5 commits into from
Jul 5, 2023
Merged

Conversation

KelipuTe
Copy link
Contributor

@KelipuTe KelipuTe commented Jul 4, 2023

我直接采用了可行方案,引入 options 结构体。

需要忽略的字段需要有个地方存储,我想过直接把需要忽略的字段加在方法后面,但这显然不是个好主意。如果后续又有其他的附加功能,那这种设计就要继续在方法后面加参数。

引入 options 结构体后,在调用方法的时候,可以使用 option 设计模式,自主决定需要怎么修改 options 结构体的参数(触发哪些附加功能)。

忽略字段的逻辑,安排在了拷贝结点的过程中,如果发现结构体的属性名在需要忽略的字段里,就跳过这个属性的拷贝。

我把之前的差不多每个有效情况的测试用例都复制了一份,添加了一些忽略字段的场景。

@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

我们的 bean 包里面有一个泛型的 Option,你可以直接使用,它里面有辅助方法帮你完成这个后面这个组装。

// options 执行复制操作时的可选配置
type options struct {
// ignoreFields 执行复制操作时,需要忽略的字段
ignoreFields []string
Copy link
Contributor

Choose a reason for hiding this comment

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

我的设计有问题,这里你可以使用我们的 HashSet 结构。这样在查找的时候就不用遍历了

},
},
{
name: "simple struct with ignore 2",
Copy link
Contributor

Choose a reason for hiding this comment

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

改成忽略多个字段

Copy link
Contributor

Choose a reason for hiding this comment

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

你的测试用例不要用 1,2,3 看不懂是测试什么场景,要具体一点。

},
},
{
name: "simple struct with ignore 3",
Copy link
Contributor

Choose a reason for hiding this comment

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

忽略全部字段

},
},
{
name: "组合 struct with ignore 2",
Copy link
Contributor

Choose a reason for hiding this comment

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

组合忽略内部字段

},
},
{
name: "组合 struct with ignore",
Copy link
Contributor

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 Show resolved Hide resolved
},
},
{
name: "复杂 Struct with ignore 4",
Copy link
Contributor

Choose a reason for hiding this comment

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

多层嵌套忽略内部多层嵌套结构体

},
},
{
name: "特殊类型 with ignore",
Copy link
Contributor

Choose a reason for hiding this comment

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

忽略切片

},
},
{
name: "特殊类型 with ignore 2",
Copy link
Contributor

Choose a reason for hiding this comment

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

忽略 map

},
},
{
name: "dst 有额外字段 with ignore 2",
Copy link
Contributor

Choose a reason for hiding this comment

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

忽略 Src 中不存在字段

@KelipuTe
Copy link
Contributor Author

KelipuTe commented Jul 5, 2023

1、用 ekit 中 bean/option/ 的 option 和 set/ 中的 mapset 重新修改了一下代码结构。mapset 使用延迟初始化的策略,当设置忽略的字段的时候,再去初始化。

2、重新给测试用例命名

@flycash
Copy link
Contributor

flycash commented Jul 5, 2023

格式化一下代码。执行 make check 就可以了。然后再把这个合并请求添加进去 .CHANGELOG 里面

@codecov
Copy link

codecov bot commented Jul 5, 2023

Codecov Report

Merging #196 (a4b7c71) into dev (0f2c145) will increase coverage by 0.04%.
The diff coverage is 100.00%.

@@            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              
Impacted Files Coverage Δ
bean/copier/copy.go 100.00% <100.00%> (ø)
bean/copier/reflect_copier.go 93.23% <100.00%> (+0.43%) ⬆️

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

bean/copier/copy.go Outdated Show resolved Hide resolved
@flycash flycash merged commit 72ded6a into ecodeclub:dev Jul 5, 2023
7 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