Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apache thrift fails generating recursive structs

I wanted to represent a standard tree structure in apache thrift, but I encountered following problem:

[ERROR:/path_to_project/thrift/service.thrift:31] (last token was 'TCategoryTree')
Type "TCategoryTree" has not been defined.

These are my thrift structures:

struct TCategory {
    1: required string name
}

struct TCategoryTree {
    1: required TCategory element,
    2: optional list<TCategoryTree> children
}

Line 31 is 2: optional list<TCategoryTree> children, where I define a field that has the same type I'm defining right now.

Could it be that apache thrift doesn't support recursive structures or am I making some kind of mistake here?

edit: I'm using version 0.9.0

like image 397
ducin Avatar asked Mar 26 '13 09:03

ducin


1 Answers

Yes, unfortuantely Thrift does not allow recursive structures yet. There are workarounds for this limitation, e.g. flatten your data structures while transmitting them. In most cases this is feasible, although it requires some extra code on both sides.

Here's a good example how to do it: http://grokbase.com/t/thrift/user/0984cqwxen/recursive-datatypes


Update

The current Thrift development trunk supports this for a while now. Be careful, as it allows for endless reference loops (A references B references A ...) resulting in an stack overflow when trying to serialize.

like image 106
JensG Avatar answered Sep 19 '22 10:09

JensG