protobuf 3 单数与可选有何不同

How is protobuf 3 singular different than optional

查看 proto3 参考资料:

https://developers.google.com/protocol-buffers/docs/proto3#simple

singular 是这样说的:

singular: a well-formed message can have zero or one of this field (but not more than one).

我不清楚这与 optional 有何不同。 singular 只是一种明确说明某物是 optional 的方式(现在对于 proto3 来说是隐式的)吗?或者还有其他我想念的东西吗?

谢谢。

可选的是 proto2 语法。单数是 proto3 语法。

在proto3中,singular是默认规则。由于今天需要改进文档,因此有一个未解决的问题:google/protobuf#3457.

另见 google/protobuf#2497 why messge type remove 'required,optional'?, and also haberman's comment on GoogleCloudPlatform/google-cloud-python#1402:

I think the question is: what are you trying to do? Why is it relevant to you whether a field is set or not and what do you intend to do with this information?

In proto3, field presence for scalar fields simply doesn't exist. Your mental model for proto3 should be that it's a C++ or Go struct. For integers and strings, there is no such thing as being set or not, it always has a value. For submessages, it's a pointer to the submessage instance which can be NULL, that's why you can test presence for it.