protobuf 自定义extend

本文详细介绍了protobuf的自定义扩展,包括proto2的extension和proto3的option。proto2的extension通过官方指南和示例进行解析,而proto3不再支持extension,转而使用option。文中重点讲解了proto3的option,列举了不同类型的option,并提供了相应的Go代码示例,展示了如何处理proto3的option值。在Go中处理proto3 option时,需要注意FieldOptions的默认值设定问题,因为proto3不允许显式设置default值。
摘要由CSDN通过智能技术生成

1、proto2  extension

proto2 extensions 官网指导说明:

https://developers.google.com/protocol-buffers/docs/proto#extensions

定义:

https://github.com/golang/protobuf/blob/4846b58453b3708320bdb524f25cc5a1d9cda4d4/internal/testprotos/proto2_proto/test.proto#L266

使用方法:

https://github.com/golang/protobuf/blob/master/proto/extensions_test.go

本文重点说明proto3,但proto3也是在proto2的基础上展开的,在理解以上链接内容可以继续向下阅读。

2、proto3 option

proto3 不支持extension,只支持option, option传值设置:

https://developers.google.com/protocol-buffers/docs/proto3#options

 

具体option可以分为:

FileOptions

MessageOptions

FieldOptions

OneofOptions

EnumOptions

EnumValueOptions

ServiceOptions

MethodOptions

UninterpretedOption

 

其字段定义见:

https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/descriptor.proto

官网也有对应使用测试:

https://github.com/protocolbuffers/protobuf/blob/master/csharp/protos/unittest_custom_options_proto3.proto

3、go代码 处理proto3 option值

test.proto:

syntax = "proto3";


import "google/protobuf/descriptor.proto";
import "google/protobuf/any.proto";


extend google.protobuf.FileOptions {
    uint64 file_opt1 &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值