阅读protobuf文档

This commit is contained in:
asahi
2025-02-25 12:41:11 +08:00
parent aa006e2dab
commit c3bf955058

View File

@@ -3,6 +3,10 @@
- [定义message type](#定义message-type)
- [Assign Field Numbers](#assign-field-numbers)
- [重复使用filed number的后果](#重复使用filed-number的后果)
- [指定字段基数](#指定字段基数)
- [Singular](#singular)
- [repeated](#repeated)
- [map](#map)
# protobuf
@@ -42,4 +46,28 @@ filed number不应该被重用。
### 重复使用filed number的后果
如果重复使用field number将会造成解码wire-format message的二义性。
protobuf wire format
> 对于protobuf wire format,其在编码和解码过程中,`fields的定义`必须一致。
field number被限制为`29bit`故而field number的最大值为`536870911`
### 指定字段基数
在protobuf协议中field可以为如下的集中类型
#### Singular
在proto3中有两种singular field
- `optional`(推荐使用): 一个optional field可能有如下两种状态
- 如果optional field值被设置那么其将会被序列化到`wire`
- 如果optional field值未被设置那么该field将会返回一个默认值并且其不会被序列化到wire中
- `implict`(不推荐使用):一个隐式字段没有显式基数标签,并且行为如下:
- 如果field为一个message type那么其行为和`optional`相同
- 如果field不是message那么其有两种状态
- 如果field被设置为非默认值non-zero其会被序列化到wire中
- 如果field被设置为zero value那么其不会被序列化到wire中
> 相比于`implict`,更推荐使用`optional`,使用`optional`能更好与proto2相兼容
#### repeated
代表该field可以在消息中出现0次或多次消息出现的顺序也将被维护
#### map
代表field为成对的键值对