我是靠谱客的博主 忧心书本,这篇文章主要介绍Protobuf(二)proto3语法格式,现在分享给大家,希望可以做个参考。

.proto文件有两种语法标准:proto2和proto3,我们以proto3为例,其语法格式如下:

复制代码
1
2
3
4
message <message_name> { <filed_rule> <filed_type> <filed_name> = <field_number> 规则 类型 名称 编号 }
  • message_name: 同一个pkg内,必须唯一
  • filed_rule: 可以没有, 常用的有repeated, oneof
  • filed_type: 数据类型, protobuf定义的数据类型, 生产代码的会映射成对应语言的数据类型
  • filed_name: 字段名称, 同一个message 内必须唯一
  • field_number: 字段的编号, 序列化成二进制数据时的字段编号

2.1.1、字段规则
 

2.1.2、字段类型

基础类型

参考官方文档(Language Guide (proto3)  |  Protocol Buffers  |  Google Developers),我这里只保留了我常用编程语言的字段类型

.proto TypeNotesC++ TypePython Type[3]C# Type
doubledoublefloatdouble
floatfloatfloatfloat
int32Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead.int32intint
int64Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead.int64int/long[4]long
uint32Uses variable-length encoding.uint32int/long[4]uint
uint64Uses variable-length encoding.uint64int/long[4]ulong
sint32Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s.int32intint
sint64Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s.int64int/long[4]long
fixed32Always four bytes. More efficient than uint32 if values are often greater than 228.uint32int/long[4]uint
fixed64Always eight bytes. More efficient than uint64 if values are often greater than 256.uint64int/long[4]ulong
sfixed32Always four bytes.int32intint
sfixed64Always eight bytes.int64int/long[4]long
boolboolboolbool
stringA string must always contain UTF-8 encoded or 7-bit ASCII text, and cannot be longer than 232.stringstr/unicode[5]string
bytesMay contain any arbitrary sequence of bytes no longer than 232.stringstr (Python 2)
bytes (Python 3)
ByteString

枚举类型

语法如下:

复制代码
1
2
3
enum <enum_name> { <element_name> = <element_number> }
  • enum_name: 枚举名称
  • element_name: pkg内全局唯一, 很重要
  • element_number: 必须从0开始, 0表示类型的默认值, 32-bit integer

如下示例,Corpus就是我们定义的一个枚举类型。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
message SearchRequest { string query = 1; int32 page_number = 2; int32 result_per_page = 3; enum Corpus { UNIVERSAL = 0; WEB = 1; IMAGES = 2; LOCAL = 3; NEWS = 4; PRODUCTS = 5; VIDEO = 6; } Corpus corpus = 4; }

如果需要将不同的枚举常量映射到相同的值。将allow_alias选项设置为true即可。(最好不要这么使用)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
message MyMessage1 { enum EnumAllowingAlias { option allow_alias = true; UNKNOWN = 0; STARTED = 1; RUNNING = 1; } } message MyMessage2 { enum EnumNotAllowingAlias { UNKNOWN = 0; STARTED = 1; // RUNNING = 1; // Uncommenting this line will cause a compile error inside Google and a warning message outside. } }

2.1.3、字段编号
 

2.1.4、综合示例

最后

以上就是忧心书本最近收集整理的关于Protobuf(二)proto3语法格式的全部内容,更多相关Protobuf(二)proto3语法格式内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(70)

评论列表共有 0 条评论

立即
投稿
返回
顶部