我想在apache thrift中表示一个标准的树结构,但是我遇到了 以下问题:
[ERROR:/path_to_project/thrift/service.thrift:31] (last token was 'TCategoryTree')
Type "TCategoryTree" has not been defined.
这些是我的储蓄结构:
struct TCategory {
1: required string name
}
struct TCategoryTree {
1: required TCategory element,
2: optional list<TCategoryTree> children
}
第 31 行是 2: optional list<TCategoryTree> children
,我在其中定义一个字段
与我现在定义的类型相同。
难道apache thrift不支持递归结构还是我在做 这里有某种错误吗?
编辑:我使用的是0.9.0版本
最佳答案
是的,不幸的是 Thrift 还不允许递归结构。对于此限制有一些解决方法,例如在传输数据时扁平化数据结构。在大多数情况下这是可行的,尽管双方都需要一些额外的代码。
这是一个很好的示例,说明如何执行此操作: http://grokbase.com/t/thrift/user/0984cqwxen/recursive-datatypes
<小时/>更新
当前的 Thrift 开发主干已经支持这一点一段时间了。请小心,因为它允许无限的引用循环(A 引用 B 引用 A ...),从而在尝试序列化时导致堆栈溢出。
关于struct - apache thrift 无法生成递归结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15634101/