Go - 如何编写 ProtoBuf 插件 (一) ?
source link: https://segmentfault.com/a/1190000041140136
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
我们要知道 proto3
和 proto2
的语法,并不是完全兼容的。
具体可查阅官方文档:
如果上述链接无法打开,可以访问这个文档:Overview - 语雀 。
自定义选项
在 proto3
中,常见的实现插件的方式是使用 自定义选项
,也就是 extend
标签,其中支持的 extend Options
有:
MethodOptions
ServiceOptions
EnumOptions
EnumValueOptions
MessageOptions
FieldOptions
FileOptions
OneofOptions
ExtensionRangeOptions
具体写法可参考:
import "google/protobuf/descriptor.proto"; extend google.protobuf.MessageOptions { optional string my_option = 51234; } message MyMessage { option (my_option) = "Hello world!"; }
假设,我们的需求场景是这样的:
我们有很多的拦截器,其中不同的 service
可能会使用一个或多个拦截器,不同的 method
也可能会使用一个或多个拦截器,在 helloworld.proto
中
service Greeter{}
支持登录令牌验证rpc SayHello1()
支持IP
白名单限制和记录日志rpc SayHello2()
支持禁止记录日志
// helloworld.proto service Greeter { rpc SayHello1 (HelloRequest) returns (HelloReply) {} rpc SayHello2 (HelloRequest) returns (HelloReply) {} } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }
我们需要在 proto
文件中,定义出 service
使用了哪些拦截器?定义出 method
使用了哪些拦截器?这样 proto
文件就会更加语义化,更加清晰明确,当大家看到定义的文件时,对使用的拦截器一目了然。
如何实现这个功能?
这时,我们就需要用到 MethodOptions
和 ServiceOptions
选项,通过名字大概也能猜到 MethodOptions
是定义方法选项的,ServiceOptions
是定义服务选项的。
extend google.protobuf.MethodOptions { ... } extend google.protobuf.ServiceOptions { ... }
大家有实现的思路吗?欢迎留言评论 ~
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK