4 ms·
is this for the protoc generated code? otherwise, could you not define different struct tags for the different serialization types? type Person { Name
by zetetic 7y ago
is this for the protoc generated code? otherwise, could you not define different struct tags for the different serialization types?
type Person {
Name string `protobuf:"name",json:"given_name"`
}
- seiferteric 7y agoYes it is for the protoc generated code. It would be nice if the protobuf spec allowed for something like tags as well. The main difference is the json coming in uses hyphens, whereas protobuf<->json only allows for underscore or camelCase field names.
- q3k 7y ago> Yes it is for the protoc generated code. It would be nice if the protobuf spec allowed for something like tags as well. Have you seen Protobuf options [1]? They allow for a tag-like-behaviour, but are strongly typed. It's notably used by gogoproto [2] to allow specifying custom field types or names [3] when generating Go stubs. [1] - https://developers.google.com/protocol-buffers/docs/proto#customoptions https://developers.google.com/protocol-buffers/docs/proto#cu... [2] - https://github.com/gogo/protobuf https://github.com/gogo/protobuf [3] - https://godoc.org/github.com/gogo/protobuf/gogoproto https://godoc.org/github.com/gogo/protobuf/gogoproto
- seiferteric 7y agoThanks, I have looked at options. I think it could work, but I think it would require writing a custom marshal/unmarshal function for each protobuf struct, even if they are essentially the same for each one. I will have to take a closer look at the links though.
- seiferteric 7y agogogoproto worked for me, using the moretags extension, thanks for the links!
- everybodyknows 7y agoCode-gen with help of the 'template' library might be a way out of the code repetition.
- LukeShu 7y agoIf you use gogoproto, it includes an extension to the protobuf spec, to override json tag for the emitted Go. https://github.com/gogo/protobuf/blob/master/extensions.md https://github.com/gogo/protobuf/blob/master/extensions.md Use it like: import "github.com/gogo/protobuf/gogoproto/gogo.proto"; ... Type myField = 1 [(gogoproto.jsontag) = "my-field"];