/
build_deftree.go
472 lines (416 loc) · 14.2 KB
/
build_deftree.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package deftree
// build_deftree.go contains the functions for the creation of a deftree and
// it's component structs.
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
plugin "github.com/gogo/protobuf/protoc-gen-gogo/plugin"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/DoNewsCode/truss/svcdef/svcparse"
"github.com/DoNewsCode/truss/truss/execprotoc"
)
var gengo *generator.Generator
func initGenGo(req *plugin.CodeGeneratorRequest) {
gengo = generator.New()
gengo.Request = req
gengo.WrapTypes()
gengo.SetPackageNames()
gengo.BuildTypeNameMap()
gengo.GenerateAllFiles()
}
// New accepts a Protobuf plugin.CodeGeneratorRequest and the contents of the
// file containing the service declaration and returns a Deftree struct
func New(req *plugin.CodeGeneratorRequest, serviceFile io.Reader) (Deftree, error) {
dt := MicroserviceDefinition{}
dt.SetName(findDeftreePackage(req))
initGenGo(req)
var svc *ProtoService
var serviceFileName string
for _, file := range req.ProtoFile {
// Check if this file is one we even should examine, and if it's not,
// skip it
if file.GetPackage() != findDeftreePackage(req) {
continue
}
// This is a file we are meant to examine, so contine with its creation
// in the Deftree
newFile, err := NewFile(file, &dt)
if err != nil {
return nil, errors.Wrapf(err, "file creation of %q failed", file.GetName())
}
if len(newFile.Services) > 0 {
svc = newFile.Services[0]
serviceFileName = newFile.GetName()
}
dt.Files = append(dt.Files, newFile)
}
// AssociateComments goes through the comments in the passed in protobuf
// CodeGeneratorRequest, figures out which node within the mostly-assembled
// deftree each comment corresponds with, then uses the `SetDescription`
// method of each node to set it's description to the comment.
// The implementation of this function is in deftree/associate_comments.go
AssociateComments(&dt, req)
err := addHttpOptions(&dt, svc, serviceFile)
if err != nil {
log.WithError(err).Warnf("Error found while parsing file %v", serviceFileName)
log.Warnf("Due to the above warning(s), http options and bindings where not parsed and will not be present in the generated documentation.")
}
return &dt, nil
}
// NewFromString creates a Deftree from a string of a valid protobuf
// definition. A very useful function within tests.
func NewFromString(def string, gopath []string) (Deftree, error) {
const defFileName = "definition.proto"
protoDir, err := ioutil.TempDir("", "truss-deftree-")
if err != nil {
return nil, errors.Wrap(err, "cannot create temp directory to store proto definition")
}
defer os.RemoveAll(protoDir)
defPath := filepath.Join(protoDir, defFileName)
err = ioutil.WriteFile(defPath, []byte(def), 0666)
if err != nil {
return nil, errors.Wrap(err, "cannot write proto definition to file")
}
req, err := execprotoc.CodeGeneratorRequest([]string{defPath}, gopath)
if err != nil {
return nil, errors.Wrap(err, "cannot create a proto CodeGeneratorRequest")
}
deftree, err := New(req, strings.NewReader(def))
if err != nil {
return nil, errors.Wrap(err, "cannot create new deftree from CodeGeneratorRequest and definition")
}
return deftree, nil
}
// Finds the package name of the proto files named on the command line
func findDeftreePackage(req *plugin.CodeGeneratorRequest) string {
for _, cmdFile := range req.GetFileToGenerate() {
for _, protoFile := range req.GetProtoFile() {
if protoFile.GetName() == cmdFile {
return protoFile.GetPackage()
}
}
}
return ""
}
// Build a new deftree.File struct
func NewFile(
pfile *descriptor.FileDescriptorProto,
curNewDt *MicroserviceDefinition) (*ProtoFile, error) {
newFile := ProtoFile{}
newFile.Name = pfile.GetName()
for _, enum := range pfile.EnumType {
newEnum, err := NewEnum(enum)
if err != nil {
return nil, errors.Wrapf(err, "error converting enum %q", enum.GetName())
}
newFile.Enums = append(newFile.Enums, newEnum)
}
for _, msg := range pfile.MessageType {
newMsg, err := NewMessage(msg)
if err != nil {
return nil, errors.Wrapf(err, "error converting message %q", msg.GetName())
}
newFile.Messages = append(newFile.Messages, newMsg)
}
for _, srvc := range pfile.Service {
newSvc, err := NewService(srvc, &newFile, curNewDt)
if err != nil {
return nil, errors.Wrapf(err, "error converting service %q", srvc.GetName())
}
// Set the new services FullyQualifiedName here so that we don't have
// to pass around additional references to pfile.
newSvc.FullyQualifiedName = "." + pfile.GetPackage() + "." + newSvc.Name
newFile.Services = append(newFile.Services, newSvc)
}
// Resolve message field types after all messages in this file have been
// accounted for.
for _, msg := range newFile.Messages {
for _, f := range msg.Fields {
tn := f.Type.Name
if !strings.Contains(tn, ".") {
continue
}
enm, err := findEnum(curNewDt, &newFile, tn)
if enm != nil {
f.Type.Enum = enm
}
if err != nil {
return nil, errors.Wrapf(err, "error while searching for enum %q", tn)
}
}
}
return &newFile, nil
}
// NewEnum returns a *ProtoEnum created from a
// *descriptor.EnumDescriptorProto
func NewEnum(enum *descriptor.EnumDescriptorProto) (*ProtoEnum, error) {
newEnum := ProtoEnum{}
newEnum.SetName(enum.GetName())
// Add values to this enum
for _, val := range enum.GetValue() {
nval := EnumValue{}
nval.SetName(val.GetName())
nval.Number = int(val.GetNumber())
newEnum.Values = append(newEnum.Values, &nval)
}
return &newEnum, nil
}
// NewMessage returns a *ProtoMessage created from a
// *descriptor.DescriptorProto
func NewMessage(msg *descriptor.DescriptorProto) (*ProtoMessage, error) {
newMsg := ProtoMessage{}
newMsg.Name = *msg.Name
// Add fields to this message
for _, field := range msg.Field {
newField := MessageField{}
newField.Number = int(field.GetNumber())
newField.Name = *field.Name
newField.Type.Name = getCorrectTypeName(field)
// The label we get back is a number, translate it to a human
// readable string
label := int32(field.GetLabel())
lname := descriptor.FieldDescriptorProto_Label_name[label]
newField.Label = lname
// Detect whether this message is a map type
// This code is sampled from the source of protoc-gen-go:
// https://github.com/gogo/protobuf/blob/2c2f7268d78c9b309e301a6df31de3b6e4430dca/protoc-gen-gogo/generator/generator.go#L1816
if *field.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE {
// use the object map that protoc-gen-go uses
desc := gengo.ObjectNamed(field.GetTypeName())
if d, ok := desc.(*generator.Descriptor); ok && d.GetOptions().GetMapEntry() {
newField.IsMap = true
}
}
// Ensure this field is marked as an enum
if *field.Type == descriptor.FieldDescriptorProto_TYPE_ENUM {
obj := gengo.ObjectNamed(field.GetTypeName())
//if id, ok := obj.(*generator.ImportedDescriptor); ok {
//// It is an enum that was publicly imported.
//// We need the underlying type.
//obj = id.o
//}
enum, ok := obj.(*generator.EnumDescriptor)
if !ok {
return nil, errors.New(fmt.Sprintf("unknown enum type: %v", obj.TypeName()))
}
var err error
newField.Type.Enum, err = NewEnum(enum.EnumDescriptorProto)
if err != nil {
return nil, errors.Wrapf(err, "could not create custom enum %q", obj.TypeName())
}
}
newMsg.Fields = append(newMsg.Fields, &newField)
}
return &newMsg, nil
}
// Finds a message given a fully qualified name to that message. The provided
// path may be either a fully qualfied name of a message, or just the bare name
// for a message.
func findMessage(md *MicroserviceDefinition, newFile *ProtoFile, path string) (*ProtoMessage, error) {
if path[0] == '.' {
parts := strings.Split(path, ".")
for _, file := range md.Files {
for _, msg := range file.Messages {
if parts[2] == msg.GetName() {
return msg, nil
}
}
}
for _, msg := range newFile.Messages {
if parts[2] == msg.GetName() {
return msg, nil
}
}
} else {
for _, msg := range newFile.Messages {
if path == msg.GetName() {
return msg, nil
}
}
}
return nil, fmt.Errorf("couldn't find message")
}
// Finds an enum given a fully qualified name to that enum. The provided
// path may be either a fully qualfied name of a enum, or just the bare name
// for a enum.
func findEnum(md *MicroserviceDefinition, nf *ProtoFile, path string) (*ProtoEnum, error) {
if path[0] == '.' {
parts := strings.Split(path, ".")
for _, file := range md.Files {
for _, enm := range file.Enums {
if parts[2] == enm.GetName() {
return enm, nil
}
}
}
for _, enm := range nf.Enums {
if parts[2] == enm.GetName() {
return enm, nil
}
}
} else {
for _, enm := range nf.Enums {
if path == enm.GetName() {
return enm, nil
}
}
}
return nil, nil
}
// NewService creates a new *ProtoService from a
// descriptor.ServiceDescriptorProto. Additionally requires being passed the
// current *ProtoFile being defined and a reference to the current
// *MicroserviceDefinition being defined; this access is necessary so that the
// RequestType and ResponseType fields of each of the methods of this service
// may be set as references to the correct ProtoMessages
func NewService(
srvc *descriptor.ServiceDescriptorProto,
curNewFile *ProtoFile,
curNewDt *MicroserviceDefinition) (*ProtoService, error) {
newSvc := ProtoService{}
newSvc.Name = *srvc.Name
// Add methods to this service
for _, meth := range srvc.Method {
newMeth := ServiceMethod{}
newMeth.Name = *meth.Name
// Set this methods request and responses to point to existing
// Message types
reqMsg, err := findMessage(curNewDt, curNewFile, *meth.InputType)
if reqMsg == nil || err != nil {
return nil, fmt.Errorf("couldn't find request message of type '%v' for method '%v'", *meth.InputType, *meth.Name)
}
respMsg, err := findMessage(curNewDt, curNewFile, *meth.OutputType)
if respMsg == nil || err != nil {
return nil, fmt.Errorf("couldn't find response message of type '%v' for method '%v'", *meth.InputType, *meth.Name)
}
newMeth.RequestType = reqMsg
newMeth.ResponseType = respMsg
newSvc.Methods = append(newSvc.Methods, &newMeth)
}
return &newSvc, nil
}
// getCorrectTypeName returns the correct name for the type of the given
// FieldDescriptorProto. The GetTypeName method on FieldDescriptorProto only
// returns the path/name of a type if that type is a message or an Enum. For
// basic types (int, float, etc.) GetTypeName() returns an empty string. In
// that case, we set the newFields type name to be the string representing the
// type of the field being examined.
func getCorrectTypeName(p *descriptor.FieldDescriptorProto) string {
rv := p.GetTypeName()
if rv == "" {
rv = p.Type.String()
}
return rv
}
// Searches all descendent directories for a file with name `fname`.
func searchFileName(fname string) string {
fname = path.Base(fname)
foundPath := ""
visitor := func(path string, info os.FileInfo, err error) error {
if info.Name() == fname {
foundPath = path
}
return nil
}
_ = filepath.Walk("./", visitor)
return foundPath
}
// convertSvcparse converts the structures returned by the service parser into
// the equivalent representation as deftree structures. At this time,
// convertSvcparse won't ever return an error, but that may change at any time,
// so please do not ignore the error on this function!
func convertSvcparse(parsedSvc *svcparse.Service) (*ProtoService, error) {
rv := &ProtoService{}
rv.SetName(parsedSvc.Name)
for _, pm := range parsedSvc.Methods {
m := &ServiceMethod{
Name: pm.Name,
Description: scrubComments(pm.Description),
}
m.RequestType = &ProtoMessage{
Name: pm.RequestType,
}
m.ResponseType = &ProtoMessage{
Name: pm.ResponseType,
}
for _, pb := range pm.HTTPBindings {
b := &MethodHttpBinding{
Description: scrubComments(pb.Description),
}
for _, pf := range pb.Fields {
f := &BindingField{
Name: pf.Name,
Description: scrubComments(pf.Description),
Kind: pf.Kind,
Value: pf.Value,
}
b.Fields = append(b.Fields, f)
}
for _, pc := range pb.CustomHTTPPattern {
f := &BindingField{
Name: pc.Name,
Description: scrubComments(pc.Description),
Kind: pc.Kind,
Value: pc.Value,
}
b.CustomHTTPPattern = append(b.CustomHTTPPattern, f)
}
m.HttpBindings = append(m.HttpBindings, b)
}
rv.Methods = append(rv.Methods, m)
}
return rv, nil
}
// Parse the protobuf files for comments surrounding http options, then add
// those to the Deftree in place.
func addHttpOptions(dt Deftree, svc *ProtoService, protoFile io.Reader) error {
lex := svcparse.NewSvcLexer(protoFile)
ps, err := svcparse.ParseService(lex)
if err != nil {
return errors.Wrapf(err, "error while parsing http options for the %v service definition", svc.GetName())
}
parsedSvc, err := convertSvcparse(ps)
if err != nil {
return errors.Wrapf(err, "error while converting result of service parser for the %v service definition", svc.GetName())
}
for _, pmeth := range parsedSvc.Methods {
meth := svc.GetByName(pmeth.GetName()).(*ServiceMethod)
meth.HttpBindings = pmeth.HttpBindings
}
// Assemble the http parameters for each http binding
err = Assemble(dt)
if err != nil {
return errors.Wrap(err, "could not assemble http parameters for each http binding")
}
return nil
}
// Searches through the files in the request and returns the path to the first
// one which contains a service declaration. If no file in the request contains
// a service, returns an empty string.
func FindServiceFile(req *plugin.CodeGeneratorRequest) string {
svcFiles := []string{}
// Since the names of proto files in FileDescriptorProto's don't contain
// the path, we have to find the first one with a service, then find its
// actual relative path by searching the slice `FileToGenerate`.
for _, file := range req.GetProtoFile() {
if len(file.GetService()) > 0 {
svcFiles = append(svcFiles, file.GetName())
}
}
for _, file := range req.GetFileToGenerate() {
for _, svcF := range svcFiles {
if strings.Contains(file, svcF) {
return file
}
}
}
return ""
}