message Person { string name = 1; int32 id = 2 [(gogoproto.jsontag) = "id"]; // Unique ID number for this person. string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { string number = 1; PhoneType type = 2; } repeated PhoneNumber phones = 4; google.protobuf.Timestamp last_updated = 5; }通过 protoc 和 plugin 生成后的 Go 代码:
type Person struct { Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` Id int32 `protobuf:"varint,2,opt,name=id,proto3" json:"id"` Email string `protobuf:"bytes,3,opt,name=email,proto3" json:"email,omitempty"` Phones []*Person_PhoneNumber `protobuf:"bytes,4,rep,name=phones,proto3" json:"phones,omitempty"` LastUpdated *timestamppb.Timestamp `protobuf:"bytes,5,opt,name=last_updated,json=lastUpdated,proto3" json:"last_updated,omitempty"` }生成后的结构体中的 Id 字段,json tag 值为 id,没有了 omitempty。可以解决很多同学在与前端对接时的一些烦恼。
// file: test.proto syntax = "proto3"; package pb; option go_package = "/pb"; message IP { // @gotags: valid:"ip" string Address = 1; // Or: string MAC = 2; // @gotags: validate:"omitempty" }通过 protoc 和 plugin 生成后的 Go 代码:
type IP struct { // @gotags: valid:"ip" Address string `protobuf:"bytes,1,opt,name=Address,json=address" json:"Address,omitempty" valid:"ip"` }可以明确看懂 json tag 多了 valid:"ip",符合我们在 proto 文件中声明的注解诉求。
通过两篇文章梳理下来,对于整个前因后果和功能特性,我们都有了相对全面的学习和了解了。官方 golang/protobuf 固然有自己的原则,社区也有自己的需求。开源的项目过大了,长年累月下来会难维护。可能像 protoc-go-inject-tag 这种较为单一职责的开源库,会活的更好,也会更好找到新的人衔接。也是一个不错的方向。
参考资料
[1]gogo.proto: https://github.com/gogo/protobuf/blob/master/gogoproto/gogo.proto
[2]More Canonical Go Structures: https://github.com/gogo/protobuf/blob/master/extensions.md#more-canonical-go-structures
[3]protoc-go-inject-tag: https://github.com/favadi/protoc-go-inject-tag