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

结构体 MarshalJSON 实现做了大量职责之外的事 #173

Open
lostpg opened this issue Apr 14, 2023 · 2 comments
Open

结构体 MarshalJSON 实现做了大量职责之外的事 #173

lostpg opened this issue Apr 14, 2023 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@lostpg
Copy link

lostpg commented Apr 14, 2023

  • Go 版本:1.20.3
  • wechatpay-go 版本:0.2.14

这个结构体的MarshalJSON方法 所示,库中所有的请求和返回结构体的MarshalJSON方法实现都硬编码了序列化后的 key,这种做法绕过了结构体注解中对json key名的标注。

重新梳理了一下,MarshalJSON实现的问题不在于用了map和硬编码结构体json key,而是MarshalJSON广泛用于了「检查必填参数是否填入」这个函数名职责以外的事情,导致了大量MarshalJSON的重复编码,同时无视了结构体注解。

检查必填参数这个事情感觉可以和参数加解密一样,用一个注解+单独的检查方法完成。

@lostpg

This comment was marked as outdated.

@lostpg lostpg changed the title 结构体 MarshalJSON 实现方法的疑惑 结构体 MarshalJSON 实现中无视了结构体注解的标注 Apr 16, 2023
@lostpg lostpg changed the title 结构体 MarshalJSON 实现中无视了结构体注解的标注 结构体 MarshalJSON 实现做了大量职责之外的事 Apr 17, 2023
@EmmetZC EmmetZC added the enhancement New feature or request label Apr 19, 2023
@xy-peng
Copy link
Contributor

xy-peng commented Apr 19, 2023

感谢 @lostpg ,是个很好的建议。当初设计时,我们对这一点有过讨论。因为这部分代码是自动生成的,所以我们没有纠结重复编码。

但是目前的设计确实违背了单一职责的原则。我们再讨论下。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants