diff --git a/protobuf/protobuf.md b/protobuf/protobuf.md index 01bf0a6..cbf6351 100644 --- a/protobuf/protobuf.md +++ b/protobuf/protobuf.md @@ -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 \ No newline at end of file +> 对于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为成对的键值对 +