2011-08-23 42 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()); 

}