使用Thrift表达树结构

时间:2012-07-02 07:38:23

标签: tree thrift

我现在使用Thrift作为通信协议。 我必须传递一个对象(树数据结构)来映射树数据结构。我想生成java代码。 如何通过Thrift传输树数据结构,我通过Tree定义了一个对象,但是thrift不允许这样,这是我的thrift文件中的一个部分:

struct MyObject {
1:i32 id,
2:i32 parentid,
3:string name,
4:list<MyObject> children
}

但是,我从Thrift得到了这个错误:类型“MyObject”尚未定义。 这有什么好的解决方法吗? 谢谢你的任何建议!

2 个答案:

答案 0 :(得分:4)

你可以试试这个。

struct TreeNode {
1:i32 id,
2:i32 parentid,
3:string name,
4:list<i32> childrenIds
}

struct Tree {
1:i32 rootId,
2:map<i32,TreeNode> nodes,
}

使用这种方式,您可以将简单的数据结构解释为树

答案 1 :(得分:1)

Thrift不允许递归: http://grokbase.com/t/thrift/user/0984cqwxen/recursive-datatypes 可能的解决方法由user537862

编写