【问题标题】:Converting Protobuf definitions to Thrift将 Protobuf 定义转换为 Thrift
【发布时间】:2012-03-26 08:39:49
【问题描述】:

是否有任何工具可以从 Protobuf 定义生成 Thrift 接口定义?

【问题讨论】:

    标签: protocol-buffers thrift


    【解决方案1】:

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

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

    Thrift 和 Protobuf 是不可互换的。查看Biggest differences of Thrift vs Protocol Buffers? 以了解一些关键差异。你到底想完成什么?

    【讨论】:

      【解决方案2】:

      wrote a translator 将 Thrift 的子集转换为 Protobuf,反之亦然。

      这是一些 Thrift 代码:

      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;
      }
      

      【讨论】:

        【解决方案3】:

        我认为没有。如果您准备为此编写代码,您可以编写一个为您生成 .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

        【讨论】:

          【解决方案4】:

          我相信有。

          查看我写的无损转换器protobuf-thrift,它可以将protobuf转换为thrift,反之亦然。更重要的是,它会保留声明顺序和 cmets,从而最大限度地保留源代码格式。

          您也可以试用网页界面here

          顺便说一句,我也同意 captncraig 的回答。 thrift 和 protobuf 确实有很多区别,比如 protobuf 中的嵌套类型和 thrift 中的联合类型。但是你不能否认它们有很多共同的语法,比如 enum => enum, struct => message, service => service,这些都是我们最常用的语法。

          所以,protobuf-thrift 是帮助您减少重复工作的工具,并且知道“它们不是 100% 可转换”的事实。

          【讨论】:

            猜你喜欢
            • 2015-11-18
            • 2013-07-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-03-16
            • 2020-04-19
            • 2019-01-06
            • 1970-01-01
            相关资源
            最近更新 更多