在生成文件时保持目录结构。

huangapple go评论100阅读模式
英文:

keep directory structure when generating files

问题

我有以下 thrift 接口:

./thrift/a/a1.thrift
./thrift/a/a2.thrift
./thrift/b/b1.thrift
./thrift/b/b2.thrift

其中 a1.thrift 包含了 a2、b1 和 b2(使用 include "thrift/a/a2.thrift"

我使用 thrift -r --gen go:package_prefix=work -I . --out . thrift/a/a1.thrift 为它们生成了 Go 文件

生成的文件如下:

./a1/constants.go
./a1/ttypes.go
./a2/...
./b1/...
./b2/...

我该如何告诉 thrift 输出到以下目录?

./a/a1/...
./a/a2/...
./b/b1/...
./b/b2/...

请注意,我可以手动移动这些文件,但首先我有很多文件,其次在 Go 中包必须与目录匹配,所以我需要编辑这些文件。例如,生成的 a1 的 Go 文件将把 a2 导入为 work/a2 而不是 work/a/a2

英文:

I have those thrift interfaces:

./thrift/a/a1.thrift
./thrift/a/a2.thrift
./thrift/b/b1.thrift
./thrift/b/b2.thrift

where a1.thrift includes a2, b1, b2 (with include "thrift/a/a2.thrift")

I generate the Go files for all those with thrift -r --gen go:package_prefix=work -I . --out . thrift/a/a1.thrift

It outputs:

./a1/constants.go
./a1/ttypes.go
./a2/...
./b1/...
./b2/...

How can I tell thrift to output in?

./a/a1/...
./a/a2/...
./b/b1/...
./b/b2/...

Note that I can move those files by hand but first I have many and second in Go the package has to match the directories, so I would need to edit those files. As an example the generated Go file for a1 will import a2 as work/a2 and not work/a/a2)

答案1

得分: 1

使用命名空间。在每个IDL文件的顶部添加类似以下的行:

 namespace go a.a1   // 根据需要修改,但每个IDL文件只能有一个命名空间

运行以下命令:

thrift -r -gen go a1.thrift

会在以下路径下生成文件:

 gen-go/a/a1/*
英文:

Use namespaces. Add a line similar to the following on top of each IDL file:

 namespace go a.a1   // whatever you need, but exactly one per IDL file

Running

thrift -r -gen go a1.thrift

creates files under

 gen-go/a/a1/*

huangapple
  • 本文由 发表于 2017年4月28日 17:25:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/43676689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定