将Protobuf定义转换为Thrift

时间:2012-03-08 20:59:44

标签: protocol-buffers thrift

是否存在从Protobuf定义生成Thrift接口定义的工具?

3 个答案:

答案 0 :(得分:1)

看来答案是“尚未”。您将面临的一个问题是thrift定义了一个带有服务和方法调用的完整RPC系统,而protobuf则专注于数据类型和序列化位。 Thrift的数据模型比protobuf(没有递归结构等)更受限制,但这不应该是节俭中的问题 - > protobuf方向。

当然,您可以非常轻松地将所有thrift数据类型转换为protobuf定义,而完全忽略服务部分。如果你愿意的话,你甚至可以在thrift编译器中添加类似的内置生成器。

Thrift和Protobuf虽然不可互换。看看Biggest differences of Thrift vs Protocol Buffers?,看一些关键的差异。你到底想要完成什么?

答案 1 :(得分:1)

wrote a translator将Thrift的一部分转换为Protobuf,反之亦然。

这是一些节俭代码:

enum Operation{
    ADD=1,SUBTRACT=2,MULTIPLY=3,DIVIDE=4
}
struct Work{1:i32 num1,2:i32 num2,4:string comment
}

会自动转换为以下Protobuf代码:

enum Operation{
    ADD=1,SUBTRACT=2,MULTIPLY=3,DIVIDE=4
}
message Work{int32 num1 = 1;int32 num2 = 2;string comment = 4;
}

答案 2 :(得分:0)

我不认为有。如果您要编写代码,可以编写一个语言生成器,为您生成.thrift文件。

你可以用任何语言编写工具(我在protobuf-j2me [1]中用C ++编写,在[2]中改编了protobuf-csharp-port代码)。

您可以让protoc.exe将其命名为:

protoc.exe --plugin=protoc-gen-thrift.exe --thrift_out=. file.proto

您需要将其命名为protoc-gen-thrift.exe才能使--thrift_out选项可用。

[1] https://github.com/igorgatis/protobuf-j2me

[2] https://github.com/jskeet/protobuf-csharp-port/blob/6ac38bf84452f1f9bd3df59a276708725be2ed49/src/ProtoGen/ProtocGenCs.cs