forked from AmandaCameron/protoc-gen-gokit
/
main.go
217 lines (174 loc) · 4.4 KB
/
main.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
// (setenv "GOPATH" "/Users/amanda/Bazel/go/.external:/Users/amanda/Bazel/go")
package main
import (
"bytes"
"io/ioutil"
"os"
"strings"
"github.com/golang/protobuf/proto"
google_protobuf "github.com/golang/protobuf/protoc-gen-go/descriptor"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/AmandaCameron/protoc-gen-gokit/protobuf/google/api"
)
func parsePath(msgs map[string]message, method *google_protobuf.MethodDescriptorProto, path string) (ret []field) {
parts := strings.Split(path, "/")
for _, p := range parts {
if len(p) > 0 && p[0] == '{' && p[len(p)-1] == '}' {
found := false
for _, field := range msgs[method.GetInputType()].Fields {
if field.ProtoName == p[1:len(p)-1] {
ret = append(ret, field)
found = true
}
}
if !found {
ret = append(ret, field{"", ""})
}
} else {
ret = append(ret, field{"", ""})
}
}
return
}
type service struct {
GoName string
Methods []method
}
type method struct {
GoName string
GoInputType string
InputType string
Input message
PathArgs []field
Path string
Method string
}
type message struct {
Fields []field
}
type field struct {
ProtoName string
GoName string
}
func goise(name string) string {
if tmp := strings.Split(name, "."); len(tmp) > 0 {
name = tmp[len(tmp)-1]
}
return CamelCase(name)
}
func main() {
msg := plugin.CodeGeneratorRequest{}
buff, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
if err := proto.Unmarshal(buff, &msg); err != nil {
panic(err)
}
ret := &plugin.CodeGeneratorResponse{}
defer func() {
buff, _ := proto.Marshal(ret)
os.Stdout.Write(buff)
}()
param := msg.GetParameter()
imports := map[string]string{}
sources := map[string]string{}
for _, p := range strings.Split(param, ",") {
if len(p) == 0 {
continue
}
if p[0] == 'M' {
parts := strings.Split(p[1:], "=")
imports[parts[0]] = parts[1]
}
}
messages := map[string]message{}
for _, file := range msg.GetProtoFile() {
for _, msg := range file.GetMessageType() {
m := message{}
for _, f := range msg.GetField() {
m.Fields = append(m.Fields, field{
ProtoName: f.GetName(),
GoName: goise(f.GetName()),
})
}
messages["."+file.GetPackage()+"."+msg.GetName()] = m
sources["."+file.GetPackage()+"."+msg.GetName()] = file.GetName()
}
}
for _, file := range msg.GetProtoFile() {
services := map[string]service{}
goPackage := "main"
if file.GetOptions() != nil {
goPackage = file.GetOptions().GetGoPackage()
}
for _, svc := range file.GetService() {
s := service{
GoName: goise(svc.GetName()),
}
for _, meth := range svc.GetMethod() {
m := method{
GoName: goise(meth.GetName()),
GoInputType: goise(meth.GetInputType()),
Input: messages[meth.GetInputType()],
InputType: meth.GetInputType(),
}
if tmp, err := proto.GetExtension(meth.GetOptions(), google_api.E_Http); err == nil {
http := tmp.(*google_api.HttpRule)
if http.Get != "" {
m.PathArgs = parsePath(messages, meth, http.Get)
m.Path = http.Get
m.Method = "GET"
}
if http.Put != "" {
m.PathArgs = parsePath(messages, meth, http.Put)
m.Path = http.Put
m.Method = "PUT"
}
if http.Post != "" {
m.PathArgs = parsePath(messages, meth, http.Post)
m.Path = http.Post
m.Method = "POST"
}
if http.Delete != "" {
m.PathArgs = parsePath(messages, meth, http.Delete)
m.Path = http.Delete
m.Method = "DELETE"
}
}
s.Methods = append(s.Methods, m)
}
if len(s.Methods) > 0 {
services[svc.GetName()] = s
}
}
if len(services) > 0 {
fname := strings.Replace(file.GetName(), ".proto", ".pb.kit.go", 1)
buff := bytes.NewBuffer(nil)
imps := map[string]string{}
impUsages := map[string]int{}
for _, svc := range services {
for _, meth := range svc.Methods {
src := sources[meth.InputType]
if src == file.GetName() {
continue
}
impUsages[src] = impUsages[src] + 1
}
}
for k := range impUsages {
imps[k] = imports[k]
}
fileTemplate.Execute(buff, struct {
Package string
Imports map[string]string
Services map[string]service
}{goPackage, imps, services})
data := buff.String()
ret.File = append(ret.File, &plugin.CodeGeneratorResponse_File{
Name: &fname,
Content: &data,
})
}
}
}