如何编写 ProtoBuf 插件 (一) ?
前言
我们要知道 proto3 和 proto2 的语法,并不是完全兼容的。
具体可查阅官方文档:
如果上述链接无法打开,可以访问这个文档:Overview - 语雀 。
自定义选项
在 proto3 中,常见的实现插件的方式是使用 自定义选项,也就是 extend 标签,其中支持的 extend Options 有:
MethodOptionsServiceOptionsEnumOptionsEnumValueOptionsMessageOptionsFieldOptionsFileOptionsOneofOptionsExtensionRangeOptions
具体写法可参考:
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 {
...
}
大家有实现的思路吗?欢迎留言评论 ~
推荐阅读
- Go - 关于 protoc 工具的小疑惑
- Go - 关于 .proto 文件的小思考
- Go - 使用 sync.WaitGroup 来实现并发操作
- Go - 使用 sync.Map 解决 map 并发安全问题
- Go - 基于逃逸分析来提升程序性能
本作品采用《CC 协议》,转载必须注明作者和本文链接
关于 LearnKu