协议缓冲区 - 唯一编号标签 - 澄清? [英] Protocol buffers - unique numbered tag - clarification?

查看:26
本文介绍了协议缓冲区 - 唯一编号标签 - 澄清?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用协议缓冲区,一切正常.除了我不明白的事实 - 为什么我需要 proto 文件中的编号标签:

message SearchRequest {所需的字符串查询 = 1;可选的 int32 page_number = 2;可选的 int32 result_per_page = 3;}

当然,我已经阅读了文档:

<块引用>

如您所见,消息定义中的每个字段都有一个唯一的编号标签.这些标签用于标识您在消息二进制格式,一旦您的消息不应更改类型正在使用中.

我不明白如果我改变它会有什么不同.(我将创建一个新的 proto 并编译它 - 那么它为什么要关心?)

另一篇文章指出: <块引用>

原型定义中的编号字段消除了对版本的需求检查这是明确说明的动机之一Protocol Buffers 的设计和实现.作为开发商文件指出,该协议的设计部分是为了避免丑陋的代码"像这样检查协议版本:

if (version == 3) {...} else if (version > 4) {如果(版本 == 5){...}...}

问题

是我个人还是完全不清楚?

让我换个方式问:

如果我有一个像上面文件一样的proto文件,然后我把它改成:

message SearchRequest {所需的字符串查询 = 3;//反转顺序可选的 int32 page_number = 2;可选的 int32 result_per_page = 1;}

它在乎什么?我重新编译并添加了文件(上周我已经做了多次).

我错过了什么?您能否为这个编号的标签提供人与人之间的解释?

解决方案

编号标签用于在序列化和反序列化数据时匹配字段.

显然,如果您更改编号方案,并将此更改应用于序列化器和解串器,则没有问题.

考虑一下,如果你用第一个编号方案保存数据,并用第二个编号方案加载它,它会尝试将 query 加载到 result_per_page 中,反序列化会可能会失败.

现在,为什么这很有用?假设您需要在架构已使用很久之后向数据添加另一个字段:

message SearchRequest {所需的字符串查询 = 1;可选的 int32 page_number = 2;可选的 int32 result_per_page = 3;可选的 int32 new_data = 4;}

因为你明确地给它一个数字,你的反序列化器仍然能够加载使用编号方案序列化的数据,忽略不存在数据的反序列化.

I'm using protocol buffers and everything is working fine. except that the fact that I don't understand - why do I need the numbered tags in the proto file :

message SearchRequest {
  required string query = 1;
  optional int32 page_number = 2;
  optional int32 result_per_page = 3;
}

Sure I've read the docs :

As you can see, each field in the message definition has a unique numbered tag. These tags are used to identify your fields in the message binary format, and should not be changed once your message type is in use.

I didn't understand what difference does it make if I change it . ( I will create a new proto and compile it - so why does it care ?)

Another article states that :

Numbered fields in proto definitions obviate the need for version checks which is one of the explicitly stated motivations for the design and implementation of Protocol Buffers. As the developer documentation states, the protocol was designed in part to avoid "ugly code" like this for checking protocol versions:

if (version == 3) {
  ...
} else if (version > 4) {
  if (version == 5) {
    ...
  }
  ...
}

Question

Is it just me or it is completely unclear ?

let me ask it in a different way :

If I have a proto file like the above file , and then I change it to :

message SearchRequest {
  required string query = 3; //reversed order
  optional int32 page_number = 2;
  optional int32 result_per_page = 1;
}

What does it care ? I re-compile and add the file ( i've done it multiple times in the last week).

what am I missing ? can you please supply a human-to human explanation for this numbered tags ?

解决方案

The numbered tags are used to match fields when serializing and deserializing the data.

Obviously, if you change the numbering scheme, and apply this change to both serializer and deserializer, there is no issue.

Consider though, if you saved data with the first numbering scheme, and loaded it with the second one, it would try to load query into result_per_page, and deserialization would likely fail.

Now, why is this useful? Let's say you need to add another field to your data, long after the schema is already in use:

message SearchRequest {
  required string query = 1;
  optional int32 page_number = 2;
  optional int32 result_per_page = 3;
  optional int32 new_data = 4;
}

Because you explicitly give it a number, your deserializer is still able to load data serialized with the old numbering scheme, ignoring deserialization of non-existent data.

这篇关于协议缓冲区 - 唯一编号标签 - 澄清?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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