我正在尝试测试一个递归的Protocol Buffers定义(使用Java但可能并不重要)。我正在尝试使用“otherGreetings”(递归结构)的RootType消息的2级深度填充我的消息。我无法获得正确的语法,也无法找到任何样本。我要么最终堆栈溢出,要么我的消息都被卡在同一个“otherGreetings”列表中。有什么想法吗?这是我的信息。
option java_outer_classname = "RootTypeProto";
message RootType{
required string attribute1 = 1;
optional string attribute2 = 2;
optional string attribute3 = 3;
required string attribute4 = 4;
required string element1 = 5;
optional string element2 = 6;
repeated string element3 = 7;
repeated string element4 = 8;
required WorldType world = 9;
optional WorldType alternateWorld = 10;
repeated RootType otherGreetings = 11;
repeated Bar foo = 12;
}
message WorldType{
required string attribute1 = 1;
repeated string element1 = 2;
}
message Bar{
required string element2 = 1;
}
答案 0 :(得分:0)
(在问题编辑中回答。转换为社区维基回答。请参阅Question with no answers, but issue solved in the comments (or extended in chat))
OP写道:更新:写了一个简单的例子来解决我的问题
问题出在一个单独的嵌套对象中,似乎没有正确地遍历树来构建结构。以下是显示它现在正常工作的简单示例:
@Test
public void testWritingReadingProto() throws Exception {
// Build complex obj WorldType
proto.gen.RootTypeProto.WorldType.Builder worldBuilder = proto.gen.RootTypeProto.WorldType
.newBuilder();
worldBuilder.setAttribute1("world-attr1");
worldBuilder.addElement1("world-elem1a");
worldBuilder.addElement1("world-elem1b");
// Build complex obj RootType (level 2)
proto.gen.RootTypeProto.RootType.Builder rootLevel2 = proto.gen.RootTypeProto.RootType
.newBuilder();
rootLevel2.setAttribute1("level2-attr1");
rootLevel2.setAttribute2("level2-attr2");
rootLevel2.setAttribute3("level2-attr3");
rootLevel2.setAttribute4("level2-attr4");
rootLevel2.setElement1("level2-elem1");
rootLevel2.setElement2("level2-elem2");
rootLevel2.setWorld(worldBuilder);
// Build complex obj RootType (level 1)
proto.gen.RootTypeProto.RootType.Builder rootLevel1 = proto.gen.RootTypeProto.RootType
.newBuilder();
rootLevel1.setAttribute1("level1-attr1");
rootLevel1.setAttribute2("level1-attr2");
rootLevel1.setAttribute3("level1-attr3");
rootLevel1.setAttribute4("level1-attr4");
rootLevel1.setElement1("level1-elem1");
rootLevel1.setElement2("level1-elem2");
rootLevel1.setWorld(worldBuilder);
rootLevel1.addOtherGreetings(rootLevel2);
// Build complex msg WorldType
proto.gen.RootTypeProto.RootType.Builder rootBuilder = proto.gen.RootTypeProto.RootType
.newBuilder();
rootBuilder.setAttribute1("attr1");
rootBuilder.setAttribute2("attr2");
rootBuilder.setAttribute3("attr3");
rootBuilder.setAttribute4("attr4");
rootBuilder.setElement1("elem1");
rootBuilder.setElement2("elem2");
// Add complex
rootBuilder.setWorld(worldBuilder);
rootBuilder.addOtherGreetings(rootLevel1);
// Build structure for output
proto.gen.RootTypeProto.RootType root = rootBuilder.build();
// Confirm structure
Assert.assertEquals("attr1", root.getAttribute1());
Assert.assertEquals("attr2", root.getAttribute2());
Assert.assertEquals("attr3", root.getAttribute3());
Assert.assertEquals("attr4", root.getAttribute4());
Assert.assertEquals("world-attr1", root.getWorld().getAttribute1());
Assert.assertEquals(2, root.getWorld().getElement1Count());
Assert.assertEquals("world-elem1a", root.getWorld().getElement1List()
.get(0));
Assert.assertEquals("world-elem1b", root.getWorld().getElement1List()
.get(1));
Assert.assertEquals(1, root.getOtherGreetingsCount());
Assert.assertEquals("level1-attr1", root.getOtherGreetingsList().get(0)
.getAttribute1());
Assert.assertEquals(1, root.getOtherGreetingsList().get(0)
.getOtherGreetingsCount());
Assert.assertEquals("level2-attr1", root.getOtherGreetingsList().get(0)
.getOtherGreetingsList().get(0).getAttribute1());
}