douzao9845 2015-01-05 12:05 采纳率: 0%
浏览 747
已采纳

为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 13: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部