Skip to content

Commit d9e233e

Browse files
feat(codegen): Remove Go specific overrides from codegen proto (#2929)
Remove overrides and rename from the codegen.proto definition. Replace them with plugin-specifc global options defined in the `options` block. Defer validation of this configuration to the plugin itself. --------- Co-authored-by: Andrew Benton <andrew@sqlc.dev>
1 parent 7461c2d commit d9e233e

File tree

37 files changed

+1239
-2673
lines changed

37 files changed

+1239
-2673
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ jobs:
5454
- name: install ./...
5555
run: go install ./...
5656

57+
- name: build internal/endtoend
58+
run: go build ./...
59+
working-directory: internal/endtoend/testdata
60+
5761
- name: test ./...
5862
run: gotestsum --junitfile junit.xml -- --tags=examples -timeout 20m ./...
5963
env:
@@ -65,16 +69,6 @@ jobs:
6569
CI_SQLC_AUTH_TOKEN: ${{ secrets.CI_SQLC_AUTH_TOKEN }}
6670
SQLC_AUTH_TOKEN: ${{ secrets.CI_SQLC_AUTH_TOKEN }}
6771

68-
- name: build internal/endtoend
69-
run: go build ./...
70-
working-directory: internal/endtoend/testdata
71-
72-
- name: report
73-
if: false
74-
run: ./scripts/report.sh
75-
env:
76-
BUILDKITE_ANALYTICS_TOKEN: ${{ secrets.BUILDKITE_ANALYTICS_TOKEN }}
77-
7872
vuln_check:
7973
runs-on: ubuntu-latest
8074
timeout-minutes: 5

internal/cmd/generate.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,19 @@ func codegen(ctx context.Context, combo config.CombinedSettings, sql outPair, re
411411

412412
opts, err := convert.YAMLtoJSON(sql.Plugin.Options)
413413
if err != nil {
414-
return "", nil, fmt.Errorf("invalid plugin options")
414+
return "", nil, fmt.Errorf("invalid plugin options: %w", err)
415415
}
416416
req.PluginOptions = opts
417417

418+
global, found := combo.Global.Options[plug.Name]
419+
if found {
420+
opts, err := convert.YAMLtoJSON(global)
421+
if err != nil {
422+
return "", nil, fmt.Errorf("invalid global options: %w", err)
423+
}
424+
req.GlobalOptions = opts
425+
}
426+
418427
case sql.Gen.Go != nil:
419428
out = combo.Go.Out
420429
handler = ext.HandleFunc(golang.Generate)
@@ -424,6 +433,14 @@ func codegen(ctx context.Context, combo config.CombinedSettings, sql outPair, re
424433
}
425434
req.PluginOptions = opts
426435

436+
if combo.Global.Overrides.Go != nil {
437+
opts, err := json.Marshal(combo.Global.Overrides.Go)
438+
if err != nil {
439+
return "", nil, fmt.Errorf("opts marshal failed: %w", err)
440+
}
441+
req.GlobalOptions = opts
442+
}
443+
427444
case sql.Gen.JSON != nil:
428445
out = combo.JSON.Out
429446
handler = ext.HandleFunc(genjson.Generate)

internal/cmd/shim.go

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package cmd
22

33
import (
4-
"strings"
5-
64
"github.com/sqlc-dev/sqlc/internal/compiler"
75
"github.com/sqlc-dev/sqlc/internal/config"
86
"github.com/sqlc-dev/sqlc/internal/config/convert"
@@ -11,53 +9,13 @@ import (
119
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
1210
)
1311

14-
func pluginOverride(r *compiler.Result, o config.Override) *plugin.Override {
15-
var column string
16-
var table plugin.Identifier
17-
18-
if o.Column != "" {
19-
colParts := strings.Split(o.Column, ".")
20-
switch len(colParts) {
21-
case 2:
22-
table.Schema = r.Catalog.DefaultSchema
23-
table.Name = colParts[0]
24-
column = colParts[1]
25-
case 3:
26-
table.Schema = colParts[0]
27-
table.Name = colParts[1]
28-
column = colParts[2]
29-
case 4:
30-
table.Catalog = colParts[0]
31-
table.Schema = colParts[1]
32-
table.Name = colParts[2]
33-
column = colParts[3]
34-
}
35-
}
36-
return &plugin.Override{
37-
CodeType: "", // FIXME
38-
DbType: o.DBType,
39-
Nullable: o.Nullable,
40-
Unsigned: o.Unsigned,
41-
Column: o.Column,
42-
ColumnName: column,
43-
Table: &table,
44-
GoType: pluginGoType(o),
45-
}
46-
}
47-
4812
func pluginSettings(r *compiler.Result, cs config.CombinedSettings) *plugin.Settings {
49-
var over []*plugin.Override
50-
for _, o := range cs.Overrides {
51-
over = append(over, pluginOverride(r, o))
52-
}
5313
return &plugin.Settings{
54-
Version: cs.Global.Version,
55-
Engine: string(cs.Package.Engine),
56-
Schema: []string(cs.Package.Schema),
57-
Queries: []string(cs.Package.Queries),
58-
Overrides: over,
59-
Rename: cs.Rename,
60-
Codegen: pluginCodegen(cs, cs.Codegen),
14+
Version: cs.Global.Version,
15+
Engine: string(cs.Package.Engine),
16+
Schema: []string(cs.Package.Schema),
17+
Queries: []string(cs.Package.Queries),
18+
Codegen: pluginCodegen(cs, cs.Codegen),
6119
}
6220
}
6321

@@ -101,20 +59,6 @@ func pluginWASM(p config.Plugin) *plugin.Codegen_WASM {
10159
return nil
10260
}
10361

104-
func pluginGoType(o config.Override) *plugin.ParsedGoType {
105-
// Note that there is a slight mismatch between this and the
106-
// proto api. The GoType on the override is the unparsed type,
107-
// which could be a qualified path or an object, as per
108-
// https://docs.sqlc.dev/en/v1.18.0/reference/config.html#type-overriding
109-
return &plugin.ParsedGoType{
110-
ImportPath: o.GoImportPath,
111-
Package: o.GoPackage,
112-
TypeName: o.GoTypeName,
113-
BasicType: o.GoBasicType,
114-
StructTags: o.GoStructTags,
115-
}
116-
}
117-
11862
func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
11963
var schemas []*plugin.Schema
12064
for _, s := range c.Schemas {

internal/codegen/golang/gen.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (t *tmplCtx) codegenQueryRetval(q Query) (string, error) {
104104
}
105105

106106
func Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {
107-
options, err := opts.ParseOpts(req)
107+
options, err := opts.Parse(req)
108108
if err != nil {
109109
return nil, err
110110
}
@@ -129,11 +129,10 @@ func Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenR
129129

130130
func generate(req *plugin.CodeGenRequest, options *opts.Options, enums []Enum, structs []Struct, queries []Query) (*plugin.CodeGenResponse, error) {
131131
i := &importer{
132-
Settings: req.Settings,
133-
Options: options,
134-
Queries: queries,
135-
Enums: enums,
136-
Structs: structs,
132+
Options: options,
133+
Queries: queries,
134+
Enums: enums,
135+
Structs: structs,
137136
}
138137

139138
tctx := tmplCtx{

internal/codegen/golang/go_type.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import (
88
"github.com/sqlc-dev/sqlc/internal/plugin"
99
)
1010

11-
func addExtraGoStructTags(tags map[string]string, req *plugin.CodeGenRequest, col *plugin.Column) {
12-
for _, oride := range req.Settings.Overrides {
11+
func addExtraGoStructTags(tags map[string]string, req *plugin.CodeGenRequest, options *opts.Options, col *plugin.Column) {
12+
for _, override := range options.Overrides {
13+
oride := override.ShimOverride
1314
if oride.GoType.StructTags == nil {
1415
continue
1516
}
16-
if !sdk.Matches(oride, col.Table, req.Catalog.DefaultSchema) {
17+
if !override.Matches(col.Table, req.Catalog.DefaultSchema) {
1718
// Different table.
1819
continue
1920
}
@@ -34,15 +35,17 @@ func addExtraGoStructTags(tags map[string]string, req *plugin.CodeGenRequest, co
3435

3536
func goType(req *plugin.CodeGenRequest, options *opts.Options, col *plugin.Column) string {
3637
// Check if the column's type has been overridden
37-
for _, oride := range req.Settings.Overrides {
38+
for _, override := range options.Overrides {
39+
oride := override.ShimOverride
40+
3841
if oride.GoType.TypeName == "" {
3942
continue
4043
}
4144
cname := col.Name
4245
if col.OriginalName != "" {
4346
cname = col.OriginalName
4447
}
45-
sameTable := sdk.Matches(oride, col.Table, req.Catalog.DefaultSchema)
48+
sameTable := override.Matches(col.Table, req.Catalog.DefaultSchema)
4649
if oride.Column != "" && sdk.MatchString(oride.ColumnName, cname) && sameTable {
4750
if col.IsSqlcSlice {
4851
return "[]" + oride.GoType.TypeName
@@ -65,7 +68,8 @@ func goInnerType(req *plugin.CodeGenRequest, options *opts.Options, col *plugin.
6568
notNull := col.NotNull || col.IsArray
6669

6770
// package overrides have a higher precedence
68-
for _, oride := range req.Settings.Overrides {
71+
for _, override := range options.Overrides {
72+
oride := override.ShimOverride
6973
if oride.GoType.TypeName == "" {
7074
continue
7175
}
@@ -77,7 +81,7 @@ func goInnerType(req *plugin.CodeGenRequest, options *opts.Options, col *plugin.
7781
// TODO: Extend the engine interface to handle types
7882
switch req.Settings.Engine {
7983
case "mysql":
80-
return mysqlType(req, col)
84+
return mysqlType(req, options, col)
8185
case "postgresql":
8286
return postgresType(req, options, col)
8387
case "sqlite":

internal/codegen/golang/imports.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
99
"github.com/sqlc-dev/sqlc/internal/metadata"
10-
"github.com/sqlc-dev/sqlc/internal/plugin"
1110
)
1211

1312
type fileImports struct {
@@ -59,11 +58,10 @@ func mergeImports(imps ...fileImports) [][]ImportSpec {
5958
}
6059

6160
type importer struct {
62-
Settings *plugin.Settings
63-
Options *opts.Options
64-
Queries []Query
65-
Enums []Enum
66-
Structs []Struct
61+
Options *opts.Options
62+
Queries []Query
63+
Enums []Enum
64+
Structs []Struct
6765
}
6866

6967
func (i *importer) usesType(typ string) bool {
@@ -157,7 +155,7 @@ var pqtypeTypes = map[string]struct{}{
157155
"pqtype.NullRawMessage": {},
158156
}
159157

160-
func buildImports(settings *plugin.Settings, options *opts.Options, queries []Query, uses func(string) bool) (map[string]struct{}, map[ImportSpec]struct{}) {
158+
func buildImports(options *opts.Options, queries []Query, uses func(string) bool) (map[string]struct{}, map[ImportSpec]struct{}) {
161159
pkg := make(map[ImportSpec]struct{})
162160
std := make(map[string]struct{})
163161

@@ -201,7 +199,8 @@ func buildImports(settings *plugin.Settings, options *opts.Options, queries []Qu
201199
}
202200

203201
overrideTypes := map[string]string{}
204-
for _, o := range settings.Overrides {
202+
for _, override := range options.Overrides {
203+
o := override.ShimOverride
205204
if o.GoType.BasicType || o.GoType.TypeName == "" {
206205
continue
207206
}
@@ -226,7 +225,9 @@ func buildImports(settings *plugin.Settings, options *opts.Options, queries []Qu
226225
}
227226

228227
// Custom imports
229-
for _, o := range settings.Overrides {
228+
for _, override := range options.Overrides {
229+
o := override.ShimOverride
230+
230231
if o.GoType.BasicType || o.GoType.TypeName == "" {
231232
continue
232233
}
@@ -241,7 +242,7 @@ func buildImports(settings *plugin.Settings, options *opts.Options, queries []Qu
241242
}
242243

243244
func (i *importer) interfaceImports() fileImports {
244-
std, pkg := buildImports(i.Settings, i.Options, i.Queries, func(name string) bool {
245+
std, pkg := buildImports(i.Options, i.Queries, func(name string) bool {
245246
for _, q := range i.Queries {
246247
if q.hasRetType() {
247248
if usesBatch([]Query{q}) {
@@ -266,7 +267,7 @@ func (i *importer) interfaceImports() fileImports {
266267
}
267268

268269
func (i *importer) modelImports() fileImports {
269-
std, pkg := buildImports(i.Settings, i.Options, nil, i.usesType)
270+
std, pkg := buildImports(i.Options, nil, i.usesType)
270271

271272
if len(i.Enums) > 0 {
272273
std["fmt"] = struct{}{}
@@ -305,7 +306,7 @@ func (i *importer) queryImports(filename string) fileImports {
305306
}
306307
}
307308

308-
std, pkg := buildImports(i.Settings, i.Options, gq, func(name string) bool {
309+
std, pkg := buildImports(i.Options, gq, func(name string) bool {
309310
for _, q := range gq {
310311
if q.hasRetType() {
311312
if q.Ret.EmitStruct() {
@@ -406,7 +407,7 @@ func (i *importer) copyfromImports() fileImports {
406407
copyFromQueries = append(copyFromQueries, q)
407408
}
408409
}
409-
std, pkg := buildImports(i.Settings, i.Options, copyFromQueries, func(name string) bool {
410+
std, pkg := buildImports(i.Options, copyFromQueries, func(name string) bool {
410411
for _, q := range copyFromQueries {
411412
if q.hasRetType() {
412413
if strings.HasPrefix(q.Ret.Type(), name) {
@@ -441,7 +442,7 @@ func (i *importer) batchImports() fileImports {
441442
batchQueries = append(batchQueries, q)
442443
}
443444
}
444-
std, pkg := buildImports(i.Settings, i.Options, batchQueries, func(name string) bool {
445+
std, pkg := buildImports(i.Options, batchQueries, func(name string) bool {
445446
for _, q := range batchQueries {
446447
if q.hasRetType() {
447448
if q.Ret.EmitStruct() {

internal/codegen/golang/mysql_type.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package golang
33
import (
44
"log"
55

6+
"github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
67
"github.com/sqlc-dev/sqlc/internal/codegen/sdk"
78
"github.com/sqlc-dev/sqlc/internal/debug"
89
"github.com/sqlc-dev/sqlc/internal/plugin"
910
)
1011

11-
func mysqlType(req *plugin.CodeGenRequest, col *plugin.Column) string {
12+
func mysqlType(req *plugin.CodeGenRequest, options *opts.Options, col *plugin.Column) string {
1213
columnType := sdk.DataType(col.Type)
1314
notNull := col.NotNull || col.IsArray
1415
unsigned := col.Unsigned
@@ -101,14 +102,14 @@ func mysqlType(req *plugin.CodeGenRequest, col *plugin.Column) string {
101102
if enum.Name == columnType {
102103
if notNull {
103104
if schema.Name == req.Catalog.DefaultSchema {
104-
return StructName(enum.Name, req.Settings)
105+
return StructName(enum.Name, options)
105106
}
106-
return StructName(schema.Name+"_"+enum.Name, req.Settings)
107+
return StructName(schema.Name+"_"+enum.Name, options)
107108
} else {
108109
if schema.Name == req.Catalog.DefaultSchema {
109-
return "Null" + StructName(enum.Name, req.Settings)
110+
return "Null" + StructName(enum.Name, options)
110111
}
111-
return "Null" + StructName(schema.Name+"_"+enum.Name, req.Settings)
112+
return "Null" + StructName(schema.Name+"_"+enum.Name, options)
112113
}
113114
}
114115
}

0 commit comments

Comments
 (0)