douzao9845 2015-01-05 20:05 采纳率: 0%
浏览 744
已采纳

为Go生成多个Thrift文件的正确方法

So I have the following files

/src/baseService.thrift
    /baseTypes.thrift
    /baseSecurity.thrift

I want all of these thrift definitions to be created into one library. The top of each file is thus:

baseService.thrift
==================
namespace java foo.bar
namespace cpp foo.bar
namespace js foo.bar
namespace go foo.bar

import "baseTypes.thrift"

baseTypes.thrift
================
namespace java foo.bar
namespace cpp foo.bar
namespace js foo.bar
namespace go foo.bar

baseSecurity.thrift
===================
namespace java foo.bar
namespace cpp foo.bar
namespace js foo.bar
namespace go foo.bar

import "baseTypes.thrift"

The problem is, how to I create all of these into one lib package? It works fine for java/cpp/js but when I try to build for go it's a no go.

With thrift, you can't do a thrift gen:baz *.thrift, you have to do the files one at a time. For the other languages, we just do a:

for f in `find *.thrift`; do
   thrift -o myGenDir --gen go $f"
done

(substitute appropriate gen command for each lang)

For Python this is fine because it puts every gen'd file in it's own dir based on the filename [ i.e. foo/bar/{filename}/ttypes.py]. For Java it dumps all of the files in foo/bar/ but every class name is unique. For cpp, it dumps it all into the gen dir, but uniquely named per thrift file [so {filename.h}, {filename.cpp}]. For Go, however, it dumps everything into foo/bar like so:

/foo/bar/constants.go
/foo/bar/service.go
/foo/bar/service-remote/
/foo/bar/baz/  [for anything that has a namespace of foo.bar.baz]
/foo/bar/ttypes.go

The problem is, the ttypes.go and (presumably) constants.go are getting overwritten by whatever is gen'd last in the for loop. Is there a way around this? It works for the other languages - seems like it's an oversight for Go. What am I missing. We've got lots of Thrift files with lots of stuff in them - I'd rather not have to combine everything that's at the same package level into one thrift file.

  • 写回答

2条回答 默认 最新

  • dounuo8797 2015-01-05 21:24
    关注

    The problem is, the ttypes.go and (presumably) constants.go are getting overwritten by whatever is gen'd last in the for loop.

    Yes, that's true.

    Is there a way around this?

    The most (cross-language) portable recommendation is to not do this. Instead:

    • put different IDL files into different namespaces
    • put everything belonging to one namespace into exactly one IDL file

    The Thrift compiler offers a few compiler switches for Go that may help you at least partially, (you get all available options for all languages by typing thrift --help on the command prompt)

     go (Go):
       package_prefix=  Package prefix for generated files.
       thrift_import=   Override thrift package import path (default:git.apache.org/thrift.git/lib/go/thrift)
       package=         Package name (default: inferred from thrift file name)
    

    These options are used like in

     thrift -gen go:package=mypack,package_prefix=myprefix
    

    It works for the other languages - seems like it's an oversight for Go.

    It might be your impression but I'd recommend not to try it, if you are interested in cross-language compatibility. The behaviour is the same with other languages. Just as an example: I recently fixed (or better: worked around) a problem with the Erlang tests, where I had to fight exactly this very same issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog