protobuf 的全局唯一选项字段编号
Globally unique option field numbers for protobufs
在 protobuf 文档 (https://developers.google.com/protocol-buffers/docs/proto#customoptions) 中,它说明了自定义选项:
One last thing: Since custom options are extensions, they must be
assigned field numbers like any other field or extension. In the
examples above, we have used field numbers in the range 50000-99999.
This range is reserved for internal use within individual
organizations, so you can use numbers in this range freely for
in-house applications. If you intend to use custom options in public
applications, however, then it is important that you make sure that
your field numbers are globally unique. To obtain globally unique
field numbers, please send a request to
protobuf-global-extension-registry@google.com. Simply provide your
project name (e.g. Object-C plugin) and your project website (if
available). Usually you only need one extension number.
为什么 public 应用程序的选项字段编号必须是全局唯一的?碰撞在哪些方面会成为问题?
基本上,因为你不知道你得到的数据是否正确。
protobuf 二进制有线格式 仅 存储字段编号和有效负载(它本身,对于复杂类型,仅字段编号和子有效负载)。没有名称数据。所以:当您存储和检索扩展字段时,您所说的只是 "fetch field {field number}, interpret it as {type}"。如果两个不同的系统使用 相同的字段编号 扩展了 相同的数据 ,那么您将无法知道您的数据是否重新提取的格式 实际上。
通常这不是问题 - 因为在同一数据上很少会出现这样的冲突;但自定义选项不同!我是图书馆作者;我可能想通过扩展(比如)MessageOptions
添加一个我的模式解析工具可以识别的自定义选项。 MessageOptions
是 DescriptorProto
的扩展点,也就是说:这就是 option (foo) = "bar";
在 message
.
中的内容
为此,我需要为 foo
分配一个号码。我任意选择5000
(MessageOptions
定义了extensions 1000 to max;
,这样就好了)。一切都很好。我的工具有效。
我不知道,另一个 库作者选择了做类似的事情并且也 使用了 5000。编译架构后 (通过 protoc
或类似的), 我只有数字 。如果我从字段 5000 中请求数据,我不知道我得到的是我的延期还是其他延期。失去了意义。好吧,一键 我也可以检查 FileDescriptorProto
上的 dependency
列表,但是......那是偶然的。
我不知道字段5000
中值1
的存在是否是:
option (.mystuff.someext) = 1;
对
option (.anotherlib.whatever) = -1; // stored as sint32
对
option (.yetanother.library.option) = true;
如果这些分机号都为 5000,则它们在线路上完全相同。
在 protobuf 文档 (https://developers.google.com/protocol-buffers/docs/proto#customoptions) 中,它说明了自定义选项:
One last thing: Since custom options are extensions, they must be assigned field numbers like any other field or extension. In the examples above, we have used field numbers in the range 50000-99999. This range is reserved for internal use within individual organizations, so you can use numbers in this range freely for in-house applications. If you intend to use custom options in public applications, however, then it is important that you make sure that your field numbers are globally unique. To obtain globally unique field numbers, please send a request to protobuf-global-extension-registry@google.com. Simply provide your project name (e.g. Object-C plugin) and your project website (if available). Usually you only need one extension number.
为什么 public 应用程序的选项字段编号必须是全局唯一的?碰撞在哪些方面会成为问题?
基本上,因为你不知道你得到的数据是否正确。
protobuf 二进制有线格式 仅 存储字段编号和有效负载(它本身,对于复杂类型,仅字段编号和子有效负载)。没有名称数据。所以:当您存储和检索扩展字段时,您所说的只是 "fetch field {field number}, interpret it as {type}"。如果两个不同的系统使用 相同的字段编号 扩展了 相同的数据 ,那么您将无法知道您的数据是否重新提取的格式 实际上。
通常这不是问题 - 因为在同一数据上很少会出现这样的冲突;但自定义选项不同!我是图书馆作者;我可能想通过扩展(比如)MessageOptions
添加一个我的模式解析工具可以识别的自定义选项。 MessageOptions
是 DescriptorProto
的扩展点,也就是说:这就是 option (foo) = "bar";
在 message
.
为此,我需要为 foo
分配一个号码。我任意选择5000
(MessageOptions
定义了extensions 1000 to max;
,这样就好了)。一切都很好。我的工具有效。
我不知道,另一个 库作者选择了做类似的事情并且也 使用了 5000。编译架构后 (通过 protoc
或类似的), 我只有数字 。如果我从字段 5000 中请求数据,我不知道我得到的是我的延期还是其他延期。失去了意义。好吧,一键 我也可以检查 FileDescriptorProto
上的 dependency
列表,但是......那是偶然的。
我不知道字段5000
中值1
的存在是否是:
option (.mystuff.someext) = 1;
对
option (.anotherlib.whatever) = -1; // stored as sint32
对
option (.yetanother.library.option) = true;
如果这些分机号都为 5000,则它们在线路上完全相同。