forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thrift_idl.go
104 lines (81 loc) · 2.08 KB
/
thrift_idl.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
package main
import (
"fmt"
"github.com/samuel/go-thrift/parser"
)
type ThriftIdlMethod struct {
Service *parser.Service
Method *parser.Method
Params []*string
Exceptions []*string
}
type ThriftIdl struct {
MethodsByName map[string]*ThriftIdlMethod
}
func fieldsToArrayById(fields []*parser.Field) []*string {
if len(fields) == 0 {
return []*string{}
}
max := 0
for _, field := range fields {
if field.Id > max {
max = field.Id
}
}
output := make([]*string, max+1, max+1)
for _, field := range fields {
if len(field.Name) > 0 {
output[field.Id] = &field.Name
}
}
return output
}
func BuildMethodsMap(thrift_files map[string]parser.Thrift) map[string]*ThriftIdlMethod {
output := make(map[string]*ThriftIdlMethod)
for _, thrift := range thrift_files {
for _, service := range thrift.Services {
for _, method := range service.Methods {
if _, exists := output[method.Name]; exists {
WARN("Thrift IDL: Method %s is defined in more services: %s and %s",
output[method.Name].Service.Name, service.Name)
}
output[method.Name] = &ThriftIdlMethod{
Service: service,
Method: method,
Params: fieldsToArrayById(method.Arguments),
Exceptions: fieldsToArrayById(method.Exceptions),
}
}
}
}
return output
}
func ReadFiles(files []string) (map[string]parser.Thrift, error) {
output := make(map[string]parser.Thrift)
thriftParser := parser.Parser{}
for _, file := range files {
files_map, _, err := thriftParser.ParseFile(file)
if err != nil {
return output, fmt.Errorf("Error parsing Thrift IDL file %s: %s", file, err)
}
for fname, parsedFile := range files_map {
output[fname] = *parsedFile
}
}
return output, nil
}
func (thriftidl *ThriftIdl) FindMethod(name string) *ThriftIdlMethod {
return thriftidl.MethodsByName[name]
}
func NewThriftIdl(idl_files []string) (*ThriftIdl, error) {
if len(idl_files) == 0 {
return nil, nil
}
thrift_files, err := ReadFiles(idl_files)
if err != nil {
return nil, err
}
return &ThriftIdl{
MethodsByName: BuildMethodsMap(thrift_files),
}, nil
}