Skip to content

Commit 9abcea9

Browse files
committed
docs: ✏️ complete doc for pb2thrift nested fields feature
1 parent 7c45667 commit 9abcea9

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

README.md

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# protobuf-thrift
22
Little cli utility for lazy guy😉 ~ Transforming protobuf idl to thrift, and vice versa.
33

4+
[![GoDoc](https://pkg.go.dev/badge/github.com/YYCoder/protobuf-thrift)](https://pkg.go.dev/github.com/YYCoder/protobuf-thrift)
5+
46
> [IDL](https://en.wikipedia.org/wiki/IDL)(Interface description language), which is a descriptive language used to define data types and interfaces in a way that is independent of the programming language or operating system/processor platform.
57
68
[中文文档](./docs/cn.md)
79

10+
811
## Install
912
For folks don't have GO development environment, directly download corresponding platform binary from latest release is the best choice.
1013

@@ -50,11 +53,11 @@ protobuf-thrift -t thrift2proto -i ./path/to/idl.thrift -o ./idl.proto -r 1`
5053

5154
![](./docs/usage.jpeg)
5255

53-
## Caveat
56+
## Notice
5457

55-
Since protobuf and thrift have many different grammars, we can only transform grammars that have same meaning, e.g. protobuf message => thrift struct, protobuf enum => thrift enum.
58+
Since protobuf and thrift have many different syntaxes, we can only transform syntaxes that have same meaning, e.g. protobuf message => thrift struct, protobuf enum => thrift enum.
5659

57-
Here is a list of transformation rule, so we hope you won't have to worry about protobuf-thrift do sth unexpected.
60+
Here is a list of transformation rule, so we hope you don't have to worry about protobuf-thrift do sth unexpected.
5861

5962
|protobuf type|thrift type|field type|notice|
6063
|:--:|:--:|:--:|:--:|
@@ -77,5 +80,42 @@ Here is a list of transformation rule, so we hope you won't have to worry about
7780
|extend|||only supported in protobuf, so thrift will omit it|
7881
|extension|||only supported in protobuf, so thrift will omit it|
7982

83+
### Nested Fields
84+
protobuf support nested field within message, but thrift does not, so protobuf-thrift will prefix nested field name with outer message name to work around this. for example:
85+
86+
```protobuf
87+
message GroupMsgTaskQueryExpress {
88+
enum QueryOp {
89+
Unknown = 0;
90+
GT = 1;
91+
}
92+
message TimeRange {
93+
int32 range_start = 1;
94+
int32 range_end = 2;
95+
}
96+
QueryOp express_op = 1;
97+
int32 op_int = 2;
98+
TimeRange time_op = 3;
99+
int32 next_op_int = 4;
100+
}
101+
```
80102

103+
will transform to:
104+
105+
```thrift
106+
struct GroupMsgTaskQueryExpress {
107+
1: GroupMsgTaskQueryExpressQueryOp ExpressOp
108+
2: i32 OpInt
109+
3: GroupMsgTaskQueryExpressTimeRange TimeOp
110+
4: i32 NextOpInt
111+
}
112+
enum GroupMsgTaskQueryExpressQueryOp {
113+
Unknown = 0
114+
GT = 1
115+
}
116+
struct GroupMsgTaskQueryExpressTimeRange {
117+
1: i32 RangeStart
118+
2: i32 RangeEnd
119+
}
120+
```
81121

docs/cn.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protobuf-thrift -t thrift2proto -i ./path/to/idl.thrift -o ./idl.proto -r 1`
4646

4747
![](./usage.jpeg)
4848

49-
## 使用声明
49+
## 注意事项
5050
由于 protobuf 与 thrift 有很多语法上的不同,我们不可能完全将一种 idl 转换成另一种,protobuf-thrift 也只是一个帮助我们摆脱复制粘贴的小工具,它所提供的功能能够满足 80% 的场景就足够了。因此,我们只会尽可能将有相同语义的语法进行转换,如 protobuf message => thrift struct,protobuf enum => thrift enum。
5151

5252
为了确保你能够明确的知道 protobuf-thrift 会如何转换,如下是目前的转换规则:
@@ -72,5 +72,44 @@ protobuf-thrift -t thrift2proto -i ./path/to/idl.thrift -o ./idl.proto -r 1`
7272
|extend|||only supported in protobuf, so thrift will omit it|
7373
|extension|||only supported in protobuf, so thrift will omit it|
7474

75+
### 嵌套字段
76+
protobuf 支持在 message 结构体中嵌套字段(如 enum/message),但在 thrift 中不支持,因此 protobuf-thrift 会通过给嵌套字段的标识符使用外部 message 名称作为前缀的方式来实现相同命名空间的效果。如下例:
77+
78+
```protobuf
79+
message GroupMsgTaskQueryExpress {
80+
enum QueryOp {
81+
Unknown = 0;
82+
GT = 1;
83+
}
84+
message TimeRange {
85+
int32 range_start = 1;
86+
int32 range_end = 2;
87+
}
88+
QueryOp express_op = 1;
89+
int32 op_int = 2;
90+
TimeRange time_op = 3;
91+
int32 next_op_int = 4;
92+
}
93+
```
94+
95+
会被转换成:
96+
97+
```thrift
98+
struct GroupMsgTaskQueryExpress {
99+
1: GroupMsgTaskQueryExpressQueryOp ExpressOp
100+
2: i32 OpInt
101+
3: GroupMsgTaskQueryExpressTimeRange TimeOp
102+
4: i32 NextOpInt
103+
}
104+
enum GroupMsgTaskQueryExpressQueryOp {
105+
Unknown = 0
106+
GT = 1
107+
}
108+
struct GroupMsgTaskQueryExpressTimeRange {
109+
1: i32 RangeStart
110+
2: i32 RangeEnd
111+
}
112+
```
113+
75114

76115

0 commit comments

Comments
 (0)