golang / protobuf

Go support for Google's protocol buffers
BSD 3-Clause "New" or "Revised" License
9.66k stars 1.58k forks source link

Can protobuf provide tags for customized message generated fields? #1625

Closed cold-bin closed 2 weeks ago

cold-bin commented 2 weeks ago

I want to use proto message to generate a structure as my http request data model or return data model. In this way, I can easily reuse code without creating redundancy. Therefore, I want to decide that the structure fields generated by proto message should have the ability to customize tags.

like this proto defined:

message Demo {
  int64 demo = 1 [adding_tag = 'json:"-'];
}

parse the proto file and add the my defined adding_tag to generated Demo struct.

puellanivis commented 2 weeks ago

This is more or less being discussed right now over at https://github.com/golang/protobuf/issues/1619 but has also been discussed before.

The TL;DR of this is that it is unlikely we would ever add such a feature, first the json tags we have are considered to have been a mistake. (We implemented a feature before the larger protobuf community settled on what the proper JSON encoding should be, and when that JSON encoding was defined, our implementation was not compliant.) Additionally, protobuf is a language-independant exchange format, and features that meet one and only one language are undesirable.

I’ll be closing this issue, but if you wish to continue your request, it would be better to do it in the linked issue above, which has already been discussing it.

cold-bin commented 2 weeks ago

thank you for your reply.