apache thrift 中是否有类似的导入语句?

bul*_*ula 3 thrift

我想在 IDL 文件中定义几个结构。然后在服务中返回该结构类型的对象。为此,我必须导入该结构。如何在 IDL 中导入它们。

namespace java abc.xyz

struct struct_{
    1:string s
    2:bool b

}

service struct_test{
    struct_ getstruct_()
}
Run Code Online (Sandbox Code Playgroud)

Jen*_*nsG 5

是的,正如@Vj- 正确指出的那样:

include "path/to/file.thrift"
Run Code Online (Sandbox Code Playgroud)

顺便说一下,可以通过在调用编译器时使用(for recursive)为所有thrift IDL生成代码,包括包含的文件-r

有两个重要的事情要知道:

(1) 包含文件中的定义在包含文件中使用前缀 引用,该前缀来自包含的 IDL 的文件名。教程有一个很好的例子(注意shared前缀):

include "shared.thrift"

service Calculator extends shared.SharedService {
    // more code
}
Run Code Online (Sandbox Code Playgroud)

(2) 非常推荐在每个 IDL 文件中声明不同的命名空间。否则,某些目标语言(例如 PHP)可能会发生从外部 IDL 生成的代码覆盖从内部 IDL 生成的代码的情况,因为使用了相同的输出文件夹。

例如:

namespace * tutorial
Run Code Online (Sandbox Code Playgroud)

namespace * shared
Run Code Online (Sandbox Code Playgroud)