-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I0c6891dc8fe2c53822a377bc03ff78cbce3d6208
- Loading branch information
Showing
9 changed files
with
162 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,71 @@ | ||
# protobuf-thrift | ||
Little thing for lazy guy, transforming protobuf idl to thrift, and vice versa. | ||
为懒人准备的 protobuf 与 thrift 互转的小工具😉。 | ||
|
||
> IDL(Interface description language), reference [Wikipedia](https://en.wikipedia.org/wiki/IDL). | ||
> [IDL](https://en.wikipedia.org/wiki/IDL)(Interface description language)。是指一种用于定义数据类型以及接口的描述性语言,与编程语言以及平台无关,常用在微服务架构中。 | ||
[cn](./docs/cn.md) | ||
## 使用示例 | ||
|
||
## Caveats | ||
### 基本用法 | ||
将 thrift 文件转成 protobuf 文件: | ||
|
||
Since protobuf and thrift have many different grammars, so we can only transform grammars that have same meaning, e.g. protobuf message => thrift struct, protobuf enum => thrift enum. | ||
``` | ||
protobuf-thrift -t thrift2proto -i ./path/to/idl.thrift -o ./idl.proto` | ||
``` | ||
|
||
将 protobuf 文件转成 thrift 文件: | ||
|
||
``` | ||
protobuf-thrift -t proto2thrift -i ./path/to/idl.thrift -o ./test.proto` | ||
``` | ||
|
||
### 交互式用法 | ||
直接使用 `protobuf-thrift -t thrift2proto` 命令会进入交互模式,直接粘贴你的源 idl 源码到终端,并按下 ctrl+D 即可。 | ||
|
||
![interactive.gif](./2021-08-09%2021_54_20.gif) | ||
|
||
### 大小写转换 | ||
得益于 [strcase](https://github.com/iancoleman/strcase),Protobuf-thrift 提供了完整的变量大小写转换能力,可用的选项已经在 **--help** 提示信息中了,请自行查阅。 | ||
|
||
### 递归转换 | ||
某些场景下,我们可能需要将整个 idl 仓库转成另一种语言,此时我们就可以使用 **-r** 选项来递归地将 import 的文件全部转换。 | ||
|
||
该选项默认是禁用的,要使用它时需要显式指定。 | ||
|
||
|
||
``` | ||
protobuf-thrift -t thrift2proto -i ./path/to/idl.thrift -o ./idl.proto -r 1` | ||
``` | ||
|
||
|
||
## 可用选项 | ||
|
||
![](./usage.jpeg) | ||
|
||
## 使用声明 | ||
由于 protobuf 与 thrift 有很多语法上的不同,我们不可能完全将一种 idl 转换成另一种,protobuf-thrift 也只是一个帮助我们摆脱复制粘贴的小工具,它所提供的功能能够满足 80% 的场景就足够了。因此,我们只会尽可能将有相同语义的语法进行转换,如 protobuf message => thrift struct,protobuf enum => thrift enum。 | ||
|
||
为了确保你能够明确的知道 protobuf-thrift 会如何转换,如下是目前的转换规则: | ||
|
||
|protobuf type|thrift type|field type|notice| | ||
|:--:|:--:|:--:|:--:| | ||
|message|struct|optional => optional; repeated T => list\<T\>|only protobuf 2 have optional field| | ||
|map<T1,T2>|map<T1,T2>||T1 only support int32/int64/string/float/double, due to thrift syntax| | ||
|enum|enum||| | ||
|int32|i32||| | ||
|int64|i64||| | ||
|float|double||| | ||
|double|double||| | ||
|bool|bool||| | ||
|string|string||| | ||
|bytes|binary||| | ||
|service|service|rpc => methods|| | ||
|constant|const||not support currently| | ||
|package|namespace||| | ||
|import|include||| | ||
|syntax|||only supported in protobuf, so thrift will omit it| | ||
|option|||only supported in protobuf, so thrift will omit it| | ||
|extend|||only supported in protobuf, so thrift will omit it| | ||
|extension|||only supported in protobuf, so thrift will omit it| | ||
|
||
Here is a list of transformation rule, so we hope you won't have to worry about protobuf-thrift | ||
|
||
* | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
enum Status { | ||
StatusUnknown = 0 | ||
StatusUnreviewed = 1 | ||
StatusOnline = 2 | ||
StatusRejected = 3 | ||
StatusOffline = 4 | ||
enum status { | ||
statusUnknown = 0 | ||
statusUnreviewed = 1 | ||
statusOnline = 2 | ||
statusRejected = 3 | ||
statusOffline = 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
include "./test.thrift"; | ||
include "./common/admin.thrift"; | ||
enum OtherEnum { | ||
OtherEnumUnknown = 0 | ||
Unreviewed = 1 | ||
Online = 2 | ||
Rejected = 3 | ||
Offline = 4 | ||
include "./test.thrift"; | ||
enum otherEnum { | ||
otherEnumUnknown = 0 | ||
unreviewed = 1 | ||
online = 2 | ||
rejected = 3 | ||
offline = 4 | ||
} | ||
struct Config { | ||
1: i64 Id | ||
2: i32 Tag | ||
3: list<i32> TypeList | ||
4: bool Boolean | ||
5: admin.Status Status | ||
6: map<i64,string> FailMap | ||
7: double Fl | ||
8: double Db | ||
9: binary Bs | ||
10: test.TimeRange Nested | ||
11: list<test.TimeRange> NestedTypeList | ||
12: map<string,test.TimeRange> NestedTypeMap | ||
struct config { | ||
1: i64 id | ||
2: i32 tag | ||
3: list<i32> typeList | ||
4: bool boolean | ||
5: admin.status status | ||
6: map<i64,string> failMap | ||
7: double fl | ||
8: double db | ||
9: binary bs | ||
10: test.timeRange nested | ||
11: list<test.timeRange> nestedTypeList | ||
12: map<string,test.timeRange> nestedTypeMap | ||
} | ||
service APIs { | ||
Config TestOther (1: Config Req) | ||
|
||
service aPIs { | ||
Config testOther (1: Config req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
struct TimeRange { | ||
1: i64 Start | ||
2: i64 End | ||
struct timeRange { | ||
1: i64 start | ||
2: i64 end | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,48 @@ | ||
namespace * test.test.test; | ||
|
||
enum Status { | ||
StatusUnknown = 0 | ||
StatusUnreviewed = 1 | ||
StatusOnline = 2 | ||
StatusRejected = 3 | ||
StatusOffline = 4 | ||
} | ||
struct Config { | ||
1: i64 Id | ||
2: optional i32 Tag | ||
3: list<i32> TypeList | ||
4: optional bool Boolean | ||
5: Status Status | ||
6: map<i64,string> FailMap | ||
7: double Fl | ||
8: double Db | ||
9: binary Bs | ||
10: TimeRange Nested | ||
11: list<TimeRange> NestedTypeList | ||
12: map<string,TimeRange> NestedTypeMap | ||
} | ||
struct TimeRange { | ||
1: i64 Start | ||
2: i64 End | ||
} | ||
struct ReqOfTestGetApi { | ||
1: i64 A | ||
2: string B | ||
} | ||
struct RespOfTestGetApi { | ||
1: i32 Code | ||
2: string Message | ||
} | ||
struct ReqOfTestPostApi { | ||
1: i64 A | ||
2: string B | ||
} | ||
struct RespOfTestPostApi { | ||
1: i32 Code | ||
2: string Message | ||
enum status { | ||
statusUnknown = 0 | ||
statusUnreviewed = 1 | ||
statusOnline = 2 | ||
statusRejected = 3 | ||
statusOffline = 4 | ||
} | ||
struct reqOfTestPostApi { | ||
1: i64 a | ||
2: string b | ||
} | ||
struct respOfTestPostApi { | ||
1: i32 code | ||
2: string message | ||
} | ||
struct config { | ||
1: i64 id | ||
2: i32 tag | ||
3: list<i32> typeList | ||
4: bool boolean | ||
5: status status | ||
6: map<i64,string> failMap | ||
7: double fl | ||
8: double db | ||
9: binary bs | ||
10: timeRange nested | ||
11: list<timeRange> nestedTypeList | ||
12: map<string,timeRange> nestedTypeMap | ||
} | ||
struct timeRange { | ||
1: i64 start | ||
2: i64 end | ||
} | ||
struct reqOfTestGetApi { | ||
1: i64 a | ||
2: string b | ||
} | ||
struct respOfTestGetApi { | ||
1: i32 code | ||
2: string message | ||
} | ||
|
||
service APIs { | ||
RespOfTestGetApi TestGetApi (1: ReqOfTestGetApi Req) | ||
RespOfTestPostApi TestPostApi (1: ReqOfTestPostApi Req) | ||
service aPIs { | ||
RespOfTestGetApi testGetApi (1: ReqOfTestGetApi req) | ||
RespOfTestPostApi testPostApi (1: ReqOfTestPostApi req) | ||
} |