(回答問題編輯轉換爲一個社區維基答案見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());
}
這裏有問題,使用protoc什麼時候?還是在運行時填充結構?如果是後者,請顯示一些運行時代碼...? –
感謝Marc的鼓勵。我正在懶惰......通過一個簡化的代碼塊(附件)找到了我的問題。 – user23969