-
Notifications
You must be signed in to change notification settings - Fork 9
/
generator.go
1057 lines (863 loc) · 30.9 KB
/
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
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package generator
import (
"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
"bytes"
"encoding/json"
"flag"
"fmt"
"go/types"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
"text/template"
"time"
"unicode"
"github.com/pkg/errors"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/imports"
)
type goType struct {
Modifiers string
ImportPath string
ImportName string
Name string
}
type templateData struct {
Package string
ImportPaths []string
Definitions []dataloaderDefinition
Subscriptions []subscription
}
type dataloaderConfig struct {
MaxBatchSize int
BatchTimeout time.Duration
PublishResults bool
Skip bool
}
type dataloaderDefinition struct {
Name string
MaxBatchSize int
BatchTimeout int64
PublishResults bool
KeyType *goType
ResultType *goType
KeyIsComparable bool
CanAutoCacheDBID bool
Embeds []embedDefinition
IsCustomBatch bool
CustomBatching *customBatchingDefinition
}
type subscription struct {
Subscriber string
Target string
Result string
Field string
ResultIsSlice bool
SingleKey bool
ManyKeys bool
}
type autoCacheEntry struct {
LoaderName string
ResultType *goType
SingleKey bool
ManyKeys bool
}
func (t *goType) HasSameBaseType(other *goType) bool {
return t.ImportPath == other.ImportPath && t.Name == other.Name
}
func (t *goType) String() string {
if t.ImportName != "" {
return t.Modifiers + t.ImportName + "." + t.Name
}
return t.Modifiers + t.Name
}
func (t *goType) StringWithoutModifiers() string {
if t.ImportName != "" {
return t.ImportName + "." + t.Name
}
return t.Name
}
func (t *goType) FullyQualifiedStringWithoutModifiers() string {
if t.ImportPath != "" {
return t.ImportPath + "." + t.Name
}
return t.Name
}
func (t *goType) IsPtr() bool {
return strings.HasPrefix(t.Modifiers, "*")
}
func (t *goType) IsSlice() bool {
return strings.HasPrefix(t.Modifiers, "[]")
}
func (t *goType) IsSliceOfPtrs() bool {
return strings.HasPrefix(t.Modifiers, "[]*")
}
var partsRe = regexp.MustCompile(`^([\[\]\*]*)(.*?)(\.\w*)?$`)
func typeToGoType(t types.Type) (*goType, error) {
return parseType(t.String())
}
var goTypeImportNames = make(map[string]string)
func parseType(str string) (*goType, error) {
parts := partsRe.FindStringSubmatch(str)
if len(parts) != 4 {
return nil, fmt.Errorf("type must be in the form []*github.com/import/path.Name")
}
t := &goType{
Modifiers: parts[1],
ImportPath: parts[2],
Name: strings.TrimPrefix(parts[3], "."),
}
if t.Name == "" {
t.Name = t.ImportPath
t.ImportPath = ""
}
if t.ImportPath != "" {
p, ok := goTypeImportNames[t.ImportPath]
if !ok {
pkgs, err := packages.Load(&packages.Config{Mode: packages.NeedName}, t.ImportPath)
if err != nil {
return nil, err
}
if len(pkgs) != 1 {
return nil, fmt.Errorf("not found")
}
p = pkgs[0].Name
goTypeImportNames[t.ImportPath] = p
}
t.ImportName = p
}
return t, nil
}
func getImportPaths(defs []dataloaderDefinition) []string {
outputImports := make([]string, 0)
seenImports := map[string]bool{}
addImport := func(importPath string) {
if importPath == "" {
return
}
if _, ok := seenImports[importPath]; !ok {
seenImports[importPath] = true
outputImports = append(outputImports, importPath)
}
}
for _, def := range defs {
addImport(def.KeyType.ImportPath)
addImport(def.ResultType.ImportPath)
}
return outputImports
}
func newSubscription(entry autoCacheEntry, target dataloaderDefinition, field string) subscription {
return subscription{
Subscriber: entry.LoaderName,
Target: target.Name,
Result: target.ResultType.String(),
ResultIsSlice: target.ResultType.IsSlice(),
Field: field,
SingleKey: entry.SingleKey,
ManyKeys: entry.ManyKeys,
}
}
const notFoundImplementationStr = `
type %s must implement getNotFoundError. Add this signature to notfound.go and have it return an appropriate error:
func (*%s) getNotFoundError(key %s) error {
// TODO: Return a specific error type, not pgx.ErrNoRows
}
`
func generateFiles(defs []dataloaderDefinition, outputDir string) error {
// Delete the old api_gen.go file to ensure that any compiler errors present in the old file
// won't stop us from parsing the package and writing a new one
apiFile := filepath.Join(outputDir, "api_gen.go")
if err := os.Remove(apiFile); err != nil && !os.IsNotExist(err) {
failWithErr(fmt.Errorf("error deleting old api_gen.go file: %v", err))
}
genPkg := getPackage(outputDir)
if genPkg == nil {
return fmt.Errorf("unable to find package info for " + outputDir)
}
importPaths := getImportPaths(defs)
data := templateData{
Package: genPkg.Name,
ImportPaths: importPaths,
Definitions: defs,
}
dataloadersFile := filepath.Join(outputDir, "dataloaders_gen.go")
if err := writeTemplate(dataloadersFile, dataloadersTemplate, data); err != nil {
return err
}
generatedPkg := loadPackage(outputDir)
// Check for required getNotFoundError implementations
for _, def := range defs {
if def.IsCustomBatch || !def.ResultType.IsSlice() {
obj := generatedPkg.Types.Scope().Lookup(def.Name)
if obj == nil {
failWithErr(fmt.Errorf("%s not found in declared types of %s", def.Name, generatedPkg))
}
if namedType, ok := obj.Type().(*types.Named); ok {
if !implementsGetNotFoundError(namedType, def.KeyType) {
return fmt.Errorf(notFoundImplementationStr, def.Name, def.Name, def.KeyType.String())
}
} else {
return fmt.Errorf("type %s must be a named type", def.Name)
}
}
}
// Map every dataloader to a list of its result types (both the top-level TResult and any sqlc.embed subfields)
defsByResultType := make(map[string][]dataloaderDefinition)
for _, def := range defs {
defsByResultType[def.ResultType.FullyQualifiedStringWithoutModifiers()] = append(defsByResultType[def.ResultType.FullyQualifiedStringWithoutModifiers()], def)
for _, embed := range def.Embeds {
defsByResultType[embed.FieldType.FullyQualifiedStringWithoutModifiers()] = append(defsByResultType[embed.FieldType.FullyQualifiedStringWithoutModifiers()], def)
}
}
data.Subscriptions = make([]subscription, 0)
// Find all dataloaders that implement the getKeyForResult or getKeysForResult functions with
// appropriate [TKey, TResult] values
autoCacheEntries := getAutoCacheEntries(generatedPkg, defs)
// For every dataloader that can cache published results, loop through all other dataloaders
// to see which ones return results of the appropriate type. This includes checking for sqlc.embed
// subfields that can be cached, too.
for _, entry := range autoCacheEntries {
targets, ok := defsByResultType[entry.ResultType.FullyQualifiedStringWithoutModifiers()]
if !ok {
continue
}
for _, target := range targets {
// Don't subscribe loaders to their own results
if entry.LoaderName == target.Name {
continue
}
// If this entry can subscribe to the target's type, add it to the list of subscriptions
if target.ResultType.HasSameBaseType(entry.ResultType) {
data.Subscriptions = append(data.Subscriptions, newSubscription(entry, target, ""))
continue
}
// If the entry couldn't subscribe to the target type, it must be able to subscribe to one of the target's fields
for _, embed := range target.Embeds {
if embed.FieldType.HasSameBaseType(entry.ResultType) {
data.Subscriptions = append(data.Subscriptions, newSubscription(entry, target, "."+embed.FieldName))
}
}
}
}
if err := writeTemplate(apiFile, apiTemplate, data); err != nil {
return err
}
fmt.Printf("wrote %d dataloaders to:\n %s\n %s\n", len(defs), dataloadersFile, apiFile)
return nil
}
func newDataloaderDefinition(name string, keyType types.Type, resultType types.Type, maxBatchSize int, batchTimeout time.Duration, publishResults bool, genPkgPath string) dataloaderDefinition {
data := dataloaderDefinition{
Name: name,
MaxBatchSize: maxBatchSize,
BatchTimeout: batchTimeout.Nanoseconds(),
PublishResults: publishResults,
KeyIsComparable: types.Comparable(keyType),
}
var err error
data.KeyType, err = typeToGoType(keyType)
if err != nil {
failWithErr(fmt.Errorf("key type: %s", err.Error()))
}
data.ResultType, err = typeToGoType(resultType)
if err != nil {
failWithErr(fmt.Errorf("result type: %s", err.Error()))
}
// if we are inside the same package as the type we don't need an import and can refer directly to the type
if genPkgPath == data.ResultType.ImportPath {
data.ResultType.ImportName = ""
data.ResultType.ImportPath = ""
}
if genPkgPath == data.KeyType.ImportPath {
data.KeyType.ImportName = ""
data.KeyType.ImportPath = ""
}
return data
}
func getPackage(dir string) *packages.Package {
p, _ := packages.Load(&packages.Config{
Dir: dir,
}, ".")
if len(p) != 1 {
return nil
}
return p[0]
}
func writeTemplate(filepath string, tpl *template.Template, data any) error {
var buf bytes.Buffer
if err := tpl.Execute(&buf, data); err != nil {
return errors.Wrap(err, "generating code")
}
src, err := imports.Process(filepath, buf.Bytes(), nil)
if err != nil {
return errors.Wrap(err, "unable to gofmt")
}
if err := os.WriteFile(filepath, src, 0644); err != nil {
return errors.Wrap(err, "writing output")
}
return nil
}
func lcFirst(s string) string {
r := []rune(s)
r[0] = unicode.ToLower(r[0])
return string(r)
}
func parseDataloaderConfig(comments []string) (*dataloaderConfig, error) {
const withDataloader = "dataloader-config:"
for _, comment := range comments {
comment = strings.TrimSpace(comment)
if strings.HasPrefix(comment, withDataloader) {
return parseConfigOptions(strings.TrimSpace(comment[len(withDataloader):]))
}
}
return parseConfigOptions("")
}
func parseConfigOptions(optionStr string) (*dataloaderConfig, error) {
config := &dataloaderConfig{}
fs := flag.NewFlagSet("dataloader generator", flag.ContinueOnError)
fs.IntVar(&config.MaxBatchSize, "maxBatchSize", 100, "max batch size")
fs.DurationVar(&config.BatchTimeout, "batchTimeout", 2*time.Millisecond, "batch timeout")
fs.BoolVar(&config.PublishResults, "publishResults", true, "publish results")
fs.BoolVar(&config.Skip, "skip", false, "don't generate a dataloader")
// Add -- prefix to each option so we can parse it with the flag package
args := strings.Fields(optionStr)
for i, arg := range args {
args[i] = "--" + arg
}
err := fs.Parse(args)
if err != nil {
return nil, err
}
return config, nil
}
func parseManifest(path string) *plugin.CodeGenRequest {
var req plugin.CodeGenRequest
manifestFile, err := os.Open(path)
if err != nil {
failWithErr(fmt.Errorf("error opening manifest file at \"%s\": %v", path, err))
}
jsonParser := json.NewDecoder(manifestFile)
if err = jsonParser.Decode(&req); err != nil {
failWithErr(fmt.Errorf("error decoding manifest file at \"%s\": %v", path, err))
}
return &req
}
func Generate(sqlcManifestPath string, outputDir string) {
wd, err := os.Getwd()
if err != nil {
failWithErr(fmt.Errorf("error getting working directory: %v", err))
}
outputDir = filepath.Join(wd, outputDir)
genPkg := getPackage(outputDir)
if genPkg == nil {
failWithErr(fmt.Errorf("unable to find package info for " + outputDir))
}
sqlcManifestPath = filepath.Join(wd, sqlcManifestPath)
sqlcManifest := parseManifest(sqlcManifestPath)
sqlcQueries := make(map[string]*plugin.Query)
sqlcEmbeds := make(map[string][]string)
for _, query := range sqlcManifest.GetQueries() {
name := query.GetName()
sqlcQueries[name] = query
columns := query.GetColumns()
// Look for columns that use sqlc.embed syntax
for _, c := range columns {
embedTable := c.GetEmbedTable()
if embedTable != nil {
sqlcEmbeds[name] = append(sqlcEmbeds[name], embedTable.GetName())
}
}
}
// Load the Queries type from the sqlc generated output directory
sourceType := filepath.Join(wd, sqlcManifest.GetSettings().GetCodegen().GetOut()+".Queries")
queriesType := loadQueriesType(sourceType)
// Get the package of the Queries type so we can compare it to other structs and see if they are
// also sqlc-generated types
dbTypesPkg := queriesType.Obj().Pkg()
dataloaderDefs := make([]dataloaderDefinition, 0)
// Loop through all methods on the Queries type
for i := 0; i < queriesType.NumMethods(); i++ {
method := queriesType.Method(i)
methodName := method.Name()
// Some methods on the Queries object (like WithTx) are not actually queries.
// We only want to examine queries, so skip any method that doesn't have a corresponding
// query in the sqlc manifest.
query, ok := sqlcQueries[methodName]
if !ok {
continue
}
config, err := parseDataloaderConfig(query.GetComments())
if err != nil {
failWithErr(fmt.Errorf("error parsing dataloader config: %s", err.Error()))
}
if config != nil && config.Skip {
continue
}
// Automatically generate dataloaders for all sqlc batch queries
sqlcCmd := query.GetCmd()
if strings.Contains(sqlcCmd, ":batch") {
var batchInputType types.Type
var batchOutputType types.Type
if strings.Contains(sqlcCmd, ":batchone") {
batchInputType, batchOutputType = getBatchOneTypes(method)
} else {
batchInputType, batchOutputType = getBatchManyTypes(method)
}
def := newDataloaderDefinition(methodName, batchInputType, batchOutputType, config.MaxBatchSize, config.BatchTimeout, config.PublishResults, genPkg.PkgPath)
if embeds, ok := sqlcEmbeds[methodName]; ok {
def.Embeds = getEmbedsForType(batchOutputType, embeds, dbTypesPkg)
}
// See whether this dataloader fits the "look up object by its DBID" pattern. If so, we can automatically
// cache results for it.
def.CanAutoCacheDBID = canAutoCacheDBID(batchInputType, batchOutputType, dbTypesPkg)
dataloaderDefs = append(dataloaderDefs, def)
continue
}
// We can also generate dataloaders for sqlc :many queries that include a batch_key_index column in their results
if strings.Contains(sqlcCmd, ":many") {
for _, col := range query.GetColumns() {
name := col.GetName()
if name == "batch_key_index" {
inputType, outputType, customBatchingDef := getCustomBatchTypes(method)
def := newDataloaderDefinition(methodName, inputType, outputType, config.MaxBatchSize, config.BatchTimeout, config.PublishResults, genPkg.PkgPath)
// If the return type consists only of batch_key_index and one other field, we hide the batch_key_index
// field from callers and just return the other field. When this happens, there are no sqlc.embed fields
// to publish, so we don't need to check this type for embeds.
if !customBatchingDef.UseOtherFieldName {
if embeds, ok := sqlcEmbeds[methodName]; ok {
def.Embeds = getEmbedsForType(outputType, embeds, dbTypesPkg)
}
}
def.IsCustomBatch = true
def.CustomBatching = customBatchingDef
dataloaderDefs = append(dataloaderDefs, def)
continue
}
}
}
}
err = generateFiles(dataloaderDefs, outputDir)
if err != nil {
failWithErr(fmt.Errorf("error generating dataloaders: %v", err))
}
}
type customBatchingDefinition struct {
BatchKeyIndexFieldName string
ResultFieldName string
QueryResultType *goType
LoaderResultType *goType
UseOtherFieldName bool
}
type embedDefinition struct {
FieldName string
FieldType *goType
}
// getEmbedsForType figures out which fields of the given type use sqlc.embed.
// It currently assumes that any subfield whose type is a sqlc generated struct
// (i.e. it's from the same package as the Queries type) is an embed.
func getEmbedsForType(t types.Type, columnNames []string, dbTypesPkg *types.Package) []embedDefinition {
embeds := make([]embedDefinition, 0)
// If t is a slice, get its underlying type
if sliceType, ok := t.(*types.Slice); ok {
t = sliceType.Elem()
}
// Check if it's a named type
namedType, ok := t.(*types.Named)
if !ok {
failWithErr(fmt.Errorf("expected named type, but got: %v", t))
}
// Check if the underlying type of the named type is a struct
structType, ok := namedType.Underlying().(*types.Struct)
if !ok {
failWithErr(fmt.Errorf("expected named type to be a struct, but got: %v", namedType))
}
// Loop over the struct's fields and see whether any of them belong to the dbTypesPkg package
for i := 0; i < structType.NumFields(); i++ {
field := structType.Field(i)
if namedFieldType, ok := field.Type().(*types.Named); ok {
if namedFieldType.Obj().Pkg().Path() == dbTypesPkg.Path() {
if gt, err := parseType(namedFieldType.String()); err == nil {
embeds = append(embeds, embedDefinition{
FieldName: field.Name(),
FieldType: gt,
})
} else {
failWithErr(fmt.Errorf("error parsing type: %v", err))
}
}
}
}
return embeds
}
// canAutoCacheDBID looks at a dataloader's input and output types to determine whether it fits the
// "look up object by its DBID" pattern. If so, we can automatically cache results for it. This function
// returns true if:
// - the inputType is a persist.DBID
// - the outputType contains a field named "ID" whose type is also a persist.DBID
func canAutoCacheDBID(inputType types.Type, outputType types.Type, dbTypesPkg *types.Package) bool {
// Check if it's a named type
keyType, ok := inputType.(*types.Named)
if !ok {
return false
}
// The key type must be a DBID
if keyType.Obj().Name() != "DBID" {
return false
}
resultType, ok := outputType.(*types.Named)
if !ok {
return false
}
// If the result isn't from the same package as the Queries type, we can't auto-cache it
if resultType.Obj().Pkg().Path() != dbTypesPkg.Path() {
return false
}
// Check if the underlying type of the named type is a struct
structResultType, ok := resultType.Underlying().(*types.Struct)
if !ok {
return false
}
// Loop over the struct's fields to find an "ID" field whose type is a persist.DBID
for i := 0; i < structResultType.NumFields(); i++ {
field := structResultType.Field(i)
if field.Name() != "ID" {
continue
}
if namedFieldType, ok := field.Type().(*types.Named); ok {
if namedFieldType.Obj().Name() == "DBID" {
return true
}
}
}
return false
}
func getCustomBatchTypes(batchFunc *types.Func) (types.Type, types.Type, *customBatchingDefinition) {
// A custom batched sqlc query should have a function with a signature like this:
// func (q *Queries) GetContractsByIDs(ctx context.Context, params []InputType) ([]OutputType, error)
signature := batchFunc.Type().(*types.Signature)
inputType := getBatchInputType(signature)
results := signature.Results()
if results.Len() != 2 {
failWithErr(fmt.Errorf("expected batch query to return 2 results like ([]TResult, error), but got: %v", results))
}
// The output type should be a slice
outputSlice, ok := results.At(0).Type().(*types.Slice)
if !ok {
failWithErr(fmt.Errorf("expected batch query to return a slice, but got: %v", results))
}
// Get the element type of the slice
elemType := outputSlice.Elem()
// The element type should be a named type
namedType, ok := elemType.(*types.Named)
if !ok {
failWithErr(fmt.Errorf("expected batch query to return a slice of a named type, but got: %v", elemType))
}
structType, ok := namedType.Underlying().(*types.Struct)
if !ok {
failWithErr(fmt.Errorf("expected batch query to return a slice of a struct type, but got: %v", elemType))
}
// If there are only two fields in the struct, we can hide the batch_key_index field from callers and
// just return the other field.
numFields := structType.NumFields()
batchKeyIndexFieldName := ""
var otherField *types.Var
for i := 0; i < numFields; i++ {
field := structType.Field(i)
// Use struct tags to match columns from the sqlc manifest to fields in the generated structs
tag := reflect.StructTag(structType.Tag(i))
if tag.Get("db") == "batch_key_index" {
batchKeyIndexFieldName = field.Name()
} else if numFields == 2 {
otherField = field
}
}
queryResultType, err := typeToGoType(namedType)
if err != nil {
failWithErr(fmt.Errorf("error parsing type: %v", err))
}
useOtherField := otherField != nil
var loaderResultType *goType
if useOtherField {
loaderResultType, err = typeToGoType(otherField.Type())
if err != nil {
failWithErr(fmt.Errorf("error parsing type: %v", err))
}
} else {
loaderResultType = queryResultType
}
resultFieldName := ""
if useOtherField {
resultFieldName = "." + otherField.Name()
}
var outputType types.Type
if useOtherField {
outputType = otherField.Type()
} else {
outputType = namedType
}
return inputType, outputType, &customBatchingDefinition{
BatchKeyIndexFieldName: batchKeyIndexFieldName,
ResultFieldName: resultFieldName,
QueryResultType: queryResultType,
LoaderResultType: loaderResultType,
UseOtherFieldName: useOtherField,
}
}
func getBatchInputType(signature *types.Signature) types.Type {
// A sqlc batch query generates a function with a signature like this:
// func (q *Queries) GetThing(ctx context.Context, arg []InputType) *GetThingResults
var input types.Type
found := false
// Iterate over input parameters and find the first one that's not context.Context
params := signature.Params()
for i := 0; i < params.Len(); i++ {
v := params.At(i)
if v.Type().String() != "context.Context" {
input = v.Type()
found = true
break
}
}
if !found {
failWithErr(fmt.Errorf("couldn't find input arg for batch signature: %v", signature))
}
// The input type should be a slice of something
sliceType, ok := input.(*types.Slice)
if !ok {
failWithErr(fmt.Errorf("expected batch input arg to be a slice. got: %v", input))
}
// Return the underlying type of the slice
return sliceType.Elem()
}
func getBatchOneTypes(batchFunc *types.Func) (input types.Type, output types.Type) {
// A :batchone query generates a function with a signature like this:
// - func (q *Queries) GetThing(ctx context.Context, arg []InputType) *GetThingResults
signature := batchFunc.Type().(*types.Signature)
return getBatchInputType(signature), getBatchOneOutputType(signature)
}
func getBatchManyTypes(batchFunc *types.Func) (input types.Type, output types.Type) {
// A :batchone query generates a function with a signature like this:
// - func (q *Queries) GetThing(ctx context.Context, arg []InputType) *GetThingResults
signature := batchFunc.Type().(*types.Signature)
return getBatchInputType(signature), getBatchManyOutputType(signature)
}
func getBatchOneOutputType(signature *types.Signature) types.Type {
// A :batchone query generates a BatchResults type that has a function
// called QueryRow with a signature like this:
// - func (b *BatchResults) QueryRow(f func(int, OutputType, error))
// This will return a non-slice type
return getBatchOutputType(signature, "QueryRow")
}
func getBatchManyOutputType(signature *types.Signature) types.Type {
// A :batchmany query generates a BatchResults type that has a function
// called QueryRow with a signature like this:
// - func (b *BatchResults) Query(f func(int, []OutputType, error))
// This will return a slice type
return getBatchOutputType(signature, "Query")
}
func getBatchOutputType(signature *types.Signature, queryFuncName string) types.Type {
results := signature.Results()
if results.Len() != 1 {
failWithErr(fmt.Errorf("expected batch query to return a single result type, but got: %v", results))
}
// The output type should be a pointer
outputPtr, ok := results.At(0).Type().(*types.Pointer)
if !ok {
failWithErr(fmt.Errorf("expected batch query to return a pointer, but got: %v", results))
}
// Get the element type of the pointer
elemType := outputPtr.Elem()
// The element type should be a named type
namedType, ok := elemType.(*types.Named)
if !ok {
failWithErr(fmt.Errorf("expected batch query to return a pointer to a named type, but got: %v", elemType))
}
// Get the queryFunc method on the named type
for i := 0; i < namedType.NumMethods(); i++ {
if namedType.Method(i).Name() != queryFuncName {
continue
}
querySig, ok := namedType.Method(i).Type().(*types.Signature)
if !ok {
failWithErr(fmt.Errorf("query method %s on %v has no signature", queryFuncName, namedType))
}
// The method should have one parameter: the function f
if querySig.Params().Len() != 1 {
failWithErr(fmt.Errorf("query method %s on %v has %d parameters (expected 1)", queryFuncName, namedType, querySig.Params().Len()))
}
// Get the type of the function f
funcType, ok := querySig.Params().At(0).Type().(*types.Signature)
if !ok {
failWithErr(fmt.Errorf("function parameter f of signature type %v has no signature", querySig))
}
// The function f should have three parameters (int, ?, error)
if funcType.Params().Len() != 3 {
failWithErr(fmt.Errorf("function parameter f of signature type %v has %d parameters (expected 3)", querySig, funcType.Params().Len()))
}
// Return the type of the second parameter of the function f
return funcType.Params().At(1).Type()
}
failWithErr(fmt.Errorf("couldn't find query method %s on %v", queryFuncName, namedType))
return nil
}
func loadQueriesType(sourceType string) *types.Named {
sourceTypePackage, sourceTypeName := splitSourceType(sourceType)
// Load the package so we can get type info
pkg := loadPackage(sourceTypePackage)
// Lookup the given source type name in the package declarations
obj := pkg.Types.Scope().Lookup(sourceTypeName)
if obj == nil {
failWithErr(fmt.Errorf("%s not found in declared types of %s", sourceTypeName, pkg))
}
objType := obj.Type()
if objType == nil {
failWithErr(fmt.Errorf("%s has no type", obj))
}
queriesType, ok := objType.(*types.Named)
if !ok {
failWithErr(fmt.Errorf("%s is not a named type (expected: types.Named)", obj))
}
return queriesType
}
func getAutoCacheEntries(generatedPkg *packages.Package, defs []dataloaderDefinition) []autoCacheEntry {
entries := make([]autoCacheEntry, 0)
for _, def := range defs {
// Lookup the given source type name in the package declarations
obj := generatedPkg.Types.Scope().Lookup(def.Name)
if obj == nil {
failWithErr(fmt.Errorf("%s not found in declared types of %s", def.Name, generatedPkg))
}
objType := obj.Type()
if objType == nil {
failWithErr(fmt.Errorf("%s has no type", obj))
}
loader, ok := objType.(*types.Named)
if !ok {
failWithErr(fmt.Errorf("%s is not a named type (expected: types.Named)", obj))
}
cacheOne := false
if resultType, keyType, ok := getAutoCacheTypes(loader, "getKeyForResult"); ok {
if !resultType.HasSameBaseType(def.ResultType) {
failWithErr(fmt.Errorf("resultType %s does not match expected: %s", resultType, def.ResultType))
}
if !keyType.HasSameBaseType(def.KeyType) {
failWithErr(fmt.Errorf("keyType %s does not match expected: %s", keyType, def.KeyType))
}
cacheOne = true
}
cacheMany := false
if resultType, keyType, ok := getAutoCacheTypes(loader, "getKeysForResult"); ok {
if !resultType.HasSameBaseType(def.ResultType) {
failWithErr(fmt.Errorf("resultType %s does not match expected: %s", resultType, def.ResultType))
}
if !keyType.HasSameBaseType(def.KeyType) {
failWithErr(fmt.Errorf("keyType %s does not match expected: %s", keyType, def.KeyType))
}
cacheMany = true
}
if cacheOne || cacheMany {
entries = append(entries, autoCacheEntry{
LoaderName: def.Name,
ResultType: def.ResultType,
SingleKey: cacheOne,
ManyKeys: cacheMany,
})
}
}
return entries
}
func getAutoCacheTypes(namedType *types.Named, funcName string) (*goType, *goType, bool) {
for i := 0; i < namedType.NumMethods(); i++ {
if namedType.Method(i).Name() != funcName {
continue
}
autoCacheSig, ok := namedType.Method(i).Type().(*types.Signature)
if !ok {
failWithErr(fmt.Errorf("method %s on %v has no signature", funcName, namedType))
}
// The method should have one parameter: the value type for the dataloader
if autoCacheSig.Params().Len() != 1 {
failWithErr(fmt.Errorf("method %s on %v has %d parameters (expected 1)", funcName, namedType, autoCacheSig.Params().Len()))
}
// Get the type of the parameter
paramType, ok := autoCacheSig.Params().At(0).Type().(*types.Named)
if !ok {
failWithErr(fmt.Errorf("input parameter of signature type %v must be a named type", autoCacheSig))
}
// The method should have one result: the key type for the dataloader (or a slice of the key type)
if autoCacheSig.Results().Len() != 1 {
failWithErr(fmt.Errorf("method %s on %v has %d results (expected 1)", funcName, namedType, autoCacheSig.Results().Len()))
}
resultType := autoCacheSig.Results().At(0).Type()
paramGoType, err := typeToGoType(paramType)
if err != nil {
failWithErr(fmt.Errorf("error converting type to go type: %v", err))
}
resultGoType, err := typeToGoType(resultType)
if err != nil {
failWithErr(fmt.Errorf("error converting type to go type: %v", err))
}
return paramGoType, resultGoType, true
}
return nil, nil, false
}
var errorType = types.Universe.Lookup("error").Type()
func implementsGetNotFoundError(namedType *types.Named, expectedParamType *goType) bool {
const funcName = "getNotFoundError"
for i := 0; i < namedType.NumMethods(); i++ {
if namedType.Method(i).Name() != funcName {
continue
}
sig, ok := namedType.Method(i).Type().(*types.Signature)
if !ok {
failWithErr(fmt.Errorf("method %s on %v has no signature", funcName, namedType))
}
// The method should have one parameter: the key type for the dataloader
if sig.Params().Len() != 1 {