我有一个这样的原型:
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
}
现在我必须添加另一个字段,所以它看起来像这样:
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
optional string phone = 4;
}
如果我现在反序列化使用旧版本序列化的人将唯一的区别是hasPhone()将始终为false?因此,假设将为所有新人设置手机,我可以通过hasPhone为真/假来实际区分它是旧版/新版proto吗?
答案 0 :(得分:2)
是的,这是正确的。
您还可以使用getPhone()
并检查字符串是否为空,因为空字符串是字符串类型字段的默认值。