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条)

报告相同问题?

悬赏问题

  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题