Enhancing Proto-Generated Structs for Gin Framework: Auto-Generating Form Tags Alongside JSON Tags #60
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
平常使用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.