has_field() 方法如何与 protobuf 中的默认值相关? [英] how do has_field() methods relate to default values in protobuf?

查看:85
本文介绍了has_field() 方法如何与 protobuf 中的默认值相关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定 默认值 和在各种编程接口中声明的 has_foo() 方法之间的关系.特别是,我试图确定在什么情况下(如果有的话)您可以区分"显式设置为默认值的字段和未设置的值.

I'm trying to determine the relationship between default values and the has_foo() methods that are declared in various programmatic interfaces. In particular, I'm trying to determine under what circumstances (if any) you can "tell the difference" between a field explicitly set to the default value, and an unset value.

  1. 如果我将一个字段(例如Bar.foo")显式设置为其默认值(例如,零),那么 Bar::has_foo() 是否保证对该数据结构返回 true?(从快速检查来看,这对于 C++ 生成的代码似乎是正确的,但这并不意味着它是有保证的.)如果这是真的,那么就可以区分显式设置的默认值和未设置的 prior序列化.

如果我明确地将一个字段设置为其默认值(例如,零),然后序列化该对象并通过网络发送它,该值是否会被发送?如果不是,那么显然任何接收此对象的代码都无法区分显式设置的默认值和未设置的值.即,在序列化之后将无法区分这两种情况 -- Bar::has_foo() 在这两种情况下都将返回 false.

If I explicitly set a field to its default value (e.g., zero), and then serialize that object and send it over the wire, will the value be sent or not? If it is not, then clearly any code that receives this object can't distinguish between an explicitly set default value and an unset value. I.e., it won't be possible to distinguish these two cases after serialization -- Bar::has_foo() will return false in both cases.

如果无法区分,如果我想编码一个可为空"的可选值,推荐的编码 protobuf 字段的技术是什么?想到了几个选项,但似乎都不是很好:(a) 添加一个额外的布尔字段来记录该字段是否已设置,或者 (b) 使用重复"字段,即使我在语义上想要一个可选字段——通过这种方式,我可以区分无值(长度为零的列表)或设置的值(长度为一的列表).

If it's not possible to tell the difference, what is the recommended technique for encoding a protobuf field if I want to encode a "nullable" optional value? A couple options come to mind, but neither seem great: (a) add an extra boolean field that records whether the field is set or not, or (b) use a "repeated" field even though I semantically want an optional field -- this way I can tell the difference between no value (length-zero list) or a set value (length-one list).

推荐答案

以下适用于 'proto2' 语法,不适用于 'proto3' :

The following applies for 'proto2' syntax, not 'proto3' :

是否设置字段的概念是 Protobuf 的核心功能.如果你将一个字段设置为一个值(任何值),那么相应的has_xxx 方法必须返回true,否则你的API 就会出现错误.

The notion of a field being set or not is a core feature of Protobuf. If you set a field to a value (any value), then the corresponding has_xxx method must return true, otherwise you have a bug in the API.

如果您不设置字段然后序列化消息,则不会为该字段发送任何值.接收方将解析消息,发现包含哪些值,并设置相应的has_xxx"值.

If you do not set a field and then serialize the message, no value is sent for that field. The receiving side will parse the message, discover which values where included, and set the corresponding "has_xxx" values.

这里记录了如何以有线格式准确实现这一点:http://code.google.com/apis/protocolbuffers/docs/encoding.html.简而言之,消息被编码为一系列键值对,只有明确设置的字段才会包含在编码消息中.

Exactly how this is implemented in the wire-format is documented here: http://code.google.com/apis/protocolbuffers/docs/encoding.html. The short version is that message are encoded as a sequence of key-value pairs, and only fields which are explicitly set are included in the encoded message.

默认值仅在您尝试读取未设置字段时起作用.

Default values only come into play when you attempt to read an unset field.

这篇关于has_field() 方法如何与 protobuf 中的默认值相关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆