calbertts
calbertts

Reputation: 1513

Generic objects in Apache Thrift

I want to know if I can define "generic objects" in Apache Thrift with IDL language, something like "Object" class in Java.

I need to send objects's list of any type, but I don't know how can I define this in the file.thrift

Like this:

struct
{
   1: list<object> listObjects;
}

or

struct
{
   1: list<?> listObjects;
}

Upvotes: 3

Views: 10581

Answers (1)

codeSF
codeSF

Reputation: 1172

Apache Thrift does not support struct inheritance or self referential structs. This makes what you would like to do, passing a polymorphic list directly, impossible. There are ways to work around this, for instance serializing objects to binary and then passing a list of binaries, de-serializing them on the other end.

struct abc {
    1: list<binary> myList,
}

All Apache Thrift structs have read and write methods which you can use to serialize them to a memory buffer (TMemoryBuffer) which you can then use as a binary object for the list. Another option might be to use a union.

union myTypes {
    1: double dbl
    2: i64 bigInt
    3: SomeOtherStruct sos
}

struct abc {
    1: list<myTypes> myList,
}

This approach creates a list of the myTypes union type, but the union can house any IDL defined type you like.

Upvotes: 16

Related Questions