Repository navigation
Expand file tree
/
Copy pathsqlc.go
More file actions
569 lines (477 loc) · 15.8 KB
/
Copy pathsqlc.go
File metadata and controls
569 lines (477 loc) · 15.8 KB
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 gogenfilter
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/go-faster/yaml"
)
const (
sqlcConfigFile = "sqlc.yaml"
sqlcConfigFileAlt = "sqlc.yml"
)
// sqlcV1Config represents a sqlc.yaml v1 configuration file structure.
type sqlcV1Config struct {
Version string `yaml:"version"`
Packages []sqlcV1Package `yaml:"packages"`
}
// sqlcV1Package represents a single package in a v1 sqlc.yaml config.
type sqlcV1Package struct {
Path string `yaml:"path"` // output directory in v1 format
OutputBatchFileName string `yaml:"output_batch_file_name"`
OutputDBFileName string `yaml:"output_db_file_name"`
OutputModelsFileName string `yaml:"output_models_file_name"`
OutputQuerierFileName string `yaml:"output_querier_file_name"`
OutputCopyfromFileName string `yaml:"output_copyfrom_file_name"`
OutputFilesSuffix string `yaml:"output_files_suffix"`
}
// sqlcConfig represents a sqlc.yaml v2 configuration file structure.
type sqlcConfig struct {
Version string `yaml:"version"`
SQL []sqlcEngine `yaml:"sql"`
}
// sqlcEngine represents a single SQL engine configuration in sqlc.yaml.
type sqlcEngine struct {
Schema string `yaml:"schema"`
Engine string `yaml:"engine"`
Gen sqlcGenConfig `yaml:"gen"`
Codegen []sqlcCodegen `yaml:"codegen"`
}
// sqlcGenConfig represents the generation configuration in sqlc.yaml.
type sqlcGenConfig struct {
Go *sqlcGoConfig `yaml:"go"`
JSON *sqlcJSONConfig `yaml:"json"`
}
// sqlcGoConfig represents the Go-specific generation configuration.
// The output_*_file_name fields customize the fixed output filenames that sqlc
// writes into the configured output directory (see sqlc gen.go: db.go, models.go,
// querier.go, batch.go, copyfrom.go). Empty means sqlc's default name.
// output_files_suffix is appended to query file names only (e.g. query.sql_gen.go).
type sqlcGoConfig struct {
Package string `yaml:"package"`
Out string `yaml:"out"`
OutputBatchFileName string `yaml:"output_batch_file_name"`
OutputDBFileName string `yaml:"output_db_file_name"`
OutputModelsFileName string `yaml:"output_models_file_name"`
OutputQuerierFileName string `yaml:"output_querier_file_name"`
OutputCopyfromFileName string `yaml:"output_copyfrom_file_name"`
OutputFilesSuffix string `yaml:"output_files_suffix"`
}
// sqlcJSONConfig represents the JSON generation configuration.
type sqlcJSONConfig struct {
Out string `yaml:"out"`
}
// sqlcCodegen represents a plugin-based codegen entry in sqlc.yaml.
type sqlcCodegen struct {
Out string `yaml:"out"`
Plugin string `yaml:"plugin"`
}
// newSQLCConfigError creates a new SQLCConfigError with consistent formatting.
func newSQLCConfigError(
code ErrorCode,
configPath string,
operation SQLCOperation,
message string,
err error,
) *SQLCConfigError {
return &SQLCConfigError{
Code: code,
ConfigPath: configPath,
Operation: operation,
Message: message,
Err: err,
}
}
func sqlcFindError(path string, err error) *SQLCConfigError {
return newSQLCConfigError(
CodeSQLCConfigFind,
path,
OpSQLCFind,
fmt.Sprintf("finding sqlc configs in %q", path),
err,
)
}
func sqlcWalkError(path string, err error) *SQLCConfigError {
return newSQLCConfigError(
CodeSQLCConfigWalk,
path,
OpSQLCWalk,
fmt.Sprintf("walking %q for sqlc configs", path),
err,
)
}
func sqlcReadError(configPath string, err error) *SQLCConfigError {
return newSQLCConfigError(
CodeSQLCConfigRead,
configPath,
OpSQLCRead,
"reading sqlc config",
err,
)
}
func sqlcCollectError(configPath string, err error) *SQLCConfigError {
return newSQLCConfigError(
CodeSQLCConfigCollect,
configPath,
OpSQLCCollect,
"processing sqlc config",
err,
)
}
// FindSQLCConfigs searches for sqlc.yaml or sqlc.yml files in the given paths.
// Searches both the provided paths and their parent directories (up to 10 levels up).
// Returns a map of config file path to project root directory.
func FindSQLCConfigs(paths []string) (map[string]string, *SQLCConfigError) {
configs := make(map[string]string)
for _, path := range paths {
err := findSQLCConfigsInPath(path, configs)
if err != nil {
return nil, err
}
}
return configs, nil
}
// findSQLCConfigsInPath searches for sqlc configs in a single path.
func findSQLCConfigsInPath(path string, configs map[string]string) *SQLCConfigError {
err := walkPathForSQLCConfigs(path, configs)
if err != nil {
return sqlcFindError(path, err)
}
findSQLCConfigsInParent(path, configs)
return nil
}
// walkPathForSQLCConfigs walks a path to find sqlc config files.
func walkPathForSQLCConfigs(path string, configs map[string]string) *SQLCConfigError {
err := filepath.WalkDir(path, func(filePath string, d os.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("accessing %q: %w", filePath, err)
}
if walkDirForSQLCConfigs(filePath, d.Name(), configs) {
return filepath.SkipDir
}
return nil
})
if err != nil {
return sqlcWalkError(path, err)
}
return nil
}
// walkDirForSQLCConfigs records sqlc config files during directory walking.
// Returns true if the directory should be skipped.
func walkDirForSQLCConfigs(filePath, dirName string, configs map[string]string) bool {
if shouldSkipDirectory(dirName) {
return true
}
recordSQLCConfig(filePath, configs)
return false
}
// shouldSkipDirectory returns true if a directory should be skipped during walk.
func shouldSkipDirectory(name string) bool {
if name == "." {
return false
}
return strings.HasPrefix(name, ".") || name == nodeModulesDir || name == vendorDir
}
// recordSQLCConfig records a sqlc config file if it matches.
func recordSQLCConfig(filePath string, configs map[string]string) {
filename := filepath.Base(filePath)
if filename == sqlcConfigFile || filename == sqlcConfigFileAlt {
configs[filePath] = filepath.Dir(filePath)
}
}
// findSQLCConfigsInParent searches parent directories for sqlc config.
func findSQLCConfigsInParent(path string, configs map[string]string) {
parentPath, err := FindProjectRoot(path, []string{sqlcConfigFile, sqlcConfigFileAlt})
if err != nil || parentPath == "" {
return
}
tryAddSQLCConfig(parentPath, sqlcConfigFile, configs)
tryAddSQLCConfig(parentPath, sqlcConfigFileAlt, configs)
}
// tryAddSQLCConfig adds a config to the map if the file exists.
func tryAddSQLCConfig(parentPath, filename string, configs map[string]string) {
configPath := filepath.Join(parentPath, filename)
if fileExists(configPath) {
configs[configPath] = parentPath
}
}
// parseSQLCConfig reads and parses a sqlc.yaml file.
func parseSQLCConfig(configPath string) (*sqlcConfig, *SQLCConfigError) {
data, err := os.ReadFile(configPath)
if err != nil {
return nil, sqlcReadError(configPath, err)
}
return unmarshalSQLCConfig(data, configPath)
}
func unmarshalSQLCYAML(data []byte, target any, configPath, errMsg string) *SQLCConfigError {
err := yaml.Unmarshal(data, target)
if err != nil {
return newSQLCConfigError(
CodeSQLCConfigParse,
configPath,
OpSQLCParse,
errMsg,
err,
)
}
return nil
}
func unmarshalSQLCConfig(data []byte, configPath string) (*sqlcConfig, *SQLCConfigError) {
var version struct {
Version string `yaml:"version"`
}
err := unmarshalSQLCYAML(data, &version, configPath, "detecting sqlc config version")
if err != nil {
return nil, err
}
switch version.Version {
case "1":
return parseV1AsV2(data, configPath)
case "2", "":
var config sqlcConfig
err := unmarshalSQLCYAML(data, &config, configPath, "parsing sqlc config")
if err != nil {
return nil, err
}
return &config, nil
default:
return nil, newSQLCConfigError(
CodeSQLCConfigParse,
configPath,
OpSQLCParse,
fmt.Sprintf("unsupported sqlc config version %q", version.Version),
nil,
)
}
}
// parseV1AsV2 parses a v1 config and converts it to v2 format.
// v1 uses packages[].path as output dirs; v2 uses sql[].gen.go.out.
func parseV1AsV2(data []byte, configPath string) (*sqlcConfig, *SQLCConfigError) {
var v1Config sqlcV1Config
err := unmarshalSQLCYAML(data, &v1Config, configPath, "parsing sqlc v1 config")
if err != nil {
return nil, err
}
config := &sqlcConfig{Version: v1Config.Version, SQL: nil}
for _, pkg := range v1Config.Packages {
if pkg.Path != "" {
config.SQL = append(config.SQL, sqlcEngine{
Schema: "",
Engine: "",
Gen: sqlcGenConfig{Go: &sqlcGoConfig{
Package: "",
Out: pkg.Path,
OutputBatchFileName: pkg.OutputBatchFileName,
OutputDBFileName: pkg.OutputDBFileName,
OutputModelsFileName: pkg.OutputModelsFileName,
OutputQuerierFileName: pkg.OutputQuerierFileName,
OutputCopyfromFileName: pkg.OutputCopyfromFileName,
OutputFilesSuffix: pkg.OutputFilesSuffix,
}, JSON: nil},
Codegen: nil,
})
}
}
return config, nil
}
// Fixed output filenames sqlc writes into each configured output directory
// (see sqlc gen.go). They are configurable via the corresponding
// output_*_file_name config keys; the defaults listed here apply when the key
// is absent.
const (
sqlcFileDB = "db.go"
sqlcFileModels = "models.go"
sqlcFileQuerier = "querier.go"
sqlcFileBatch = "batch.go"
sqlcFileCopyfrom = "copyfrom.go"
)
// sqlcDefaultFileNames is the ordered list of fixed output filenames. Order
// matters for test assertions.
//
//nolint:gochecknoglobals // immutable lookup table, not configurable state
var sqlcDefaultFileNames = []string{
sqlcFileDB,
sqlcFileModels,
sqlcFileQuerier,
sqlcFileBatch,
sqlcFileCopyfrom,
}
// sqlcCustomFileName returns the config-customized filename for the given
// default name, or empty if not customized.
func sqlcCustomFileName(defaultName string, goCfg *sqlcGoConfig) string {
switch defaultName {
case sqlcFileDB:
return goCfg.OutputDBFileName
case sqlcFileModels:
return goCfg.OutputModelsFileName
case sqlcFileQuerier:
return goCfg.OutputQuerierFileName
case sqlcFileBatch:
return goCfg.OutputBatchFileName
case sqlcFileCopyfrom:
return goCfg.OutputCopyfromFileName
default:
return ""
}
}
// configuredSQLCFileNames returns the Go output filenames sqlc writes into a
// configured output dir, resolved from the config (empty → default). Per-query
// <source>.sql.go files are intentionally excluded — they are detected by the
// generic filename pattern, not by config membership.
func configuredSQLCFileNames(goCfg *sqlcGoConfig) []string {
if goCfg == nil {
return nil
}
names := make([]string, 0, len(sqlcDefaultFileNames))
for _, def := range sqlcDefaultFileNames {
if custom := sqlcCustomFileName(def, goCfg); custom != "" {
names = append(names, custom)
} else {
names = append(names, filepath.Base(def))
}
}
return names
}
// extractOutputDirs extracts output directories from a sqlc config's SQL engines.
func extractOutputDirs(config *sqlcConfig, projectRoot string) []string {
var outputDirs []string
for _, sqlEngine := range config.SQL {
// Go output: sql[].gen.go.out
if sqlEngine.Gen.Go != nil && sqlEngine.Gen.Go.Out != "" {
outDir := filepath.Join(projectRoot, sqlEngine.Gen.Go.Out)
outDir = filepath.Clean(outDir)
outputDirs = append(outputDirs, outDir)
}
// JSON output: sql[].gen.json.out
if sqlEngine.Gen.JSON != nil && sqlEngine.Gen.JSON.Out != "" {
outDir := filepath.Join(projectRoot, sqlEngine.Gen.JSON.Out)
outDir = filepath.Clean(outDir)
outputDirs = append(outputDirs, outDir)
}
// Plugin codegen: sql[].codegen[].out
for _, cg := range sqlEngine.Codegen {
if cg.Out != "" {
outDir := filepath.Join(projectRoot, cg.Out)
outDir = filepath.Clean(outDir)
outputDirs = append(outputDirs, outDir)
}
}
}
return outputDirs
}
// GetSQLOutputDirs returns a list of output directories from sqlc configuration files.
func GetSQLOutputDirs(paths []string) ([]string, *SQLCConfigError) {
configPaths, err := FindSQLCConfigs(paths)
if err != nil {
return nil, err
}
var outputDirs []string
for configPath, projectRoot := range configPaths {
config, err := parseSQLCConfig(configPath)
if err != nil {
return nil, sqlcCollectError(configPath, err)
}
outputDirs = append(outputDirs, extractOutputDirs(config, projectRoot)...)
}
return outputDirs, nil
}
// sqlcDerivedConfig is the distilled, detection-relevant view of all sqlc
// config(s) in a project. It maps each declared output directory (cleaned,
// slash-separated, relative to the fs root) to the set of filenames sqlc writes
// there — the fixed defaults plus any output_*_file_name customizations.
// It is built once per Filter and cached.
type sqlcDerivedConfig struct {
// dirFiles: output dir → accepted base filenames (including `.sql.go`
// per-query names when the dir is a sqlc output dir).
dirFiles map[string]map[string]bool
}
// sqlcDerivedConfigForFS builds the derived config from all sqlc config files
// reachable in fsys. Paths that don't exist or can't be parsed are skipped;
// a nil (not empty) error means no usable sqlc config was found. Returns the
// derived config and nil even when no configs exist (empty map).
func sqlcDerivedConfigForFS(fsys fs.FS, paths []string) (*sqlcDerivedConfig, *SQLCConfigError) {
configPaths, err := FindSQLCConfigsFS(fsys, paths)
if err != nil {
return nil, err
}
derived := &sqlcDerivedConfig{dirFiles: make(map[string]map[string]bool)}
for configPath := range configPaths {
config, cfgErr := parseSQLCConfigFS(fsys, configPath)
if cfgErr != nil {
return nil, sqlcCollectError(configPath, cfgErr)
}
mergeSQLCConfig(derived, config)
}
return derived, nil
}
// mergeSQLCConfig folds one sqlc config into the derived config. For each
// Go output dir it records the fixed filenames (defaults + custom names) and
// the per-query ".sql.go" name.
func mergeSQLCConfig(derived *sqlcDerivedConfig, config *sqlcConfig) {
if config == nil {
return
}
for i := range config.SQL {
engine := &config.SQL[i]
goCfg := engine.Gen.Go
if goCfg == nil || goCfg.Out == "" {
continue
}
outDir := filepath.ToSlash(filepath.Clean(goCfg.Out))
names := derived.dirFiles[outDir]
if names == nil {
names = make(map[string]bool)
derived.dirFiles[outDir] = names
}
for _, name := range configuredSQLCFileNames(goCfg) {
names[filepath.Base(name)] = true
}
}
}
// FindSQLCConfigsFS searches for sqlc.yaml or sqlc.yml files using the provided filesystem.
// Paths must be valid within fsys (relative to the FS root).
// Unlike FindSQLCConfigs, this does not search parent directories.
func FindSQLCConfigsFS(fsys fs.FS, paths []string) (map[string]string, *SQLCConfigError) {
configs := make(map[string]string)
for _, path := range paths {
err := fs.WalkDir(fsys, path, func(filePath string, d fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("accessing %q: %w", filePath, err)
}
if walkDirForSQLCConfigs(filePath, d.Name(), configs) {
return fs.SkipDir
}
return nil
})
if err != nil {
return nil, sqlcWalkError(path, err)
}
}
return configs, nil
}
// parseSQLCConfigFS reads and parses a sqlc.yaml file from the given filesystem.
func parseSQLCConfigFS(fsys fs.FS, configPath string) (*sqlcConfig, *SQLCConfigError) {
data, err := fs.ReadFile(fsys, configPath)
if err != nil {
return nil, sqlcReadError(configPath, err)
}
return unmarshalSQLCConfig(data, configPath)
}
// GetSQLOutputDirsFS returns output directories from sqlc configs using the provided filesystem.
// Paths must be valid within fsys (relative to the FS root).
func GetSQLOutputDirsFS(fsys fs.FS, paths []string) ([]string, *SQLCConfigError) {
configPaths, err := FindSQLCConfigsFS(fsys, paths)
if err != nil {
return nil, err
}
var outputDirs []string
for configPath, projectRoot := range configPaths {
config, cfgErr := parseSQLCConfigFS(fsys, configPath)
if cfgErr != nil {
return nil, sqlcCollectError(configPath, cfgErr)
}
outputDirs = append(outputDirs, extractOutputDirs(config, projectRoot)...)
}
return outputDirs, nil
}