protobuf 关键字同时使用

本文探讨了如何在protobuf中创建带有重复装饰的自定义选项,通过实例展示了如何利用message类型来实现这一功能,提供了解决该限制的工作绕过方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

refs:

https://cxwangyi.wordpress.com/2012/04/13/mastering-google-protocol-buffer-custom-option-with-repeated-fields/


使用protobuf时 发现repeat和optional不能同时使用,参考refs后有了workaround方法。


We encountered a question about Google protocol buffer: could we create a custom option which has a repeated decoration?

The protocol buffer compiler, protoc, answers: no.

However, it is OK to create an option with a message type, and the message could contain a repeated field. Here follows an example program.

The content of learn.proto:

	
import "google/protobuf/descriptor.proto";
 
message Something {
  repeated int32 a = 1;
}
 
extend google.protobuf.FieldOptions {
  optional float my_field_option = 50002;
  optional Something my_another_field_option = 50003;
}
 
message MyMessage {
  optional string my_field = 1
    [(my_field_option) = 1.1];
  optional string my_another_field = 2
    [(my_another_field_option) = { a : [111, 222, 333] } ];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值