应用程序 A 使用了这个原型(prototype)文件
message The_message
{
enum The_enum
{
one = 1;
two = 2;
}
required The_enum the_enum = 1;
}
应用程序 B 使用文件的下一个版本。除了多了一个 enum 字段外,它很相似
three = 3;
如果应用程序 B 使用 three
枚举向应用程序 A 发送消息会怎样?
if you try to provide a different value, the parser will treat it like an unknown field
所以枚举 three
变成了一个未知字段。如果 the_enum
是可选的,那会很好,但它是必填字段。
应用A能否正确解析消息?
枚举不应该以这种方式扩展吗?
最佳答案
我希望解析器的行为方式与遇到缺少 required
字段的消息一样。我希望解析器拒绝该消息。
您链接到的页面是这样说的:
Required Is Forever - You should be very careful about marking fields as required. If at some point you wish to stop writing or sending a required field, it will be problematic to change the field to an optional field – old readers will consider messages without this field to be incomplete and may reject or drop them unintentionally.
现在这不是您所说的情况,但这意味着缺少必需的消息将导致消息不被接受。
如果枚举类型的字段不是必需的
,则文档暗示该字段将被视为未知字段。如果您使用的是支持它的 protobuf API 版本,您可以访问此字段/值,但您会将其视为字节或其他内容,而不是映射的 Java 或 C# enum
类型.
关于java - 如果需要但不在范围内,枚举的值是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8675808/