Reputation: 12414
I am looking into thrift for serialization of data. But Document says
cyclic structs - Structs can only contain structs that have been declared before it. A struct also cannot contain itself
One of our requirement is
So reading requirement i can't have Struct within itself at any level? can i have it in cyclic model as i have it above. Struct is not member of Struct directly but it has some other member and it contains struct.
Their document is not so well descriptive.
Is it possible in Thrift? Does protobuf supports it?
Upvotes: 3
Views: 1787
Reputation: 13411
Yes, starting with Thrift 0.9.2 this scenario is supported.
With any earlier version of thrift the following (intentionally) lead to an compiler error message:
struct Foo {
1 : Foo foo // error - Foo not fully defined yet
2 : Bar bar // error - Bar not defined yet
}
struct Bar {
1 : Foo left // ok, Foo has been defined earlier
2 : Foo right // ok, Foo has been defined earlier
}
There are still a few caveats though. The deveoper is responsible for not producing endless loops or diamond references, like
var foo = new Foo();
foo.foo = foo; // will crash on serialization with stack overflow
var bar = new Bar();
bar.left = foo;
bar.right = foo; // points to same object only BEFORE deserialization
Upvotes: 2
Reputation: 284796
According to this discussion, it's not possible in Thrift. However, there is a workaround of using integers to index into a master list. Essentially, this is a form of poor man's pointers.
struct A
{
1: list<i32> subitems;
}
struct AllAs
{
1: list<A> items;
}
subitems is essentially a list of pointers into AllAs.items
In Protocol Buffers, it's trivial:
message A {
repeated A subitems = 1;
}
Upvotes: 2