Java protobuf在根消息和子消息中设置了字段

时间:2019-01-24 06:20:18

标签: java protocol-buffers protobuf-java

我们如何从在Java中嵌套了消息的protobuf构建消息实例。

使用下面描述的协议,方法是设置根目录下的字段和protobuf的嵌套消息中的字段,并返回整个消息的实例。

message Envelope {

  message quantity {
    optional string a = 1;
    optional string b = 2;
  }
  message quality {
    required string c = 1;
  }

  optional string e = 1;
}

在以上消息中,我想通过在根目录中设置字段“ e”并在嵌套消息中将字段设置为“ a”,“ b”,“ quantity”和“ c”中的方式来返回Envelope实例。 “质量”。我该怎么办?

1 个答案:

答案 0 :(得分:0)

在protobuf定义中,您创建了3种消息类型,其中两种嵌套。

This nesting is just about the namespace.要将它们实际包含在Envelop中,您应该编写:

message Envelope {

  message Quantity {
    optional string a = 1;
    optional string b = 2;
  }
  message Quality {
    required string c = 1;
  }

  optional string e = 1;
  optional Quantity quantity = 2;
  optional Quality quality = 3;
}
相关问题