2011-08-23 84 views
0

我想測試遞歸協議緩衝區定義(使用Java,但可能無所謂)。我試圖用2層RootType消息「otherGreetings」(遞歸結構)填充我的消息。我無法獲得正確的語法,找不到任何樣本。我最終會發生堆棧溢出,或者我的郵件都被卡在同一個「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

這裏有問題,使用protoc什麼時候?還是在運行時填充結構?如果是後者,請顯示一些運行時代碼...? –

+0

感謝Marc的鼓勵。我正在懶惰......通過一個簡化的代碼塊(附件)找到了我的問題。 – user23969

回答

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()); 

}