forked from infiotinc/gqlgenc
/
source_generator.go
428 lines (349 loc) · 9.88 KB
/
source_generator.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
package clientgen
import (
"fmt"
"go/types"
"math"
"sort"
"strings"
"github.com/99designs/gqlgen/codegen/config"
"github.com/99designs/gqlgen/codegen/templates"
config2 "github.com/maaft/gqlgenc/config"
"github.com/vektah/gqlparser/v2/ast"
)
type FieldPath struct {
Kind ast.DefinitionKind
path []string
}
func NewFieldPath(kind ast.DefinitionKind, name string) FieldPath {
return FieldPath{
Kind: kind,
path: []string{name},
}
}
func (p FieldPath) With(n string) FieldPath {
p.path = append(p.path, n)
return p
}
func (p FieldPath) Name() string {
pn := make([]string, 0, len(p.path))
for _, n := range p.path {
pn = append(pn, templates.ToGo(n))
}
return strings.Join(pn, "_")
}
func (p FieldPath) String() string {
return strings.Join(p.path, ".")
}
type Argument struct {
Variable string
Type types.Type
}
type ResponseField struct {
Name string
IsFragmentSpread bool
IsInlineFragment bool
Type types.Type
Tags []string
ResponseFields ResponseFieldList
}
type ResponseFieldList []*ResponseField
func (rs ResponseFieldList) IsFragment() bool {
if len(rs) != 1 {
return false
}
return rs[0].IsInlineFragment || rs[0].IsFragmentSpread
}
func (rs ResponseFieldList) IsBasicType() bool {
return len(rs) == 0
}
func (rs ResponseFieldList) IsStructType() bool {
return len(rs) > 0 && !rs.IsFragment()
}
type genType struct {
name string
typ *Type
}
type PtrType struct {
Name string
Type types.Type
}
type SourceGenerator struct {
cfg *config.Config
ccfg *config2.Config
binder *config.Binder
client config.PackageConfig
genTypes []genType
ptrTypes map[types.Type]PtrType
}
func (r *SourceGenerator) RegisterGenType(name string, typ *Type) {
if gt := r.GetGenType(name); gt != nil {
panic(name + ": gen type already defined")
}
if typ.RefType == nil {
typ.RefType = types.NewNamed(
types.NewTypeName(0, r.client.Pkg(), name, nil),
nil,
nil,
)
}
r.genTypes = append(r.genTypes, genType{
name: name,
typ: typ,
})
}
func (r *SourceGenerator) GetGenType(name string) *Type {
for _, gt := range r.genTypes {
if gt.name == name {
return gt.typ
}
}
return nil
}
func (r *SourceGenerator) GenTypes() []*Type {
typs := make([]*Type, 0)
for _, gt := range r.genTypes {
typs = append(typs, gt.typ)
}
sort.SliceStable(typs, func(i, j int) bool {
pi := typs[i].Path.path
pj := typs[j].Path.path
for n := 0; n < int(math.Min(float64(len(pi)), float64(len(pj)))); n++ {
if pi[n] != pj[n] {
return pi[n] < pj[n]
}
}
return len(pi) < len(pj)
})
return typs
}
func (r *SourceGenerator) PtrTypes() []PtrType {
typs := make([]PtrType, 0)
for _, t := range r.ptrTypes {
typs = append(typs, t)
}
sort.SliceStable(typs, func(i, j int) bool {
pi := typs[i].Name
pj := typs[j].Name
return pi < pj
})
return typs
}
func NewSourceGenerator(cfg *config.Config, ccfg *config2.Config, client config.PackageConfig) *SourceGenerator {
return &SourceGenerator{
cfg: cfg,
ccfg: ccfg,
binder: cfg.NewBinder(),
client: client,
}
}
func (r *SourceGenerator) addTypenameIfInlineFragment(selectionSet *ast.SelectionSet) {
for _, s := range *selectionSet {
switch s.(type) {
case *ast.InlineFragment:
for _, s := range *selectionSet {
if field, ok := s.(*ast.Field); ok {
if field.Alias == "__typename" {
return // Already has it
}
}
}
*selectionSet = append(ast.SelectionSet{&ast.Field{
Name: "__typename",
Alias: "__typename",
Definition: &ast.FieldDefinition{
Name: "Typename",
Type: ast.NonNullNamedType("String", nil),
Arguments: ast.ArgumentDefinitionList{
{Name: "name", Type: ast.NonNullNamedType("String", nil)},
},
},
}}, *selectionSet...)
return
}
}
}
func (r *SourceGenerator) NewResponseFields(path FieldPath, selectionSet *ast.SelectionSet) ResponseFieldList {
r.addTypenameIfInlineFragment(selectionSet)
responseFields := make(ResponseFieldList, 0, len(*selectionSet))
for _, selection := range *selectionSet {
rf := r.NewResponseField(path, selection)
responseFields = append(responseFields, rf)
}
return responseFields
}
func (r *SourceGenerator) GetNamedType(fullname string) types.Type {
if gt := r.GetGenType(fullname); gt != nil {
return gt.Type
}
if r.cfg.Models.Exists(fullname) && len(r.cfg.Models[fullname].Model) > 0 {
model := r.cfg.Models[fullname].Model[0]
typ, err := r.binder.FindTypeFromName(model)
if err != nil {
panic(fmt.Errorf("cannot get type for %v (%v): %w", fullname, model, err))
}
if n, is := typ.(*types.Named); is {
return n.Underlying()
}
return typ
}
return nil
}
func (r *SourceGenerator) namedType(path FieldPath, gen func() types.Type) types.Type {
fullname := path.Name()
if gt := r.GetGenType(fullname); gt != nil {
return gt.RefType
}
if r.cfg.Models.Exists(fullname) && len(r.cfg.Models[fullname].Model) > 0 {
model := r.cfg.Models[fullname].Model[0]
typ, err := r.binder.FindTypeFromName(model)
if err != nil {
panic(fmt.Errorf("cannot get type for %v (%v): %w", fullname, model, err))
}
return typ
} else {
genTyp := &Type{
Name: fullname,
Path: path,
}
r.RegisterGenType(fullname, genTyp)
genTyp.Type = gen()
return genTyp.RefType
}
}
func (r *SourceGenerator) genFromResponseFields(path FieldPath, fieldsResponseFields ResponseFieldList) types.Type {
fullname := path.Name()
vars := make([]*types.Var, 0, len(fieldsResponseFields))
tags := make([]string, 0, len(fieldsResponseFields))
unmarshalTypes := map[string]TypeTarget{}
for _, field := range fieldsResponseFields {
typ := field.Type
fieldName := templates.ToGo(field.Name)
if field.IsInlineFragment {
unmarshalTypes[field.Name] = TypeTarget{
Type: typ,
Name: fieldName,
}
typ = types.NewPointer(typ)
}
vars = append(vars, types.NewVar(0, nil, fieldName, typ))
tags = append(tags, strings.Join(field.Tags, " "))
}
genType := r.GetGenType(fullname)
genType.UnmarshalTypes = unmarshalTypes
return types.NewStruct(vars, tags)
}
func (r *SourceGenerator) AstTypeToType(path FieldPath, fields ResponseFieldList, typ *ast.Type) types.Type {
switch {
case fields.IsBasicType():
def := r.cfg.Schema.Types[typ.Name()]
return r.namedType(NewFieldPath(def.Kind, def.Name), func() types.Type {
return r.genFromDefinition(def)
})
case fields.IsFragment():
// if a child field is fragment, this field type became fragment.
return fields[0].Type
case fields.IsStructType():
return r.namedType(path, func() types.Type {
return r.genFromResponseFields(path, fields)
})
default:
// ここにきたらバグ
// here is bug
panic("not match type")
}
}
func (r *SourceGenerator) NewResponseField(path FieldPath, selection ast.Selection) *ResponseField {
switch selection := selection.(type) {
case *ast.Field:
fieldPath := path.With(selection.Name)
fieldsResponseFields := r.NewResponseFields(fieldPath, &selection.SelectionSet)
baseType := r.AstTypeToType(fieldPath, fieldsResponseFields, selection.Definition.Type)
// GraphQLの定義がオプショナルのはtypeのポインタ型が返り、配列の定義場合はポインタのスライスの型になって返ってきます
// return pointer type then optional type or slice pointer then slice type of definition in GraphQL.
typ := r.binder.CopyModifiersFromAst(selection.Definition.Type, baseType)
return &ResponseField{
Name: selection.Alias,
Type: typ,
Tags: []string{
fmt.Sprintf(`json:"%s"`, selection.Alias),
},
ResponseFields: fieldsResponseFields,
}
case *ast.FragmentSpread:
fieldsResponseFields := r.NewResponseFields(path, &selection.Definition.SelectionSet)
name := selection.Definition.Name
typ := r.namedType(NewFieldPath(selection.ObjectDefinition.Kind, name), func() types.Type {
panic(fmt.Sprintf("fragment %v must already be generated", name))
})
return &ResponseField{
Name: selection.Name,
Type: typ,
IsFragmentSpread: true,
ResponseFields: fieldsResponseFields,
}
case *ast.InlineFragment:
// InlineFragmentは子要素をそのままstructとしてもつので、ここで、構造体の型を作成します
path := path.With(selection.TypeCondition)
fieldsResponseFields := r.NewResponseFields(path, &selection.SelectionSet)
typ := r.namedType(path, func() types.Type {
return r.genFromResponseFields(path, fieldsResponseFields)
})
return &ResponseField{
Name: selection.TypeCondition,
Type: typ,
IsInlineFragment: true,
ResponseFields: fieldsResponseFields,
Tags: []string{`json:"-"`},
}
}
panic("unexpected selection type")
}
func (r *SourceGenerator) collectPtrTypes(t types.Type, ptr bool) {
if r.ptrTypes == nil {
r.ptrTypes = map[types.Type]PtrType{}
}
if _, ok := r.ptrTypes[t]; ok {
return
}
if p, is := t.(*types.Pointer); is {
r.collectPtrTypes(p.Elem(), true)
} else if n, is := t.(*types.Named); is {
if ptr {
r.ptrTypes[n] = PtrType{
Name: n.Obj().Name(),
Type: n,
}
}
t := r.GetNamedType(n.Obj().Name())
if t == nil {
return
}
if s, is := t.(*types.Struct); is {
for i := 0; i < s.NumFields(); i++ {
r.collectPtrTypes(s.Field(i).Type(), false)
}
}
} else if b, is := t.(*types.Basic); is {
if ptr {
r.ptrTypes[b] = PtrType{
Name: b.Name(),
Type: b,
}
}
}
}
func (r *SourceGenerator) OperationArguments(variableDefinitions ast.VariableDefinitionList) []*Argument {
argumentTypes := make([]*Argument, 0, len(variableDefinitions))
for _, v := range variableDefinitions {
baseType := r.namedType(NewFieldPath(v.Definition.Kind, v.Definition.Name), func() types.Type {
return r.genFromDefinition(v.Definition)
})
typ := r.binder.CopyModifiersFromAst(v.Type, baseType)
r.collectPtrTypes(typ, false)
argumentTypes = append(argumentTypes, &Argument{
Variable: v.Variable,
Type: typ,
})
}
return argumentTypes
}