【问题标题】:What is the default value for an "other message type" in protobuf 2protobuf 2 中“其他消息类型”的默认值是什么
【发布时间】:2020-06-11 16:52:32
【问题描述】:

我想要一个带有其他消息类型作为字段的原始响应。我希望只有其中一些字段,我想知道如果我不设置这些字段之一,默认值会是什么。

上下文是https://developers.google.com/protocol-buffers/docs/proto#other中列出的“其他消息类型”

message SearchResponse {
  optional Result1 result1 = 1;
  optional Result1 result2 = 2;
}

message Result1 {
  required string url = 1;
  optional string title = 2;
  repeated string snippets = 3;
}

message Result2 {
  required string url2 = 1;
  optional string title2 = 2;
  repeated string snippets2 = 3;
}

例如,如果我设置了 result1 而不是 result2,result2 是 null 还是完全缺失,或者 result2 会以所有字符串的默认值出现?

【问题讨论】:

    标签: protocol-buffers


    【解决方案1】:

    通常缺失,在 proto2 和 proto3 中 - 位可能取决于您的目标平台是否有缺失的概念。

    考虑:消息可以是递归的。 Foo 可以将 Foo 作为字段。如果不被视为失踪,它会立即爆炸。

    【讨论】:

    • 感谢您的回复。目标平台是Java。会为空吗?
    • @JBLevs 这将是我的期望,但是:试试吧!
    猜你喜欢
    • 2021-03-17
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 2014-09-30
    相关资源
    最近更新 更多