/
module_interfaces.go
724 lines (621 loc) · 22 KB
/
module_interfaces.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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
package templates
import (
"fmt"
"go/types"
"sort"
"strings"
. "github.com/dave/jennifer/jen" //nolint:stylecheck
"github.com/iancoleman/strcase"
)
func (ps *parseState) parseGoIface(t *types.Interface, named *types.Named) (*parsedIfaceType, error) {
spec := &parsedIfaceType{
goType: t,
moduleName: ps.moduleName,
}
if named == nil {
return nil, fmt.Errorf("struct types must be named")
}
spec.name = named.Obj().Name()
if spec.name == "" {
return nil, fmt.Errorf("struct types must be named")
}
// It's safe to compare objects directly: https://github.com/golang/example/tree/1d6d2400d4027025cb8edc86a139c9c581d672f7/gotypes#objects
// (search "objects are routinely compared by the addresses of the underlying pointers")
daggerObjectIfaceMethods := make(map[types.Object]bool)
daggerObjectMethodSet := types.NewMethodSet(ps.daggerObjectIfaceType)
for i := 0; i < daggerObjectMethodSet.Len(); i++ {
daggerObjectIfaceMethods[daggerObjectMethodSet.At(i).Obj()] = false
}
goFuncTypes := []*types.Func{}
methodSet := types.NewMethodSet(named)
for i := 0; i < methodSet.Len(); i++ {
methodObj := methodSet.At(i).Obj()
// check if this is a method from the embedded DaggerObject interface
if _, ok := daggerObjectIfaceMethods[methodObj]; ok {
daggerObjectIfaceMethods[methodObj] = true
continue
}
goFuncType, ok := methodObj.(*types.Func)
if !ok {
return nil, fmt.Errorf("expected method to be a func, got %T", methodObj)
}
if !goFuncType.Exported() {
continue
}
goFuncTypes = append(goFuncTypes, goFuncType)
}
// verify the DaggerObject interface methods are all there
for methodObj, found := range daggerObjectIfaceMethods {
if !found {
return nil, fmt.Errorf("missing method %s from DaggerObject interface, which must be embedded in interfaces used in Functions and Objects", methodObj.Name())
}
}
sort.Slice(goFuncTypes, func(i, j int) bool {
return goFuncTypes[i].Pos() < goFuncTypes[j].Pos()
})
for _, goFuncType := range goFuncTypes {
funcTypeSpec, err := ps.parseGoFunc(named, goFuncType)
if err != nil {
return nil, fmt.Errorf("failed to parse method %s: %w", goFuncType.Name(), err)
}
spec.methods = append(spec.methods, funcTypeSpec)
}
// get the comment above the interface (if any)
astSpec, err := ps.astSpecForNamedType(named)
if err != nil {
return nil, fmt.Errorf("failed to find decl for named type %s: %w", spec.name, err)
}
spec.doc = astSpec.Doc.Text()
return spec, nil
}
type parsedIfaceType struct {
name string
doc string
methods []*funcTypeSpec
goType *types.Interface
moduleName string
}
var _ NamedParsedType = &parsedIfaceType{}
func (spec *parsedIfaceType) TypeDefCode() (*Statement, error) {
withIfaceArgsCode := []Code{
Lit(spec.name),
}
withIfaceOptsCode := []Code{}
if spec.doc != "" {
withIfaceOptsCode = append(withIfaceOptsCode, Id("Description").Op(":").Lit(strings.TrimSpace(spec.doc)))
}
if len(withIfaceOptsCode) > 0 {
withIfaceArgsCode = append(withIfaceArgsCode, Id("TypeDefWithInterfaceOpts").Values(withIfaceOptsCode...))
}
typeDefCode := Qual("dag", "TypeDef").Call().Dot("WithInterface").Call(withIfaceArgsCode...)
for _, method := range spec.methods {
fnTypeDefCode, err := method.TypeDefCode()
if err != nil {
return nil, fmt.Errorf("failed to convert method %s to function def: %w", method.name, err)
}
typeDefCode = dotLine(typeDefCode, "WithFunction").Call(Add(Line(), fnTypeDefCode))
}
return typeDefCode, nil
}
func (spec *parsedIfaceType) GoType() types.Type {
return spec.goType
}
func (spec *parsedIfaceType) GoSubTypes() []types.Type {
var subTypes []types.Type
for _, method := range spec.methods {
subTypes = append(subTypes, method.GoSubTypes()...)
}
return subTypes
}
func (spec *parsedIfaceType) Name() string {
return spec.name
}
func (spec *parsedIfaceType) ModuleName() string {
return spec.moduleName
}
// The code implementing the concrete struct that implements the interface and associated methods.
func (spec *parsedIfaceType) ImplementationCode() (*Statement, error) {
// the base boilerplate methods needed for all structs implementing an api type
code := Empty().
Add(spec.concreteStructDefCode()).Line().Line().
Add(spec.idDefCode()).Line().Line().
Add(spec.loadFromIDMethodCode()).Line().Line().
Add(spec.withGraphQLQuery()).Line().Line().
Add(spec.graphqlTypeMethodCode()).Line().Line().
Add(spec.graphqlIDTypeMethodCode()).Line().Line().
Add(spec.graphqlIDMethodCode()).Line().Line().
Add(spec.marshalJSONMethodCode()).Line().Line().
Add(spec.unmarshalJSONMethodCode()).Line().Line().
Add(spec.toIfaceMethodCode()).Line().Line()
// the ID method, which is not explicitly declared by the user but needed internally
idMethodCode, err := spec.concreteMethodCode(&funcTypeSpec{
name: "ID",
argSpecs: []paramSpec{{name: "ctx", isContext: true}},
returnSpec: &parsedPrimitiveType{goType: types.Typ[types.String], alias: spec.idTypeName()},
returnsError: true,
})
if err != nil {
return nil, fmt.Errorf("failed to generate ID method code: %w", err)
}
code.Add(idMethodCode).Line().Line()
// the implementations of the methods declared on the interface
for _, method := range spec.methods {
methodCode, err := spec.concreteMethodCode(method)
if err != nil {
return nil, fmt.Errorf("failed to generate method %s code: %w", method.name, err)
}
code.Add(methodCode).Line().Line()
}
return code, nil
}
func (spec *parsedIfaceType) concreteStructName() string {
return formatIfaceImplName(spec.name)
}
func (spec *parsedIfaceType) idTypeName() string {
return spec.name + "ID"
}
func (spec *parsedIfaceType) loadFromIDMethodName() string {
return fmt.Sprintf("Load%sFromID", spec.name)
}
func (spec *parsedIfaceType) idDefCode() *Statement {
return Type().Id(spec.idTypeName()).String()
}
func (spec *parsedIfaceType) concreteStructCachedFieldName(method *funcTypeSpec) string {
return strcase.ToLowerCamel(method.name)
}
/*
The struct definition for the concrete implementation of the interface. e.g.:
type customIfaceImpl struct {
query *querybuilder.Selection
id *CustomIfaceID
str *string
int *int
bool *bool
}
*/
func (spec *parsedIfaceType) concreteStructDefCode() *Statement {
return Type().Id(spec.concreteStructName()).StructFunc(func(g *Group) {
g.Id("query").Op("*").Qual("querybuilder", "Selection")
g.Id("id").Op("*").Id(spec.idTypeName())
for _, method := range spec.methods {
if method.returnSpec == nil {
continue
}
primitiveType, ok := method.returnSpec.(*parsedPrimitiveType)
if !ok {
continue
}
g.Id(spec.concreteStructCachedFieldName(method)).Op("*").Id(primitiveType.GoType().String())
}
})
}
/*
The Load*FromID method attached to the top-level Client struct for this interface. e.g.:
func LoadCustomIfaceFromID(r *Client, id CustomIfaceID) CustomIface {
q = querybuilder.Query().Client(r.GraphQLClient())
q = q.Select("loadTestCustomIfaceFromID")
q = q.Arg("id", id)
return &customIfaceImpl{
query: q,
}
}
*/
func (spec *parsedIfaceType) loadFromIDMethodCode() *Statement {
return Func().
Id(spec.loadFromIDMethodName()).
Params(Id("r").Op("*").Id("Client"), Id("id").Id(spec.idTypeName())).
Params(Id(spec.name)).
BlockFunc(func(g *Group) {
g.Id("q").Op(":=").Id("querybuilder").Dot("Query").Call().Dot("Client").Call(Id("r").Dot("GraphQLClient").Call())
g.Id("q").Op("=").Id("q").Dot("Select").Call(Lit(loadFromIDGQLFieldName(spec)))
g.Id("q").Op("=").Id("q").Dot("Arg").Call(Lit("id"), Id("id"))
g.Return(Op("&").Id(spec.concreteStructName()).Values(Dict{
Id("query"): Id("q"),
}))
})
}
/*
The WithGraphQLQuery sets the underlying query for the impl.
func (r *customIfaceImpl) WithGraphQLQuery(q *querybuilder.Selection) CustomIface {
return &customIfaceImpl{query: q}
}
*/
func (spec *parsedIfaceType) withGraphQLQuery() *Statement {
return Func().Params(Id("r").Op("*").Id(spec.concreteStructName())).
Id("WithGraphQLQuery").
Params(Id("q").Op("*").Id("querybuilder").Dot("Selection")).
Params(Id(spec.name)).
BlockFunc(func(g *Group) {
g.Return(Op("&").Id(spec.concreteStructName()).Values(Dict{Id("query"): Id("q")}))
})
}
/*
The XXX_GraphQLType method attached to the concrete implementation of the interface. e.g.:
func (r *customIfaceImpl) XXX_GraphQLType() string {
return "CustomIface"
}
*/
func (spec *parsedIfaceType) graphqlTypeMethodCode() *Statement {
return Func().Params(Id("r").Op("*").Id(spec.concreteStructName())).
Id("XXX_GraphQLType").
Params().
Params(Id("string")).
Block(Return(Lit(spec.name)))
}
/*
The XXX_GraphQLIDType method attached to the concrete implementation of the interface. e.g.:
func (r *customIfaceImpl) XXX_GraphQLIDType() string {
return "CustomIfaceID"
}
*/
func (spec *parsedIfaceType) graphqlIDTypeMethodCode() *Statement {
return Func().Params(Id("r").Op("*").Id(spec.concreteStructName())).
Id("XXX_GraphQLIDType").
Params().
Params(Id("string")).
Block(Return(Lit(spec.idTypeName())))
}
/*
The XXX_GraphQLID method attached to the concrete implementation of the interface. e.g.:
func (r *customIfaceImpl) XXX_GraphQLID(ctx context.Context) (string, error) {
id, err := r.ID(ctx)
if err != nil {
return "", err
}
return string(id), nil
}
*/
func (spec *parsedIfaceType) graphqlIDMethodCode() *Statement {
return Func().Params(Id("r").Op("*").Id(spec.concreteStructName())).
Id("XXX_GraphQLID").
Params(Id("ctx").Qual("context", "Context")).
Params(Id("string"), Id("error")).
BlockFunc(func(g *Group) {
g.List(Id("id"), Id("err")).Op(":=").Id("r").Dot("ID").Call(Id("ctx"))
g.If(Id("err").Op("!=").Nil()).Block(Return(Lit(""), Id("err")))
g.Return(Id("string").Parens(Id("id")), Nil())
})
}
/*
The MarshalJSON method attached to the concrete implementation of the interface. e.g.:
func (r *customIfaceImpl) MarshalJSON() ([]byte, error) {
if r == nil {
return []byte("\"\""), nil
}
id, err := r.ID(context.Background())
if err != nil {
return nil, err
}
return json.Marshal(id)
}
*/
func (spec *parsedIfaceType) marshalJSONMethodCode() *Statement {
return Func().Params(Id("r").Op("*").Id(spec.concreteStructName())).
Id("MarshalJSON").
Params().
Params(Id("[]byte"), Id("error")).
BlockFunc(func(g *Group) {
g.If(Id("r").Op("==").Nil()).Block(Return(Index().Byte().Parens(Lit(`""`)), Nil()))
g.List(Id("id"), Id("err")).Op(":=").Id("r").Dot("ID").Call(Qual("context", "Background").Call())
g.If(Id("err").Op("!=").Nil()).Block(Return(Nil(), Id("err")))
g.Return(Id("json").Dot("Marshal").Call(Id("id")))
})
}
/*
The UnmarshalJSON method attached to the concrete implementation of the interface. e.g.:
func (r *customIfaceImpl) UnmarshalJSON(bs []byte) error {
var id CustomIfaceID
err := json.Unmarshal(bs, &id)
if err != nil {
return err
}
*r = *dag.LoadCustomIfaceFromID(id).(*customIfaceImpl)
return nil
}
*/
func (spec *parsedIfaceType) unmarshalJSONMethodCode() *Statement {
return Func().Params(Id("r").Op("*").Id(spec.concreteStructName())).
Id("UnmarshalJSON").
Params(Id("bs").Id("[]byte")).
Params(Id("error")).
BlockFunc(func(g *Group) {
g.Var().Id("id").Id(spec.idTypeName())
g.Id("err").Op(":=").Id("json").Dot("Unmarshal").Call(Id("bs"), Op("&").Id("id"))
g.If(Id("err").Op("!=").Nil()).Block(Return(Id("err")))
g.Op("*").Id("r").Op("=").Op("*").Id(spec.loadFromIDMethodName()).
Call(Id("dag"), Id("id")).Assert(Id("*").Id(spec.concreteStructName()))
g.Return(Nil())
})
}
/*
The toIface helper method attached to the concrete implementation of the interface
that's used to convert the concrete implementation to the interface. e.g.:
func (r *customIfaceImpl) toIface() CustomIface {
if r == nil {
return nil
}
return r
}
*/
func (spec *parsedIfaceType) toIfaceMethodCode() *Statement {
return Func().Params(Id("r").Op("*").Id(spec.concreteStructName())).
Id("toIface").
Params().
Params(Id(spec.name)).
BlockFunc(func(g *Group) {
g.If(Id("r").Op("==").Nil()).Block(Return(Nil()))
g.Return(Id("r"))
})
}
/*
The code for the given interface method's concrete implementation attached to concrete
implementation struct. e.g.:
func (r *customIfaceImpl) WithSomeArg(ctx context.Context, someArg string) CustomIface {
q := r.query.Select("withSomeArg")
q = q.Arg("someArg", someArg)
// concreteMethodExecuteQueryCode...
}
*/
func (spec *parsedIfaceType) concreteMethodCode(method *funcTypeSpec) (*Statement, error) {
methodArgs := []Code{}
for _, argSpec := range method.argSpecs {
if argSpec.isContext {
// ctx context.Context case
methodArgs = append(methodArgs, Id("ctx").Qual("context", "Context"))
continue
}
argTypeCode, err := spec.concreteMethodSigTypeCode(argSpec.typeSpec)
if err != nil {
return nil, fmt.Errorf("failed to generate arg type code: %w", err)
}
methodArgs = append(methodArgs, Id(argSpec.name).Add(argTypeCode))
}
methodReturns := []Code{}
if method.returnSpec != nil {
methodReturnCode, err := spec.concreteMethodSigTypeCode(method.returnSpec)
if err != nil {
return nil, fmt.Errorf("failed to generate return type code: %w", err)
}
methodReturns = append(methodReturns, methodReturnCode)
}
if method.returnsError {
methodReturns = append(methodReturns, Id("error"))
}
gqlFieldName := strcase.ToLowerCamel(method.name)
executeQueryCode, err := spec.concreteMethodExecuteQueryCode(method)
if err != nil {
return nil, fmt.Errorf("failed to generate execute query code: %w", err)
}
return Func().Params(Id("r").Op("*").Id(spec.concreteStructName())).
Id(method.name).
Params(methodArgs...).
Params(methodReturns...).
BlockFunc(func(g *Group) {
g.Add(spec.concreteMethodCheckCachedFieldCode(method))
g.Id("q").Op(":=").Id("r").Dot("query").Dot("Select").Call(Lit(gqlFieldName))
for _, argSpec := range method.argSpecs {
if argSpec.typeSpec == nil {
// skip context
continue
}
gqlArgName := strcase.ToLowerCamel(argSpec.name)
setCode := Id("q").Op("=").Id("q").Dot("Arg").Call(Lit(gqlArgName), Id(argSpec.name))
g.Add(setCode).Line()
}
g.Add(executeQueryCode)
}), nil
}
/*
The code for binding args and executing the query for the given interface method's concrete implementation.
*/
func (spec *parsedIfaceType) concreteMethodExecuteQueryCode(method *funcTypeSpec) (*Statement, error) {
s := Empty()
switch returnType := method.returnSpec.(type) {
case nil:
/*
Void return, just need to return error. e.g.:
q := r.query.Select("void")
var response Void
q = q.Bind(&response)
return q.Execute(ctx)
*/
implTypeCode, err := spec.concreteMethodImplTypeCode(method.returnSpec)
if err != nil {
return nil, fmt.Errorf("failed to generate return type code: %w", err)
}
s.Var().Id("response").Add(implTypeCode).Line()
s.Id("q").Op("=").Id("q").Dot("Bind").Call(Op("&").Id("response")).Line()
s.Return(
Id("q").Dot("Execute").Call(Id("ctx")),
)
case *parsedPrimitiveType:
/*
Just return the primitive type response + error. e.g.:
q := r.query.Select("str")
var response string
q = q.Bind(&response)
return response, q.Execute(ctx)
*/
implTypeCode, err := spec.concreteMethodImplTypeCode(method.returnSpec)
if err != nil {
return nil, fmt.Errorf("failed to generate return type code: %w", err)
}
s.Var().Id("response").Add(implTypeCode).Line()
s.Id("q").Op("=").Id("q").Dot("Bind").Call(Op("&").Id("response")).Line()
s.Return(
Id("response"),
Id("q").Dot("Execute").Call(Id("ctx")),
)
case *parsedIfaceTypeReference, *parsedObjectTypeReference:
/*
Just object type with chained query (no error). e.g.:
return (&customIfaceImpl{}).WithGraphQLQuery(q)
*/
implTypeCode, err := spec.concreteMethodImplTypeCode(method.returnSpec)
if err != nil {
return nil, fmt.Errorf("failed to generate return type code: %w", err)
}
s.Return(Params(Op("&").Add(implTypeCode).Values()).Dot("WithGraphQLQuery").Call(Id("q")))
case *parsedSliceType:
switch underlyingReturnType := returnType.underlying.(type) {
case NamedParsedType:
/*
Need to return a slice of an object/interface. This is done by querying for the IDs and then
converting those ids into a slice of the object/interface. e.g.:
q = q.Select("id")
var idResults []struct {
Id DirectoryID
}
q = q.Bind(&idResults)
err := q.Execute(ctx)
if err != nil {
return nil, err
}
var results []*Directory
for _, idResult := range idResults {
id := idResult.Id
results = append(results, &Directory{
query: q.query.Root().Select("loadDirectoryFromID").Arg("id", id),
})
}
return results, nil
*/
// TODO: if iface is from this module then it needs namespacing...
idScalarName := fmt.Sprintf("%sID", strcase.ToCamel(underlyingReturnType.Name()))
loadFromIDQueryName := loadFromIDGQLFieldName(underlyingReturnType)
s.Id("q").Op("=").Id("q").Dot("Select").Call(Lit("id")).Line()
s.Var().Id("idResults").Index().Struct(Id("Id").Id(idScalarName)).Line()
s.Id("q").Op("=").Id("q").Dot("Bind").Call(Op("&").Id("idResults")).Line()
s.Id("err").Op(":=").Id("q").Dot("Execute").Call(Id("ctx")).Line()
s.If(Id("err").Op("!=").Nil()).Block(Return(Nil(), Id("err"))).Line()
underlyingReturnTypeCode, err := spec.concreteMethodSigTypeCode(returnType.underlying)
if err != nil {
return nil, fmt.Errorf("failed to generate underlying return type code: %w", err)
}
underlyingImplTypeCode, err := spec.concreteMethodImplTypeCode(returnType.underlying)
if err != nil {
return nil, fmt.Errorf("failed to generate underlying impl type code: %w", err)
}
s.Var().Id("results").Index().Add(underlyingReturnTypeCode).Line()
s.For(List(Id("_"), Id("idResult")).Op(":=").Range().Id("idResults")).BlockFunc(func(g *Group) {
g.Id("id").Op(":=").Id("idResult").Dot("Id")
query := Id("r").Dot("query").Dot("Root").Call().Dot("Select").Call(Lit(loadFromIDQueryName)).Dot("Arg").Call(Lit("id"), Id("id"))
g.Id("results").Op("=").Append(Id("results"), Params(Op("&").Add(underlyingImplTypeCode).Values()).Dot("WithGraphQLQuery").Call(query))
}).Line()
s.Return(Id("results"), Nil())
case *parsedPrimitiveType, nil:
/*
Need to return the slice of the primitive, e.g.:
var response []string
q = q.Bind(&response)
return response, q.Execute(ctx)
*/
implTypeCode, err := spec.concreteMethodImplTypeCode(method.returnSpec)
if err != nil {
return nil, fmt.Errorf("failed to generate return type code: %w", err)
}
s.Var().Id("response").Add(implTypeCode).Line()
s.Id("q").Op("=").Id("q").Dot("Bind").Call(Op("&").Id("response")).Line()
s.Return(
Id("response"),
Id("q").Dot("Execute").Call(Id("ctx")),
)
default:
return nil, fmt.Errorf("unsupported method return slice element type %T", underlyingReturnType)
}
default:
return nil, fmt.Errorf("unsupported method return type %T", method.returnSpec)
}
return s, nil
}
/*
Code for checking whether we have already cached the result of a primitive type in the concrete struct
e.g.:
if r.str != nil {
return *r.str, nil
}
*/
func (spec *parsedIfaceType) concreteMethodCheckCachedFieldCode(method *funcTypeSpec) *Statement {
structFieldName := spec.concreteStructCachedFieldName(method)
s := Null()
if _, ok := method.returnSpec.(*parsedPrimitiveType); ok {
s.If(Id("r").Dot(structFieldName).Op("!=").Nil()).Block(
Return(Op("*").Id("r").Dot(structFieldName), Nil()),
)
}
return s
}
/*
The code to use for the given type when used in a method signature as an arg or a return type. It's
important that this always be the expected pointer type and, if it's an interface, the actual go
interface type rather than the underlying concrete struct implementing it.
*/
func (spec *parsedIfaceType) concreteMethodSigTypeCode(argTypeSpec ParsedType) (*Statement, error) {
s := Empty()
switch argTypeSpec := argTypeSpec.(type) {
case nil:
// theoretically there should never be a void arg, but it's trivial enough to handle gracefully here...
s.Id("Void")
case *parsedPrimitiveType:
// just make sure to use the alias of the primitive type if set, e.g. if it's a type declared like
// `type MyString string` then we want to use `MyString` rather than `string`
if argTypeSpec.alias != "" {
s.Id(argTypeSpec.alias)
} else {
s.Id(argTypeSpec.GoType().String())
}
case *parsedSliceType:
// just return []T for the underlying element type
underlyingCode, err := spec.concreteMethodSigTypeCode(argTypeSpec.underlying)
if err != nil {
return nil, fmt.Errorf("failed to generate underlying type code: %w", err)
}
s.Index().Add(underlyingCode)
case *parsedObjectTypeReference:
if argTypeSpec.isPtr {
s.Op("*")
}
s.Id(argTypeSpec.name)
case *parsedIfaceTypeReference:
s.Id(argTypeSpec.name)
default:
return nil, fmt.Errorf("unsupported method signature type %T", argTypeSpec)
}
return s, nil
}
/*
The code to use for the given type when used in the actual implementation of a method. This differs from
concreteMethodSigTypeCode when the type is an interface, in which case we want to use the internal concrete
struct rather than the interface type.
*/
func (spec *parsedIfaceType) concreteMethodImplTypeCode(returnTypeSpec ParsedType) (*Statement, error) {
s := Empty()
switch returnTypeSpec := returnTypeSpec.(type) {
case nil:
s.Id("Void")
case *parsedPrimitiveType:
if returnTypeSpec.alias != "" {
s.Id(returnTypeSpec.alias)
} else {
s.Id(returnTypeSpec.GoType().String())
}
case *parsedSliceType:
underlyingTypeCode, err := spec.concreteMethodImplTypeCode(returnTypeSpec.underlying)
if err != nil {
return nil, fmt.Errorf("failed to generate underlying type code: %w", err)
}
s.Index().Add(underlyingTypeCode)
case *parsedObjectTypeReference:
s.Id(returnTypeSpec.name)
case *parsedIfaceTypeReference:
s.Id(formatIfaceImplName(returnTypeSpec.name))
default:
return nil, fmt.Errorf("unsupported method concrete return type %T", returnTypeSpec)
}
return s, nil
}
// The name of the concrete struct implementing the interface with the given name.
// If the interface is "Foo", this is "fooImpl".
func formatIfaceImplName(s string) string {
return strcase.ToLowerCamel(s) + "Impl"
}