Apache thrift, struct contain itself
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
- Struct A
- List of C开发者_C百科hild items
- Items(Items are Struct A )
- List of C开发者_C百科hild items
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?
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;
}
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
精彩评论