Protocol Buffers (protobuf) v3.0.0-alpha-2 中的可选字段和约束
Optional fields and constraints in Protocol Buffers (protobuf) v3.0.0-alpha-2
我目前正在使用 Google Protocol Buffers.
的 v3.0.0-alpha-2
据我了解,v3 删除了 required
关键字,extensions
字段的关键字和默认值以简化原型语言。
我不理解的是proto3中optional
关键字的含义。
示例:
syntax = "proto3";
package fw.example;
message ExampleMessage {
optional string optional_string = 1;
string normal_string = 2;
}
问题:optional_string
和normal_string
有什么区别
除了名称和标签?
我已经阅读了以下资源(它们似乎是唯一公开的
protobuf v3 可用):
但他们甚至没有提到 optional
关键字。
optional
在 proto3 中是否已过时,因为字段始终是可选的?
- 如果
required
消失了,如何使用 proto3 强制要求字段?
似乎在 proto3 中,人们无法再区分未设置的字段和
客户端设置为(隐式)默认值的字段。
将每个 proto3 消息包装在特定语言中的最佳做法是 class?
我正在使用 C++,我需要确保设置了特定的字段。看起来
现在必须在特定于语言的源代码中手动完成验证,在
对比 proto2.
有人能启发我吗,对a应用约束的最佳方法是什么
proto3 消息但允许方案演变?目前我认为一个新的 API
必须围绕 proto3 消息编写,以便客户端不处理
直接使用 proto3 生成的代码,但使用自定义 API 代码。就是它
正确吗?
也许有人可以给我一个具体的例子来讨论一下。
我很困惑,因为以下内容在 v3 的发行说明中有说明:
We recommend that new Protocol Buffers users use proto3. However, we do not
generally recommend that existing users migrate from proto2 from proto3 due to
API incompatibility, and we will continue to support proto2 for a long time.
如果 proto3 是可行的方法,为什么事情会这么复杂?在我看来,我现在确实需要编写比 proto2 更多的代码。
proto3 documentation(第 "Using proto2 Message Types" 节)说您可以使用 'import' 关键字导入 proto2 .proto 文件,反之亦然。这意味着 v2 和 v3 .proto 语法是兼容的,这意味着仍然支持可选和必需的。
我说试一试,看看会发生什么。
我的推理如下:
既然required
已经过时,那么一切都是optional
。因此没有理由使用显式关键字。
language guide 中的以下段落说明了如果未设置值是如何初始化的:
Note that for scalar message fields, once a message is parsed there's
no way of telling whether a field was explicitly set to the default
value (for example whether a boolean was set to false) or just not set
at all: you should bear this in mind when defining your message types.
所以optional
真正的意思是"if you didn't set it explicitly, we'll set it to default value for you!"
请注意,这允许他们进行一些不错的优化(如果设置为默认值,则不包括线路上的值):
For example, don't have a boolean that switches on some behaviour when
set to false if you don't want that behaviour to also happen by
default. Also note that if a scalar message field is set to its
default, the value will not be serialized on the wire.
proto3 的最终版本没有 optional
关键字。在 proto3 中,.
我目前正在使用 Google Protocol Buffers.
的 v3.0.0-alpha-2据我了解,v3 删除了 required
关键字,extensions
字段的关键字和默认值以简化原型语言。
我不理解的是proto3中optional
关键字的含义。
示例:
syntax = "proto3";
package fw.example;
message ExampleMessage {
optional string optional_string = 1;
string normal_string = 2;
}
问题:optional_string
和normal_string
有什么区别
除了名称和标签?
我已经阅读了以下资源(它们似乎是唯一公开的 protobuf v3 可用):
但他们甚至没有提到 optional
关键字。
optional
在 proto3 中是否已过时,因为字段始终是可选的?- 如果
required
消失了,如何使用 proto3 强制要求字段?
似乎在 proto3 中,人们无法再区分未设置的字段和 客户端设置为(隐式)默认值的字段。
将每个 proto3 消息包装在特定语言中的最佳做法是 class? 我正在使用 C++,我需要确保设置了特定的字段。看起来 现在必须在特定于语言的源代码中手动完成验证,在 对比 proto2.
有人能启发我吗,对a应用约束的最佳方法是什么 proto3 消息但允许方案演变?目前我认为一个新的 API 必须围绕 proto3 消息编写,以便客户端不处理 直接使用 proto3 生成的代码,但使用自定义 API 代码。就是它 正确吗?
也许有人可以给我一个具体的例子来讨论一下。
我很困惑,因为以下内容在 v3 的发行说明中有说明:
We recommend that new Protocol Buffers users use proto3. However, we do not generally recommend that existing users migrate from proto2 from proto3 due to API incompatibility, and we will continue to support proto2 for a long time.
如果 proto3 是可行的方法,为什么事情会这么复杂?在我看来,我现在确实需要编写比 proto2 更多的代码。
proto3 documentation(第 "Using proto2 Message Types" 节)说您可以使用 'import' 关键字导入 proto2 .proto 文件,反之亦然。这意味着 v2 和 v3 .proto 语法是兼容的,这意味着仍然支持可选和必需的。
我说试一试,看看会发生什么。
我的推理如下:
既然required
已经过时,那么一切都是optional
。因此没有理由使用显式关键字。
language guide 中的以下段落说明了如果未设置值是如何初始化的:
Note that for scalar message fields, once a message is parsed there's no way of telling whether a field was explicitly set to the default value (for example whether a boolean was set to false) or just not set at all: you should bear this in mind when defining your message types.
所以optional
真正的意思是"if you didn't set it explicitly, we'll set it to default value for you!"
请注意,这允许他们进行一些不错的优化(如果设置为默认值,则不包括线路上的值):
For example, don't have a boolean that switches on some behaviour when set to false if you don't want that behaviour to also happen by default. Also note that if a scalar message field is set to its default, the value will not be serialized on the wire.
proto3 的最终版本没有 optional
关键字。在 proto3 中,