Skip to content

Conversation

dddyiersan
Copy link

平常使用gin框架开发,gin内置ShouldBindJSON和ShouldBindQuery方法。使用它们可以很方便的绑定标签带有json标签和 form标签的字段,但proto自动生成结构体只有json标签,没有form标签,在开发的时候很不方便,所以希望在生成json标签时也生成form标签。

在开发时,有些字段虽然存在有效值,但是在序列化时希望隐藏它,使用json选项json_name="-"实现这个逻辑,这次提交没有增加字段,对原有逻辑的影响约等于无,但是开发便捷度却大大增加了。
同理有些字段虽然是零值,但是在序列化时不希望被隐藏,使用json_name="MUST",来实现这个功能。

When developing with the Gin framework, the built-in ShouldBindJSON and ShouldBindQuery methods are commonly used. These methods conveniently bind fields with JSON and form tags. However, Proto-generated structs only include JSON tags and lack form tags, making development less efficient. The goal is to generate form tags alongside JSON tags during generation.

During development, some fields may contain valid values but should be hidden during serialization. This is achieved using the json_name="-" option. This commit does not add new fields, so the impact on existing logic is negligible, while significantly improving development convenience.

Similarly, some fields might be zero-valued but should not be hidden during serialization. This functionality is implemented via json_name="MUST".

This ensures both JSON and form tags are generated for better compatibility with Gin's binding methods.

Copy link

google-cla bot commented Jul 19, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@puellanivis
Copy link

Thank you for your interest into contributing to this project.

Unfortunately this project does not accept GitHub pull requests as the source-of-truth for this project is hosted at https://go.googlesource.com/protobuf. This project page on GitHub is a mirror of that other repository.

If you would like to contribute to this project, please follow the contribution guidelines for instructions on how to send a change. If the change you'd like to make is more substantial or introduces any new features, then it should first be discussed on the issue tracker.

Comment on lines +759 to +763
if field.Desc.JSONName() == "-" {
return "-"
} else if field.Desc.JSONName() == "MUST" {
return string(field.Desc.Name())
}

Choose a reason for hiding this comment

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

I believe such a change of semantics would result in this package having incompatible behavior with other language’s protobuf.

Since protocolbuffers are a multi-/intra-language encoding/decoding format, we cannot unilaterally produce such features as you seem to be designing here.

This will need to be implemented by either its own protobuf extension, or seeking to get this semantic accepted into the wider protobuf ecosystem. Until the later is met, this package would not be considering adding this feature.

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