how can I define a map accept different kind of value in thrift?

只愿长相守 提交于 2019-12-19 11:25:13

问题


I define a struct with thrift:

struct QuerySetRecord {
    1:string recordId,
    2:string crawlerName,
    3:string recordType,
    4:map<string,string> dataMap,
    5:i16 priority,
}

the problem is the dataMap, I do not only want to accept string value, I may still want to accept List or Map, such as map<string, list<string>> dataMap. In other words, I want a type like the root Object in Java, object in python

Can I do this?


回答1:


You would have to create your own Object and list all possible classes in it.

union Object {
   1: string str;
   2: i32 number32;
}

(as I'm not sure how union implementation works in all langs I'd go with struct with all fields optional)

struct Object {
   1: optional string str;
   2: optional i32 number32;
}

and then: map<string, Object>

In Thrift you can't create 'accept all' field, as it could not be fully portable across languages, and that's one of key functionalities of Thrift.



来源:https://stackoverflow.com/questions/31958381/how-can-i-define-a-map-accept-different-kind-of-value-in-thrift

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