forked from danielvladco/go-proto-gql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
653 lines (563 loc) · 21 KB
/
plugin.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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
package plugin
import (
"bytes"
"errors"
"fmt"
"log"
"os"
"reflect"
"strconv"
"strings"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
"github.com/gogo/protobuf/types"
golangproto "github.com/golang/protobuf/proto"
golangdescriptor "github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/golang/protobuf/ptypes/any"
"google.golang.org/genproto/googleapis/api/annotations"
"github.com/csharpru/go-proto-gql/pb"
)
const (
ScalarBytes = "Bytes"
ScalarFloat32 = "Float32"
ScalarInt64 = "Int64"
ScalarInt32 = "Int32"
ScalarUint32 = "Uint32"
ScalarUint64 = "Uint64"
ScalarAny = "Any"
ScalarDirective = "__Directive"
ScalarType = "__Type"
ScalarField = "__Field"
ScalarEnumValue = "__EnumValue"
ScalarInputValue = "__InputValue"
ScalarSchema = "__Schema"
ScalarInt = "Int"
ScalarFloat = "Float"
ScalarString = "String"
ScalarBoolean = "Boolean"
ScalarID = "ID"
)
func NewPlugin() *Plugin {
return &Plugin{
fileIndex: -1,
types: make(map[string]*Type),
inputs: make(map[string]*Type),
enums: make(map[string]*Type),
maps: make(map[string]*Type),
oneofs: make(map[string]*Type),
oneofsRef: make(map[OneofRef]string),
scalars: map[string]*Type{
"__Directive": {ModelDescriptor: ModelDescriptor{BuiltIn: true}},
"__Type": {ModelDescriptor: ModelDescriptor{BuiltIn: true}},
"__Field": {ModelDescriptor: ModelDescriptor{BuiltIn: true}},
"__EnumValue": {ModelDescriptor: ModelDescriptor{BuiltIn: true}},
"__InputValue": {ModelDescriptor: ModelDescriptor{BuiltIn: true}},
"__Schema": {ModelDescriptor: ModelDescriptor{BuiltIn: true}},
"Int": {ModelDescriptor: ModelDescriptor{BuiltIn: true}},
"Float": {ModelDescriptor: ModelDescriptor{BuiltIn: true}},
"String": {ModelDescriptor: ModelDescriptor{BuiltIn: true}},
"Boolean": {ModelDescriptor: ModelDescriptor{BuiltIn: true}},
"ID": {ModelDescriptor: ModelDescriptor{BuiltIn: true}},
},
logger: log.New(os.Stderr, "protoc-gen-gogql: ", log.Lshortfile),
}
}
type Plugin struct {
*generator.Generator
// info for the current schema
mutations Methods
queries Methods
subscriptions Methods
inputs map[string]*Type // map containing string representation of proto message that is used as gql input
types map[string]*Type // map containing string representation of proto message that is used as gql type
enums map[string]*Type // map containing string representation of proto enum that is used as gql enum
scalars map[string]*Type
maps map[string]*Type
oneofs map[string]*Type
oneofsRef map[OneofRef]string // map containing reference to gql oneof names, we need this since oneofs cant fe found by name
fileIndex int
schemas []*schema
gqlModelNames []map[*Type]string // map containing reference to gql type names
logger *log.Logger
}
func (p *Plugin) GetOneof(ref OneofRef) *Type { return p.oneofs[p.oneofsRef[ref]] }
func (p *Plugin) Oneofs() map[string]*Type { return p.oneofs }
func (p *Plugin) Maps() map[string]*Type { return p.maps }
func (p *Plugin) Scalars() map[string]*Type { return p.scalars }
func (p *Plugin) Enums() map[string]*Type { return p.enums }
func (p *Plugin) Types() map[string]*Type { return p.types }
func (p *Plugin) Inputs() map[string]*Type { return p.inputs }
func (p *Plugin) Subscriptions() Methods { return p.subscriptions }
func (p *Plugin) Queries() Methods { return p.queries }
func (p *Plugin) Mutations() Methods { return p.mutations }
func (p *Plugin) GetSchemaByIndex(index int) *bytes.Buffer { return p.schemas[index].buffer }
func (p *Plugin) IsAny(typeName string) (ok bool) {
if len(typeName) > 0 && typeName[0] == '.' {
messageType := proto.MessageType(strings.TrimPrefix(typeName, "."))
if messageType == nil {
messageType = golangproto.MessageType(strings.TrimPrefix(typeName, "."))
if messageType == nil {
return false
}
_, ok = reflect.New(messageType).Elem().Interface().(*any.Any)
return ok
}
_, ok = reflect.New(messageType).Elem().Interface().(*types.Any)
return ok
}
return false
}
// same isEmpty but for mortals
func (p *Plugin) IsEmpty(t *Type) bool { return p.isEmpty(t, make(map[*Type]struct{})) }
// make sure objects are fulled with all types
func (p *Plugin) isEmpty(t *Type, callstack map[*Type]struct{}) bool {
callstack[t] = struct{}{} // don't forget to free stack on calling return when needed
if t.DescriptorProto != nil {
if len(t.DescriptorProto.GetField()) == 0 {
return true
} else {
for _, f := range t.GetField() {
if !f.IsMessage() {
delete(callstack, t)
return false
}
fieldType, ok := p.inputs[f.GetTypeName()]
if !ok {
if fieldType, ok = p.types[f.GetTypeName()]; !ok {
if fieldType, ok = p.enums[f.GetTypeName()]; !ok {
if fieldType, ok = p.scalars[f.GetTypeName()]; !ok {
if fieldType, ok = p.maps[f.GetTypeName()]; !ok {
continue
}
}
}
}
}
// check if the call stack already contains a reference to this type and prevent it from calling itself again
if _, ok := callstack[fieldType]; ok {
return true
}
if !p.isEmpty(fieldType, callstack) {
delete(callstack, t)
return false
}
}
delete(callstack, t)
return true
}
}
delete(callstack, t)
return false
}
func (p *Plugin) Error(err error, msgs ...string) {
s := strings.Join(msgs, " ") + ":" + err.Error()
_ = p.logger.Output(2, s)
os.Exit(1)
}
func (p *Plugin) Warn(msgs ...interface{}) {
_ = p.logger.Output(2, fmt.Sprintln(append([]interface{}{"WARN: "}, msgs...)...))
}
func (p *Plugin) getImportPath(file *descriptor.FileDescriptorProto) string {
fileName := strings.Split(file.GetName(), "/")
gopackageOpt := strings.Split(file.GetOptions().GetGoPackage(), ";")
if path, ok := p.ImportMap[file.GetName()]; ok {
return path
}
if len(gopackageOpt) == 1 && strings.Contains(gopackageOpt[0], "/") { // check if it is a path or if it is just the package name
return gopackageOpt[0]
}
if len(gopackageOpt) == 2 {
return gopackageOpt[0]
}
if len(fileName) > 1 {
return strings.Join(fileName[:len(fileName)-1], "/")
}
p.Warn(fmt.Errorf("go import path for %s could not be resolved correctly: use option go_package with full package name in this file", file.GetName()))
return file.GetName()
}
func (p *Plugin) getEnumType(file *descriptor.FileDescriptorProto, typeName string) *Type {
packagePrefix := "." + file.GetPackage() + "."
if strings.HasPrefix(typeName, packagePrefix) {
typeWithoutPrefix := strings.TrimPrefix(typeName, packagePrefix)
if e := getEnum(file, typeWithoutPrefix); e != nil {
return &Type{EnumDescriptorProto: e, ModelDescriptor: ModelDescriptor{
TypeName: generator.CamelCaseSlice(strings.Split(typeWithoutPrefix, ".")),
PackageDir: p.getImportPath(file),
originalPackage: file.GetPackage(),
packageName: strings.Replace(file.GetPackage(), ".", "_", -1) + "_",
}}
}
}
return nil
}
// recursively goes trough all fields of the types from messages map and fills it with child types.
// i. e. if message Type1 { Type2: field1 = 1; } exists in the map this function will look up Type2 and add Type2 to the map as well
func (p *Plugin) FillTypeMap(typeName string, messages map[string]*Type, inputField bool) {
p.fillTypeMap(typeName, messages, inputField, NewCallstack())
}
// creates oneof type with full path name
func (p *Plugin) createOneofFromParent(name string, parent *Type, callstack Callstack, inputField bool, field *descriptor.FieldDescriptorProto, file *descriptor.FileDescriptorProto) {
objects := make(map[string]*Type)
if inputField {
objects = p.inputs
} else {
objects = p.types
}
oneofName := ""
for _, typeName := range callstack.List() {
oneofName += "."
oneofName += objects[typeName].DescriptorProto.GetName()
}
// the name of the generated type
fieldType := "." + strings.Trim(parent.originalPackage, ".") + oneofName + "_" + strings.Title(generator.CamelCase(field.GetName()))
uniqueOneofName := "." + strings.Trim(parent.originalPackage, ".") + oneofName + "." + name
// create types for each field
if !inputField {
objects[fieldType] = &Type{
DescriptorProto: &descriptor.DescriptorProto{
Field: []*descriptor.FieldDescriptorProto{{TypeName: field.TypeName, Name: field.Name, Type: field.Type, Options: field.Options}},
},
ModelDescriptor: ModelDescriptor{
TypeName: generator.CamelCaseSlice(strings.Split(strings.Trim(oneofName+"."+strings.Title(field.GetName()), "."), ".")),
PackageDir: parent.PackageDir,
Index: 0,
packageName: parent.packageName,
originalPackage: parent.originalPackage,
UsedAsInput: inputField,
},
}
}
// create type for a Oneof construct
if _, ok := p.oneofs[uniqueOneofName]; !ok {
p.oneofs[uniqueOneofName] = &Type{
ModelDescriptor: ModelDescriptor{
OneofTypes: map[string]struct{}{fieldType: {}},
originalPackage: parent.originalPackage,
packageName: parent.packageName,
UsedAsInput: inputField,
Index: 0,
PackageDir: parent.PackageDir,
TypeName: generator.CamelCaseSlice(strings.Split("Is"+strings.Trim(oneofName+"."+name, "."), ".")),
},
}
} else {
p.oneofs[uniqueOneofName].OneofTypes[fieldType] = struct{}{}
}
p.oneofsRef[OneofRef{Parent: parent, Index: field.GetOneofIndex()}] = uniqueOneofName
}
func (p *Plugin) getMessageType(file *descriptor.FileDescriptorProto, typeName string) *Type {
packagePrefix := "." + file.GetPackage() + "."
if strings.HasPrefix(typeName, packagePrefix) {
typeWithoutPrefix := strings.TrimPrefix(typeName, packagePrefix)
if m := file.GetMessage(typeWithoutPrefix); m != nil {
// Any is considered to be scalar
if p.IsAny(typeName) {
p.scalars[typeName] = &Type{ModelDescriptor: ModelDescriptor{
PackageDir: "github.com/csharpru/go-proto-gql/pb", //TODO generate gqlgen.yml
TypeName: ScalarAny,
}}
return nil
}
return &Type{DescriptorProto: m, ModelDescriptor: ModelDescriptor{
TypeName: generator.CamelCaseSlice(strings.Split(typeWithoutPrefix, ".")),
PackageDir: p.getImportPath(file),
originalPackage: file.GetPackage(),
packageName: strings.Replace(file.GetPackage(), ".", "_", -1) + "_",
}}
}
}
return nil
}
func (p *Plugin) fillTypeMap(typeName string, objects map[string]*Type, inputField bool, callstack Callstack) {
callstack.Push(typeName) // if add a return statement dont forget to clean stack
defer callstack.Pop(typeName)
for _, file := range p.AllFiles().GetFile() {
if enum := p.getEnumType(file, typeName); enum != nil {
if enum.EnumDescriptorProto != nil {
p.enums[typeName] = enum
continue
}
}
if message := p.getMessageType(file, typeName); message != nil {
if message.DescriptorProto.GetOptions().GetMapEntry() { // if the message is a map
message.UsedAsInput = inputField
p.maps[typeName] = message
} else {
objects[typeName] = message
}
for _, field := range message.GetField() {
if field.OneofIndex != nil {
p.createOneofFromParent(message.OneofDecl[field.GetOneofIndex()].GetName(), message, callstack, inputField, field, file)
}
// defines scalars for unsupported graphql types
switch *field.Type {
case descriptor.FieldDescriptorProto_TYPE_BYTES:
p.scalars[ScalarBytes] = &Type{ModelDescriptor: ModelDescriptor{PackageDir: "github.com/csharpru/go-proto-gql/pb", TypeName: ScalarBytes}}
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
p.scalars[ScalarFloat32] = &Type{ModelDescriptor: ModelDescriptor{PackageDir: "github.com/csharpru/go-proto-gql/pb", TypeName: ScalarFloat32}}
case descriptor.FieldDescriptorProto_TYPE_INT64,
descriptor.FieldDescriptorProto_TYPE_SINT64,
descriptor.FieldDescriptorProto_TYPE_SFIXED64:
p.scalars[ScalarInt64] = &Type{ModelDescriptor: ModelDescriptor{PackageDir: "github.com/csharpru/go-proto-gql/pb", TypeName: ScalarInt64}}
case descriptor.FieldDescriptorProto_TYPE_INT32,
descriptor.FieldDescriptorProto_TYPE_SINT32,
descriptor.FieldDescriptorProto_TYPE_SFIXED32:
p.scalars[ScalarInt32] = &Type{ModelDescriptor: ModelDescriptor{PackageDir: "github.com/csharpru/go-proto-gql/pb", TypeName: ScalarInt32}}
case descriptor.FieldDescriptorProto_TYPE_UINT32,
descriptor.FieldDescriptorProto_TYPE_FIXED32:
p.scalars[ScalarUint32] = &Type{ModelDescriptor: ModelDescriptor{PackageDir: "github.com/csharpru/go-proto-gql/pb", TypeName: ScalarUint32}}
case descriptor.FieldDescriptorProto_TYPE_UINT64,
descriptor.FieldDescriptorProto_TYPE_FIXED64:
p.scalars[ScalarUint64] = &Type{ModelDescriptor: ModelDescriptor{PackageDir: "github.com/csharpru/go-proto-gql/pb", TypeName: ScalarUint64}}
}
if !field.IsMessage() && !field.IsEnum() {
continue
}
// check if already called and do nothing
if callstack.Has(field.GetTypeName()) {
continue
}
p.fillTypeMap(field.GetTypeName(), objects, inputField, callstack)
}
return // we know that it is not possible for multiple messages to have the same name
}
}
}
func (p *Plugin) defineGqlTypes(entries map[string]*Type, suffix ...string) {
gqlModels := make(map[string]*Type)
scalars := make(map[string]struct{})
for _, scalar := range p.scalars {
scalars[scalar.TypeName] = struct{}{}
}
for _, entry := range entries {
entryName := entry.ModelDescriptor.TypeName
// concatenate package name in case if the message has a scalar name, Any is considered a scalar
if _, ok := scalars[entryName]; ok {
entryName = entry.packageName + entryName
} else {
// add suffix to type if needed but only if it is not a scalar type
for _, sf := range suffix {
entryName += sf
}
}
// concatenate package name in case if two types with the same name are found
if sameModel, ok := gqlModels[entryName]; ok {
// redesign old type to have package namespace as well
p.gqlModelNames[p.fileIndex][sameModel] = sameModel.packageName + entryName
gqlModels[sameModel.packageName+entryName] = sameModel
entryName = entry.packageName + entryName
}
// add entries to the type map
p.gqlModelNames[p.fileIndex][entry] = entryName
gqlModels[entryName] = entry
}
}
func (p *Plugin) getMethodType(rpc *descriptor.MethodDescriptorProto) gql.Type {
if rpc.Options != nil {
v, err := proto.GetExtension(rpc.Options, gql.E_RpcType)
if err == nil {
tt := v.(*gql.Type)
if tt != nil {
return *tt
}
}
}
return p.getMethodTypeFromGoogleAnnotations(rpc)
}
func (p *Plugin) getServiceType(svc *descriptor.ServiceDescriptorProto) gql.Type {
if svc.Options != nil {
v, err := proto.GetExtension(svc.Options, gql.E_SvcType)
if err == nil {
tt := v.(*gql.Type)
if tt != nil {
return *tt
}
}
}
return gql.Type_DEFAULT
}
func (p *Plugin) PrintComments(path ...int) bool {
nl := ""
comments := p.Comments(strings.Join(func() (pp []string) {
for p := range path {
pp = append(pp, strconv.Itoa(p))
}
return
}(), ","))
if comments == "" {
return false
}
for _, line := range strings.Split(comments, "\n") {
p.schemas[p.fileIndex].P(nl, "# ", strings.TrimPrefix(line, " "))
nl = "\n"
}
return true
}
func (p *Plugin) checkInitialized() {
if p.fileIndex == -1 {
panic("file is not initialized! please use the InitFile(file) method for the file you want to generate code")
}
}
func (p *Plugin) GraphQLType(field *descriptor.FieldDescriptorProto, messagesIn map[string]*Type) string {
p.checkInitialized()
var gqltype string
switch field.GetType() {
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
gqltype = ScalarFloat
case descriptor.FieldDescriptorProto_TYPE_BYTES:
gqltype = ScalarBytes
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
gqltype = ScalarFloat32
case descriptor.FieldDescriptorProto_TYPE_INT64,
descriptor.FieldDescriptorProto_TYPE_SINT64,
descriptor.FieldDescriptorProto_TYPE_SFIXED64:
gqltype = ScalarInt64
case descriptor.FieldDescriptorProto_TYPE_INT32,
descriptor.FieldDescriptorProto_TYPE_SINT32,
descriptor.FieldDescriptorProto_TYPE_SFIXED32:
gqltype = ScalarInt32
case descriptor.FieldDescriptorProto_TYPE_UINT32,
descriptor.FieldDescriptorProto_TYPE_FIXED32:
gqltype = ScalarUint32
case descriptor.FieldDescriptorProto_TYPE_UINT64,
descriptor.FieldDescriptorProto_TYPE_FIXED64:
gqltype = ScalarUint64
case descriptor.FieldDescriptorProto_TYPE_BOOL:
gqltype = ScalarBoolean
case descriptor.FieldDescriptorProto_TYPE_STRING:
gqltype = ScalarString
case descriptor.FieldDescriptorProto_TYPE_GROUP:
p.Error(errors.New("proto2 groups are not supported please use proto3 syntax"))
case descriptor.FieldDescriptorProto_TYPE_ENUM:
gqltype = p.gqlModelNames[p.fileIndex][p.enums[field.GetTypeName()]]
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
if msg, ok := messagesIn[field.GetTypeName()]; ok {
gqltype = p.gqlModelNames[p.fileIndex][msg]
} else if scalar, ok := p.scalars[field.GetTypeName()]; ok {
gqltype = p.gqlModelNames[p.fileIndex][scalar]
} else if mp, ok := p.maps[field.GetTypeName()]; ok {
return p.gqlModelNames[p.fileIndex][mp]
} else {
panic("unknown proto field type")
}
default:
panic("unknown proto field type")
}
suffix := ""
prefix := ""
if field.IsRepeated() {
prefix += "["
suffix += "!]"
}
if resolveRequired(field) {
suffix += "!"
}
return prefix + gqltype + suffix
}
func (p *Plugin) In() {
p.checkInitialized()
p.schemas[p.fileIndex].In()
}
func (p *Plugin) Out() {
p.checkInitialized()
p.schemas[p.fileIndex].Out()
}
func (p *Plugin) P(str ...interface{}) {
p.checkInitialized()
p.schemas[p.fileIndex].P(str...)
}
func (p *Plugin) GqlModelNames() map[*Type]string {
p.checkInitialized()
return p.gqlModelNames[p.fileIndex]
}
func (p *Plugin) defineMethods(file *generator.FileDescriptor) {
for svci, svc := range file.GetService() {
for rpci, rpc := range svc.GetMethod() {
m := &Method{
Name: ToLowerFirst(svc.GetName()) + strings.Title(rpc.GetName()),
InputType: rpc.GetInputType(),
OutputType: rpc.GetOutputType(),
ServiceIndex: svci,
Index: rpci,
MethodDescriptorProto: rpc,
ServiceDescriptorProto: svc,
}
if rpc.GetClientStreaming() && rpc.GetServerStreaming() {
p.mutations = append(p.mutations, &Method{
Name: ToLowerFirst(svc.GetName()) + strings.Title(rpc.GetName()),
InputType: rpc.GetInputType(),
OutputType: "",
Phony: true,
ServiceIndex: svci,
Index: rpci,
MethodDescriptorProto: rpc,
ServiceDescriptorProto: svc,
})
}
if rpc.GetServerStreaming() {
p.subscriptions = append(p.subscriptions, m)
} else {
switch p.getMethodType(rpc) {
case gql.Type_DEFAULT:
switch ttt := p.getServiceType(svc); ttt {
case gql.Type_DEFAULT, gql.Type_MUTATION:
p.mutations = append(p.mutations, m)
case gql.Type_QUERY:
p.queries = append(p.queries, m)
}
case gql.Type_MUTATION:
p.mutations = append(p.mutations, m)
case gql.Type_QUERY:
p.queries = append(p.queries, m)
}
}
p.inputs[rpc.GetInputType()] = &Type{}
p.types[rpc.GetOutputType()] = &Type{}
}
}
}
func (p *Plugin) InitFile(file *generator.FileDescriptor) {
p.fileIndex++
p.schemas = append(p.schemas, &schema{buffer: &bytes.Buffer{}})
p.gqlModelNames = append(p.gqlModelNames, make(map[*Type]string))
// purge data for the current schema
p.types, p.inputs, p.enums, p.maps, p.scalars, p.oneofs, p.oneofsRef,
p.subscriptions, p.mutations, p.queries =
make(map[string]*Type), make(map[string]*Type), make(map[string]*Type),
make(map[string]*Type), make(map[string]*Type), make(map[string]*Type),
make(map[OneofRef]string), nil, nil, nil
p.defineMethods(file)
// get all input messages
for inputName := range p.inputs {
p.FillTypeMap(inputName, p.inputs, true)
}
// get all type messages
for inputName := range p.types {
p.FillTypeMap(inputName, p.types, false)
}
//p.mapToScalar()
p.defineGqlTypes(p.inputs, "Input")
p.defineGqlTypes(p.types)
p.defineGqlTypes(p.enums)
p.defineGqlTypes(p.maps)
p.defineGqlTypes(p.scalars)
p.defineGqlTypes(p.oneofs)
}
func (p *Plugin) getMethodTypeFromGoogleAnnotations(rpc *descriptor.MethodDescriptorProto) gql.Type {
data, err := proto.Marshal(rpc)
if err == nil {
googleRpc := &golangdescriptor.MethodDescriptorProto{}
err := golangproto.Unmarshal(data, googleRpc)
if err == nil {
v, err := golangproto.GetExtension(googleRpc.Options, annotations.E_Http)
if err == nil {
tt := v.(*annotations.HttpRule)
if tt != nil && tt.GetGet() != "" {
return gql.Type_QUERY
}
}
}
}
return gql.Type_DEFAULT
}