Search code examples
javapythonobjectdictionarythrift

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


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?


Solution

  • 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.