-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic.go
317 lines (306 loc) · 9.6 KB
/
dynamic.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package client
import (
"fmt"
"git.apache.org/thrift.git/lib/go/thrift"
"github.com/samuel/go-thrift/parser"
"reflect"
"strings"
)
type Dynamic struct {
FilePath string
ThriftIDLs map[string]*parser.Thrift
}
func NewDynamic(filepath string) (c *Dynamic, err error) {
p := parser.Parser{}
c = &Dynamic{}
c.ThriftIDLs, c.FilePath, err = p.ParseFile(filepath)
if err != nil {
return nil, err
}
return c, nil
}
func (s *Dynamic) getThriftType(t string) thrift.TType {
switch strings.ToUpper(t) {
case "BOOL":
return thrift.BOOL
case "BYTE":
return thrift.BYTE
case "I16":
return thrift.I16
case "I32":
return thrift.I32
case "I64":
return thrift.I64
case "STRING":
return thrift.STRING
case "MAP":
return thrift.MAP
case "SET":
return thrift.SET
case "LIST":
return thrift.LIST
case "STRUCT":
return thrift.STRUCT
default:
if _, ok := s.ThriftIDLs[s.FilePath].Structs[t]; ok {
return thrift.STRUCT
}
if _, ok := s.ThriftIDLs[s.FilePath].Enums[t]; ok {
return thrift.I32
}
return thrift.STOP
}
}
func (s *Dynamic) Write(service string, api string, oprot thrift.TProtocol, args map[string]interface{}) (err error) {
serviceItem, ok := s.ThriftIDLs[s.FilePath].Services[service]
if !ok {
return fmt.Errorf("service:%s not found in thrift idl:%s", service, s.FilePath)
}
method, ok := serviceItem.Methods[api]
if !ok {
return fmt.Errorf("method:%s not found in service:%s, thrift idl:%s", api, service, s.FilePath)
}
if err = oprot.WriteStructBegin(fmt.Sprintf("%s_args", api)); err != nil {
return fmt.Errorf("%T write struct begin error: %s", s, err)
}
for i := 0; i < len(method.Arguments); i++ {
// to do check default value
if _, exists := args[method.Arguments[i].Name]; !exists && method.Arguments[i].Optional == false {
return fmt.Errorf("%s not exists in %s method's Arguments", method.Arguments[i].Name, method.Name)
}
if err = s.writeFields(method.Arguments[i], oprot, args[method.Arguments[i].Name]); err != nil {
return
}
}
if err = oprot.WriteFieldStop(); err != nil {
return fmt.Errorf("write field stop error: %s", err)
}
if err := oprot.WriteStructEnd(); err != nil {
return fmt.Errorf("write struct stop error: %s", err)
}
return nil
}
func (s *Dynamic) writeStruct(Struct *parser.Struct, oprot thrift.TProtocol, input interface{}) (err error) {
arg, ok := input.(map[string]interface{})
if !ok {
return fmt.Errorf("struct error")
}
if err = oprot.WriteStructBegin(Struct.Name); err != nil {
return fmt.Errorf("write struct:%s begin error: %s", Struct.Name, err)
}
for i := 0; i < len(Struct.Fields); i++ {
if err = s.writeFields(Struct.Fields[i], oprot, arg[Struct.Fields[i].Name]); err != nil {
return err
}
}
if err = oprot.WriteFieldStop(); err != nil {
return fmt.Errorf("write field stop error: %s", err)
}
if err = oprot.WriteStructEnd(); err != nil {
return fmt.Errorf("write struct stop error: %s", err)
}
return nil
}
func (s *Dynamic) writeBaseType(valueTType thrift.TType, oprot thrift.TProtocol, arg interface{}) (err error) {
switch valueTType {
case thrift.BOOL:
if err = oprot.WriteBool(reflect.ValueOf(arg).Bool()); err != nil {
return fmt.Errorf("write bool value error:%s", err)
}
case thrift.I16:
if err = oprot.WriteI16(int16(reflect.ValueOf(arg).Int())); err != nil {
return fmt.Errorf("write i16 value error:%s", err)
}
case thrift.I32:
if err = oprot.WriteI32(int32(reflect.ValueOf(arg).Int())); err != nil {
return fmt.Errorf("write int32 value error:%s", err)
}
case thrift.I64:
if err = oprot.WriteI64(int64(reflect.ValueOf(arg).Int())); err != nil {
return fmt.Errorf("write int64 value error:%s", err)
}
case thrift.STRING:
if err = oprot.WriteString(string(reflect.ValueOf(arg).String())); err != nil {
return fmt.Errorf("write string value error:%s", err)
}
case thrift.DOUBLE:
if err = oprot.WriteDouble(float64(reflect.ValueOf(arg).Float())); err != nil {
return fmt.Errorf("write double value error: %s", err)
}
case thrift.BYTE:
if err = oprot.WriteByte(byte(reflect.ValueOf(arg).Int())); err != nil {
return fmt.Errorf("write byte value error: %s", err)
}
default:
return fmt.Errorf("unsupport value type:%d", valueTType)
}
return nil
}
func (s *Dynamic) writeStringMap(f *parser.Field, oprot thrift.TProtocol, arg interface{}) (err error) {
m, ok := arg.(map[string]interface{})
if !ok {
return fmt.Errorf("map bad format (%+v)", arg)
}
valueTType := s.getThriftType(f.Type.ValueType.Name)
if valueTType == thrift.STOP {
return fmt.Errorf("map value type unknonw, %d:%s:%s", f.ID, f.Name, f.Type.ValueType.Name)
}
if err = oprot.WriteMapBegin(thrift.STRING, valueTType, len(m)); err != nil {
return fmt.Errorf("error writing map begin: %s", err)
}
for k, v := range m {
if err = oprot.WriteString(string(k)); err != nil {
return fmt.Errorf("map:%d:%s key write error: %s", f.ID, f.Name, err)
}
if err = s.writeBaseType(valueTType, oprot, v); err != nil {
return err
}
}
return nil
}
func (s *Dynamic) WriteI32Map(f *parser.Field, oprot thrift.TProtocol, arg interface{}) (err error) {
return fmt.Errorf("unsupport map<int32>")
}
func (s *Dynamic) writeMap(f *parser.Field, oprot thrift.TProtocol, arg interface{}) (err error) {
keyTType := s.getThriftType(f.Type.KeyType.Name)
switch keyTType {
case thrift.STRING:
if err = s.writeStringMap(f, oprot, arg); err != nil {
return err
}
case thrift.I32:
if err = s.WriteI32Map(f, oprot, arg); err != nil {
return err
}
}
if err := oprot.WriteMapEnd(); err != nil {
return fmt.Errorf("error writing map end: %s", err)
}
return nil
}
func (s *Dynamic) writeList(f *parser.Field, oprot thrift.TProtocol, arg interface{}) (err error) {
valueTType := s.getThriftType(f.Type.ValueType.Name)
if err := oprot.WriteListBegin(valueTType, reflect.ValueOf(arg).Len()); err != nil {
return fmt.Errorf("error writing list begin: %s", err)
}
for i := 0; i < reflect.ValueOf(arg).Len(); i++ {
if valueTType == thrift.STRUCT {
if err = s.writeStruct(s.ThriftIDLs[s.FilePath].Structs[f.Type.ValueType.Name], oprot, reflect.ValueOf(arg).Index(i)); err != nil {
return fmt.Errorf("write map struct value error:%s", err)
}
} else {
if err = s.writeBaseType(valueTType, oprot, reflect.ValueOf(arg).Index(i).Interface()); err != nil {
return err
}
}
}
if err := oprot.WriteListEnd(); err != nil {
return fmt.Errorf("error writing list end: %s", err)
}
return nil
}
func (s *Dynamic) writeSet(f *parser.Field, oprot thrift.TProtocol, arg interface{}) (err error) {
return nil
}
// byte has some problem, reflect get byte Type is uint8, thrift is int8
// double has problem, too
// binary also has problem too, reflect get Type nothing...
func (s *Dynamic) writeFields(f *parser.Field, oprot thrift.TProtocol, arg interface{}) (err error) {
if arg == nil {
if f.Optional == true {
return nil
} else {
return fmt.Errorf("field is required, %d:%s", f.ID, f.Name)
}
}
/* check type
fType := strings.ToUpper(f.Type.Name)
if strings.ToUpper(reflect.TypeOf(arg).Name()) != fType {
return fmt.Errorf("field:%s type should %s not %s", f.Name, fType, reflect.TypeOf(arg).Name())
}
*/
fTType := s.getThriftType(f.Type.Name)
if err = oprot.WriteFieldBegin(f.Name, fTType, int16(f.ID)); err != nil {
return fmt.Errorf("write field begin error %d:%s: %s", s, f.ID, f.Name, err)
}
if fTType == thrift.MAP {
if err = s.writeMap(f, oprot, arg); err != nil {
return err
}
} else if fTType == thrift.SET {
if err = s.writeSet(f, oprot, arg); err != nil {
return err
}
} else if fTType == thrift.LIST {
if err = s.writeList(f, oprot, arg); err != nil {
return err
}
} else if fTType == thrift.STRUCT {
if err = s.writeStruct(s.ThriftIDLs[s.FilePath].Structs[f.Type.Name], oprot, arg); err != nil {
return err
}
} else if err = s.writeBaseType(fTType, oprot, arg); err != nil {
return err
}
if err = oprot.WriteFieldEnd(); err != nil {
return fmt.Errorf("write field end error %d:%s: %s", f.ID, f.Name, err)
}
return nil
}
func (s *Dynamic) readBaseType(ttype *parser.Type, iprot thrift.TProtocol) (v interface{}, err error) {
rtnTType := s.getThriftType(ttype.Name)
switch rtnTType {
case thrift.BOOL:
return iprot.ReadBool()
case thrift.BYTE:
return iprot.ReadByte()
case thrift.I16:
return iprot.ReadI16()
case thrift.I32:
return iprot.ReadI32()
case thrift.I64:
return iprot.ReadI64()
case thrift.STRING:
return iprot.ReadString()
case thrift.DOUBLE:
return iprot.ReadDouble()
case thrift.MAP:
case thrift.LIST:
case thrift.SET:
case thrift.STRUCT:
default:
return nil, fmt.Errorf("unsupport type")
}
return nil, nil
}
func (s *Dynamic) Read(service string, api string, iprot thrift.TProtocol, value map[string]interface{}) (err error) {
serviceItem, ok := s.ThriftIDLs[s.FilePath].Services[service]
if !ok {
return fmt.Errorf("service:%s not found in thrift idl:%s", service, s.FilePath)
}
method, ok := serviceItem.Methods[api]
if !ok {
return fmt.Errorf("method:%s not found in service:%s, thrift idl:%s", api, service, s.FilePath)
}
if _, err := iprot.ReadStructBegin(); err != nil {
return fmt.Errorf("service:%s api:%s read error: %s", service, api, err)
}
var v interface{}
for {
_, fieldTypeId, fieldId, err := iprot.ReadFieldBegin()
if err != nil {
return fmt.Errorf("field %d read error: %s", fieldId, err)
}
if fieldTypeId == thrift.STOP {
break
}
if v, err = s.readBaseType(method.ReturnType, iprot); err != nil {
return err
}
value[fmt.Sprintf("%s_result", method.Name)] = v
}
if err := iprot.ReadStructEnd(); err != nil {
return fmt.Errorf("service:%s api:%s read struct end error: %s", service, api, err)
}
return nil
}