将原型字段更改为“重复”时,我真的需要更改关联的数字吗?

When changing a proto field to `repeated` do I actually need to change the associated number?

假设我有一条消息

message Something {
  int32 foo = 1;
  int32 bar = 2;
  string baz = 3;
}

我想将 baz 更改为 repeated string。我真的必须更改号码吗? (例如到 4 并弃用 3)。

我和一个朋友正在争论这个问题。他在 protobuffs 方面更有经验(我是新手),但根据我对 the docs 的阅读,我实际上不明白为什么有必要。

虽然文档没有直接解决我所看到的问题,但我的推理如下:

以上可能会序列化为类似

的内容
0001(32bits)0011(utf-8bits)0010(32bits)

反序列化基本上会将其读取为

所以根据文档添加 repeated:

this field can be repeated any number of times (including zero) in a well-formed message

这告诉我当遇到 3 时我们读取的实际位数不会有任何不同,我们只是可能遇到多个 3。因此,它是序列化和反序列化时的验证,但实际上不是编码的一部分。

这个逻辑对吗?我可以继续使用同一个号码吗?还是我遗漏了什么?

来自语言指南:

optional is compatible with repeated. Given serialized data of a repeated field as input, clients that expect this field to be optional will take the last input value if it's a primitive type field or merge all input elements if it's a message type field.

(proto2: https://developers.google.com/protocol-buffers/docs/proto#updating )

这对 proto2 有效,我想说即使没有指定,这仍然适用于 proto3。行为应该是遗留代码将处理列表并在内存中保留最后读取的值。

将字段从可选更改为重复时,我也会注意 API 更改。这可能是你的朋友想要强调的。