-
Notifications
You must be signed in to change notification settings - Fork 0
/
thrift_parser.go
144 lines (123 loc) · 2.48 KB
/
thrift_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package st2
import (
"errors"
"io"
"github.com/cloudwego/thriftgo/parser"
)
// ThriftParser is a Parser to parse thrift source
type ThriftParser struct {
ctx Context
}
// NewThriftParser create [ThriftParser]
func NewThriftParser(ctx Context) *ThriftParser {
return &ThriftParser{
ctx: ctx,
}
}
// Parse method parse thrift source
func (p ThriftParser) Parse(reader io.Reader) ([]*Struct, error) {
data, err := io.ReadAll(reader)
if err != nil {
return nil, errors.New("read data failed")
}
if len(data) == 0 {
return nil, nil
}
thrift, err := parser.ParseString("", string(data))
if err != nil {
return nil, err
}
if thrift == nil {
return nil, errors.New("Parse thrift failed")
}
res := make([]*Struct, 0)
for _, e := range thrift.Enums {
res = append(res, p.enum2struct(e))
}
for _, s := range thrift.Structs {
res = append(res, p.structLike2struct(s, SLSStruct))
}
for _, u := range thrift.Unions {
res = append(res, p.structLike2struct(u, SLSUnion))
}
return res, nil
}
func (p ThriftParser) enum2struct(e *parser.Enum) *Struct {
s := &Struct{
Type: &EnumType{
Name: e.Name,
},
}
for _, value := range e.Values {
member := &Member{
Field: value.Name,
Type: s.Type,
Index: int(value.Value),
}
s.Members = append(s.Members, member)
}
return s
}
func (p ThriftParser) structLike2struct(sl *parser.StructLike, source StructLikeSource) *Struct {
s := &Struct{
Type: &StructLikeType{
Name: sl.Name,
Source: source,
},
}
for _, field := range sl.Fields {
t := p.type2Type(field.Type)
if t == nil {
continue
}
member := &Member{
Field: field.Name,
Type: t,
Index: int(field.ID),
Optional: field.Requiredness == parser.FieldType_Optional,
}
s.Members = append(s.Members, member)
}
return s
}
func (p ThriftParser) type2Type(t *parser.Type) Type {
if t == nil {
return nil
}
switch t.Name {
case StrBool:
return BoolVal
case StrByte:
return Int8Val
case StrI16:
return Int16Val
case StrI32:
return Int32Val
case StrI64:
return Int64Val
case StrDouble:
return Float64Val
case StrString:
return StringVal
case StrBinary:
return BinaryVal
case StrMap:
return &MapType{
Key: p.type2Type(t.KeyType),
Value: p.type2Type(t.ValueType),
}
case StrList:
return &ArrayType{
ChildType: p.type2Type(t.ValueType),
}
case StrSet:
return &SetType{
Key: p.type2Type(t.ValueType),
}
default:
return &StructLikeType{
Name: t.Name,
}
}
return nil
}