thrift的IDL语法中,可以生成java的数组吗?

我正在接触thrift,现在想定义一个struct,在--gen java后,想生成一个类似String[]这样的数组属性。
例如,对于

struct SomePojo{
    1:string attr1,
    2:list<string> parameterTypes
}

我想要第二个属性parameterTypes对应的java代码为String[] parameterTypes,而不是List<String> parameterTypes

找了半天也没有找到对应的语法。难不成是因为某些语言可能不支持数组类型,所以thrift为了避免跨语言兼容性问题,所以刻意只给了list、set、map这3中container type?

阅读 2.5k
1 个回答

https://thrift.apache.org/doc...

Containers

Thrift containers are strongly typed containers that map to commonly used and commonly available container types in most programming languages.

There are three container types:

    list: An ordered list of elements. Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
    set: An unordered set of unique elements. Translates to an STL set, Java HashSet, set in Python, etc. Note: PHP does not support sets, so it is treated similar to a List
    map<type1,type2>: A map of strictly unique keys to values. Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc. While defaults are provided, the type mappings are not explicitly fixed. Custom code generator directives have been added to allow substitution of custom types in various destination languages.

Container elements may be of any valid Thrift Type.

N.B.: For maximal compatibility, the key type for map should be a basic type rather than a struct or container type. There are some languages which do not support more complex key types in their native map types. In addition the JSON protocol only supports key types that are base types.

不同语言数组的表现形式不一样,即便是容器来讲,不同语言也不一样,所以thrift只支持某种语言的某个容器,不敢说自己包治百病。

已参与了 SegmentFault 思否社区 10 周年「问答」打卡 ,欢迎正在阅读的你也加入。