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

无法复制某些字段 #129

Closed
tangqiu0205 opened this issue Feb 28, 2022 · 4 comments
Closed

无法复制某些字段 #129

tangqiu0205 opened this issue Feb 28, 2022 · 4 comments
Assignees

Comments

@tangqiu0205
Copy link

tangqiu0205 commented Feb 28, 2022

Your Question

源:
image

目标:
image

结果:
image

Id没有正确复制进去.
另外如何把time.Time 复制时转换为timestamppb.Timestamp

Expected answer

@tangqiu0205 tangqiu0205 changed the title 无法复制某个字段 无法复制某些字段 Feb 28, 2022
@pupilji
Copy link

pupilji commented Feb 28, 2022

  1. 没有复制成功的原因应该是:
    AlbumAlbumParamChildIdAlbumAlbumsChildID 字段名不同。
    可以参考 usage 中的 EmployeCode 字段
  2. 类型转换可以参考 usage DoubleAge 字段,在一个 func 中转换类型

@tangqiu0205
Copy link
Author

1: 结构体加了copier tag, 可以正常复制, 我有个小疑惑, 复制字段不是通过json这个tag 匹配的吗
2:如果是两个同名字段但类型不一致, 似乎不能使用 func DoubleAge 这种形式.

@pupilji
Copy link

pupilji commented Mar 1, 2022

@ZhangHu1225

  1. 我理解 json 这个 tag 是用来解析 json 的,现在你需要用 copier.Copy() ,所以肯定需要用 copier 规定的 tag
  2. 可以看到 usage 中的 EmployeCode 字段,它和 fromto 中对应字段名是不同的,你也可以试下用一个与这个名字不同名的方法转换,用 copier 的 tag 让复制走这个方法

@jinzhu jinzhu closed this as completed Mar 1, 2022
@zedisdog
Copy link

这里有个问题 A是grpc的结构体(自动生成的) B是gorm的结构体(自动生成的) 当是这样一种情况的时候 而我又不想改生成的代码 就比较尴尬了

Wenpiner added a commit to Wenpiner/copier that referenced this issue Apr 1, 2024
Custom Tag function The default json is effective in proto [jinzhu#129](jinzhu#129 (comment)), providing a more flexible tagging method. This feature has been thoroughly tested and proven in local environments.
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

No branches or pull requests

4 participants