Generic objects in Apache Thrift

落花浮王杯 提交于 2019-12-22 11:10:03

问题


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;
}

回答1:


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.



来源:https://stackoverflow.com/questions/16845752/generic-objects-in-apache-thrift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!