forked from lyft/protoc-gen-star
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatherer.go
569 lines (449 loc) · 13.1 KB
/
gatherer.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
package pgs
import (
"errors"
"fmt"
"strings"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/golang/protobuf/protoc-gen-go/generator"
)
const gathererPluginName = "gatherer"
type gatherer struct {
*PluginBase
entities map[string]Entity
pkgs map[string]Package
targets map[string]Package
}
func newGatherer() *gatherer { return &gatherer{PluginBase: &PluginBase{}} }
func (g *gatherer) Name() string { return gathererPluginName }
func (g *gatherer) Init(gen *generator.Generator) {
g.PluginBase.Init(gen)
g.targets = make(map[string]Package)
g.pkgs = make(map[string]Package)
g.entities = make(map[string]Entity)
}
func (g *gatherer) Generate(f *generator.FileDescriptor) {
comments := make(map[string]string)
for _, loc := range f.GetSourceCodeInfo().GetLocation() {
if loc.LeadingComments == nil {
continue
}
name, err := g.nameByPath(f.FileDescriptorProto, loc.Path)
if err != nil {
g.Debug("unable to convert path to name:", err.Error())
}
comments[name] = strings.TrimSuffix(loc.GetLeadingComments(), "\n")
}
pkg := g.hydratePackage(f, comments)
pkg.addFile(g.hydrateFile(pkg, f, comments))
}
func (g *gatherer) hydratePackage(f *generator.FileDescriptor, comments map[string]string) Package {
// TODO(btc): perhaps return error with specific info about failure
importPath := goImportPath(g.Generator.Unwrap(), f)
name := string(g.Generator.GoPackageName(importPath))
if p, n, found := goPackageOption(f); found {
if p != "" {
importPath = generator.GoImportPath(p)
}
if n != "" {
name = n
}
}
g.push("package:" + name)
defer g.pop()
// have we already hydrated this package. In case we already did, and if
// current file contains comments in the package statement, concatenate it
// so that we don't give any precedence to whatsoever file.
pcomments := comments[fmt.Sprintf(".%s", name)]
if p, ok := g.pkgs[name]; ok {
c := make([]string, 0, 2)
ccomments := p.Comments()
if ccomments != "" {
c = append(c, ccomments)
}
if pcomments != "" {
c = append(c, pcomments)
}
p.setComments(strings.Join(c, "\n"))
return p
}
p := &pkg{
fd: f,
name: name,
importPath: string(importPath),
comments: pcomments,
}
g.pkgs[name] = p
return p
}
func (g *gatherer) hydrateFile(pkg Package, f *generator.FileDescriptor, comments map[string]string) File {
fl := &file{
pkg: pkg,
desc: f,
outputPath: FilePath(goFileName(f, g.Parameters().Paths())),
}
if out, ok := g.seen(fl); ok {
return out.(*file)
}
g.add(fl)
g.push("file:" + fl.Name().String())
defer g.pop()
g.Assert(f.GetPackage() == pkg.ProtoName().String(),
"proto package names should not be mixed in the same directory (",
pkg.ProtoName().String(), ", ", f.GetPackage(), ")")
fl.buildTarget = g.BuildTarget(f.GetName())
fl.comments = comments
if _, seen := g.targets[fl.pkg.GoName().String()]; fl.buildTarget && !seen {
g.Debug("adding target package:", fl.pkg.GoName())
g.targets[fl.pkg.GoName().String()] = fl.pkg
}
fl.msgs = make([]Message, 0, len(f.GetMessageType()))
fl.enums = make([]Enum, 0, len(f.GetEnumType()))
fl.srvs = make([]Service, 0, len(f.GetService()))
// populate all enum types
for _, ed := range f.GetEnumType() {
fl.addEnum(g.hydrateEnum(fl, ed))
}
// populate all message types
for _, md := range f.GetMessageType() {
fl.addMessage(g.hydrateMessage(fl, md))
}
// populates all field types. This must come after all messages to permit
// hydrating all types prior to hydration
for _, m := range fl.AllMessages() {
// This must come after all messages but before normal message fields to
// permit the later hydration.
for _, me := range m.MapEntries() {
for _, fld := range me.Fields() {
fld.addType(g.hydrateFieldType(fld))
}
}
for _, fld := range m.Fields() {
fld.addType(g.hydrateFieldType(fld))
}
}
// populate all services
for _, sd := range f.GetService() {
fl.addService(g.hydrateService(fl, sd))
}
return fl
}
func (g *gatherer) hydrateMessage(parent ParentEntity, md *descriptor.DescriptorProto) Message {
m := &msg{
rawDesc: md,
parent: parent,
}
if out, ok := g.seen(m); ok {
return out.(*msg)
}
g.add(m)
g.push("msg:" + m.Name().String())
defer g.pop()
name := m.FullyQualifiedName()
m.genDesc = g.Generator.ObjectNamed(name).(*generator.Descriptor)
m.comments = m.File().lookupComments(name)
// populate all nested enums
for _, ed := range md.GetEnumType() {
m.addEnum(g.hydrateEnum(m, ed))
}
// populate all nested messages. If the message is a map entry type, stash it.
for _, smd := range md.GetNestedType() {
if sm := g.hydrateMessage(m, smd); sm.IsMapEntry() {
m.addMapEntry(sm)
} else {
m.addMessage(sm)
}
}
// populate all fields
for _, fd := range md.GetField() {
m.addField(g.hydrateField(m, fd))
}
// populate all oneofs. This must come after the fields to properly associate
// the field relationships
for i, od := range md.GetOneofDecl() {
m.addOneOf(g.hydrateOneOf(m, int32(i), od))
}
return m
}
func (g *gatherer) hydrateField(msg Message, fd *descriptor.FieldDescriptorProto) Field {
f := &field{
desc: fd,
msg: msg,
}
if out, ok := g.seen(f); ok {
return out.(*field)
}
g.add(f)
f.comments = f.File().lookupComments(f.FullyQualifiedName())
return f
}
func (g *gatherer) hydrateFieldType(fld Field) FieldType {
g.push("field-type:" + fld.FullyQualifiedName())
defer g.pop()
msg := fld.Message().Descriptor()
name, _ := g.Generator.GoType(msg, fld.Descriptor())
s := &scalarT{
fld: fld,
name: TypeName(name),
}
switch {
case s.ProtoType() == GroupT:
g.Fail("group types are deprecated and unsupported. Use an embedded message instead.")
return nil
case s.ProtoLabel() == Repeated:
return g.hydrateRepeatedFieldType(s)
case s.ProtoType() == EnumT:
return g.hydrateEnumFieldType(s)
case s.ProtoType() == MessageT:
return g.hydrateEmbedFieldType(s)
default:
return s
}
}
func (g *gatherer) hydrateEnumFieldType(s *scalarT) FieldType {
e := &enumT{scalarT: s}
ent, ok := g.seenObj(g.Generator.ObjectNamed(s.fld.Descriptor().GetTypeName()))
g.Assert(ok, "enum type not seen")
en, ok := ent.(Enum)
g.Assert(ok, "unexpected entity type")
e.enum = en
return e
}
func (g *gatherer) hydrateEmbedFieldType(s *scalarT) FieldType {
e := &embedT{scalarT: s}
ent, ok := g.seenObj(g.Generator.ObjectNamed(s.fld.Descriptor().GetTypeName()))
g.Assert(ok, "embed type not seen")
m, ok := ent.(Message)
g.Assert(ok, "unexpected entity type")
e.msg = m
return e
}
func (g *gatherer) hydrateRepeatedFieldType(s *scalarT) FieldType {
r := &repT{scalarT: s}
r.el = &scalarE{
typ: r,
ptype: r.ProtoType(),
name: r.Name().Element(),
}
switch s.ProtoType() {
case EnumT:
ent, ok := g.seenObj(g.Generator.ObjectNamed(s.fld.Descriptor().GetTypeName()))
g.Assert(ok, "enum type not seen")
en, ok := ent.(Enum)
g.Assert(ok, "unexpected entity type")
r.el = &enumE{
scalarE: r.el.(*scalarE),
enum: en,
}
case MessageT:
ent, ok := g.seenObj(g.Generator.ObjectNamed(s.fld.Descriptor().GetTypeName()))
g.Assert(ok, "embed type not seen")
m, ok := ent.(Message)
g.Assert(ok, "unexpected entity type")
if m.IsMapEntry() {
return g.hydrateMapFieldType(r, m)
}
r.el = &embedE{
scalarE: r.el.(*scalarE),
msg: m,
}
}
return r
}
func (g *gatherer) hydrateMapFieldType(r *repT, m Message) FieldType {
mt := &mapT{repT: r}
mt.key = m.Fields()[0].Type().toElem()
mt.key.setType(mt)
mt.el = m.Fields()[1].Type().toElem()
mt.el.setType(mt)
mt.name = TypeName(fmt.Sprintf(
"map[%s]%s",
mt.key.Name(),
mt.el.Name()))
return mt
}
func (g *gatherer) hydrateOneOf(msg Message, idx int32, od *descriptor.OneofDescriptorProto) OneOf {
o := &oneof{
desc: od,
msg: msg,
}
if out, ok := g.seen(o); ok {
return out.(*oneof)
}
g.add(o)
g.push("oneof:" + o.Name().String())
defer g.pop()
o.comments = o.File().lookupComments(o.FullyQualifiedName())
for _, f := range msg.Fields() {
if i := f.Descriptor().OneofIndex; i != nil && idx == *i {
o.addField(f)
}
}
return o
}
func (g *gatherer) hydrateEnum(parent ParentEntity, ed *descriptor.EnumDescriptorProto) Enum {
e := &enum{
rawDesc: ed,
parent: parent,
}
if out, ok := g.seen(e); ok {
return out.(*enum)
}
g.add(e)
g.push("enum:" + e.Name().String())
defer g.pop()
name := e.FullyQualifiedName()
e.genDesc = g.Generator.ObjectNamed(name).(*generator.EnumDescriptor)
e.comments = e.File().lookupComments(name)
for _, vd := range ed.GetValue() {
e.addValue(g.hydrateEnumValue(e, vd))
}
return e
}
func (g *gatherer) hydrateEnumValue(parent Enum, vd *descriptor.EnumValueDescriptorProto) EnumValue {
ev := &enumVal{
desc: vd,
enum: parent,
}
if out, ok := g.seen(ev); ok {
return out.(*enumVal)
}
g.add(ev)
ev.comments = ev.File().lookupComments(ev.FullyQualifiedName())
return ev
}
func (g *gatherer) hydrateService(parent File, sd *descriptor.ServiceDescriptorProto) Service {
s := &service{
desc: sd,
file: parent,
}
if out, ok := g.seen(s); ok {
return out.(*service)
}
g.add(s)
g.push("service:" + s.Name().String())
defer g.pop()
s.comments = s.File().lookupComments(s.FullyQualifiedName())
for _, md := range sd.GetMethod() {
s.addMethod(g.hydrateMethod(s, md))
}
return s
}
func (g *gatherer) hydrateMethod(parent Service, md *descriptor.MethodDescriptorProto) Method {
m := &method{
desc: md,
service: parent,
}
if out, ok := g.seen(m); ok {
return out.(*method)
}
g.add(m)
g.push("method:" + m.Name().String())
defer g.pop()
m.comments = m.File().lookupComments(m.FullyQualifiedName())
in, ok := g.seenName(md.GetInputType())
g.Assert(ok, "input type", md.GetInputType(), "not hydrated")
m.in = in.(*msg)
out, ok := g.seenName(md.GetOutputType())
g.Assert(ok, "output type", md.GetOutputType(), "not hydrated")
m.out = out.(*msg)
return m
}
func (g *gatherer) push(prefix string) { g.BuildContext = g.Push(prefix) }
func (g *gatherer) pop() { g.BuildContext = g.Pop() }
func (g *gatherer) seen(e Entity) (Entity, bool) { return g.seenName(g.resolveFullyQualifiedName(e)) }
func (g *gatherer) seenName(ln string) (Entity, bool) {
out, ok := g.entities[ln]
return out, ok
}
func (g *gatherer) seenObj(o generator.Object) (Entity, bool) {
ent, ok := g.seenName(o.File().GetName())
g.Assert(ok, "dependent proto file not seen:", o.File().GetName())
fl := ent.File()
return g.seenName(fl.FullyQualifiedName() + "." + strings.Join(o.TypeName(), "."))
}
func (g *gatherer) add(e Entity) { g.entities[g.resolveFullyQualifiedName(e)] = e }
func (g *gatherer) resolveFullyQualifiedName(e Entity) string {
if f, ok := e.(File); ok {
return f.Name().String()
}
return e.FullyQualifiedName()
}
func (g *gatherer) nameByPath(f *descriptor.FileDescriptorProto, path []int32) (string, error) {
const (
packagePath = 2 // FileDescriptorProto.Package
messageTypePath = 4 // FileDescriptorProto.MessageType
enumTypePath = 5 // FileDescriptorProto.EnumType
servicePath = 6 // FileDescriptorProto.Service
messageTypeFieldPath = 2 // DescriptorProto.Field
messageTypeNestedTypePath = 3 // DescriptorProto.NestedType
messageTypeEnumTypePath = 4 // DescriptorProto.EnumType
messageTypeOneofDeclPath = 8 // DescriptorProto.OneofDecl
)
// return fast in case it's the package leading comment
packageName := f.GetPackage()
if path[0] == packagePath {
return fmt.Sprintf(".%s", packageName), nil
}
// as we're refering to concrete entities, entity type should be followed by
// an index number thus always leading to even paths.
if len(path)%2 != 0 {
return "", errors.New("path must have even elements")
}
// tail-call recursive path to name conversion functor
var fn func(interface {
GetName() string
}, []int32, *[]string) error
fn = func(parent interface {
GetName() string
}, path []int32, names *[]string) error {
if len(path) == 0 {
return nil
}
t := path[0]
n := path[1]
switch td := parent.(type) {
case *descriptor.FileDescriptorProto:
switch t {
case messageTypePath:
parent = td.MessageType[n]
case enumTypePath:
parent = td.EnumType[n]
case servicePath:
parent = td.Service[n]
}
case *descriptor.ServiceDescriptorProto:
parent = td.Method[n]
case *descriptor.EnumDescriptorProto:
parent = td.Value[n]
case *descriptor.DescriptorProto:
switch t {
case messageTypeFieldPath:
parent = td.Field[n]
case messageTypeNestedTypePath:
parent = td.NestedType[n]
case messageTypeEnumTypePath:
parent = td.EnumType[n]
case messageTypeOneofDeclPath:
parent = td.OneofDecl[n]
}
}
*names = append(*names, parent.GetName())
return fn(parent, path[2:], names)
}
// reserve exactly the required capacity
var names []string
namesLen := uint(len(path) / 2)
if packageName != "" {
names = make([]string, 0, namesLen+1)
names = append(names, packageName)
} else {
names = make([]string, 0, namesLen)
}
// start the conversion
err := fn(f, path, &names)
if err != nil {
return "", err
}
return fmt.Sprintf(".%s", strings.Join(names, ".")), nil
}
var _ generator.Plugin = (*gatherer)(nil)