Skip to content

Commit 3853612

Browse files
committed
refactor: Remove ParsedGoType from codegen.proto, pass in opts as JSON
The `ParsedGoType` configuration only applies to Go, so we remove it from the codegen plugin proto and instead pass the configuration as JSON to be unmarshaled by the codegen/golang package like other options. Eventually we will need to push all of the validation of overrides from internal/config into the codegen/golang package, but that's a fair bit of work so I didn't push it here. Aside from the proto change, which is obviously important, the other meaningful change is to generate.go on line 421, and the function implementation in shim.go. The types in opts/override.go are just recreations of what the plugin proto types were.
1 parent c84379f commit 3853612

File tree

12 files changed

+456
-1939
lines changed

12 files changed

+456
-1939
lines changed

internal/cmd/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ func codegen(ctx context.Context, combo config.CombinedSettings, sql outPair, re
418418
case sql.Gen.Go != nil:
419419
out = combo.Go.Out
420420
handler = ext.HandleFunc(golang.Generate)
421-
opts, err := json.Marshal(sql.Gen.Go)
421+
opts, err := json.Marshal(pluginGoOpts(sql.Gen.Go, combo, result))
422422
if err != nil {
423423
return "", nil, fmt.Errorf("opts marshal failed: %w", err)
424424
}

internal/cmd/shim.go

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"strings"
55

6+
goopts "github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
67
"github.com/sqlc-dev/sqlc/internal/compiler"
78
"github.com/sqlc-dev/sqlc/internal/config"
89
"github.com/sqlc-dev/sqlc/internal/config/convert"
@@ -11,7 +12,7 @@ import (
1112
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
1213
)
1314

14-
func pluginOverride(r *compiler.Result, o config.Override) *plugin.Override {
15+
func pluginOverride(r *compiler.Result, o config.Override) goopts.Override {
1516
var column string
1617
var table plugin.Identifier
1718

@@ -33,7 +34,7 @@ func pluginOverride(r *compiler.Result, o config.Override) *plugin.Override {
3334
column = colParts[3]
3435
}
3536
}
36-
return &plugin.Override{
37+
return goopts.Override{
3738
CodeType: "", // FIXME
3839
DbType: o.DBType,
3940
Nullable: o.Nullable,
@@ -46,18 +47,13 @@ func pluginOverride(r *compiler.Result, o config.Override) *plugin.Override {
4647
}
4748

4849
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-
}
5350
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),
51+
Version: cs.Global.Version,
52+
Engine: string(cs.Package.Engine),
53+
Schema: []string(cs.Package.Schema),
54+
Queries: []string(cs.Package.Queries),
55+
Rename: cs.Rename,
56+
Codegen: pluginCodegen(cs, cs.Codegen),
6157
}
6258
}
6359

@@ -101,12 +97,12 @@ func pluginWASM(p config.Plugin) *plugin.Codegen_WASM {
10197
return nil
10298
}
10399

104-
func pluginGoType(o config.Override) *plugin.ParsedGoType {
100+
func pluginGoType(o config.Override) *goopts.ParsedGoType {
105101
// Note that there is a slight mismatch between this and the
106102
// proto api. The GoType on the override is the unparsed type,
107103
// which could be a qualified path or an object, as per
108104
// https://docs.sqlc.dev/en/v1.18.0/reference/config.html#type-overriding
109-
return &plugin.ParsedGoType{
105+
return &goopts.ParsedGoType{
110106
ImportPath: o.GoImportPath,
111107
Package: o.GoPackage,
112108
TypeName: o.GoTypeName,
@@ -115,6 +111,46 @@ func pluginGoType(o config.Override) *plugin.ParsedGoType {
115111
}
116112
}
117113

114+
func pluginGoOpts(sqlGo *config.SQLGo, cs config.CombinedSettings, r *compiler.Result) *goopts.Options {
115+
var overrides []goopts.Override
116+
for _, o := range cs.Overrides {
117+
overrides = append(overrides, pluginOverride(r, o))
118+
}
119+
return &goopts.Options{
120+
EmitInterface: sqlGo.EmitInterface,
121+
EmitJsonTags: sqlGo.EmitJSONTags,
122+
JsonTagsIdUppercase: sqlGo.JsonTagsIDUppercase,
123+
EmitDbTags: sqlGo.EmitDBTags,
124+
EmitPreparedQueries: sqlGo.EmitPreparedQueries,
125+
EmitExactTableNames: sqlGo.EmitExactTableNames,
126+
EmitEmptySlices: sqlGo.EmitEmptySlices,
127+
EmitExportedQueries: sqlGo.EmitExportedQueries,
128+
EmitResultStructPointers: sqlGo.EmitResultStructPointers,
129+
EmitParamsStructPointers: sqlGo.EmitParamsStructPointers,
130+
EmitMethodsWithDbArgument: sqlGo.EmitMethodsWithDBArgument,
131+
EmitPointersForNullTypes: sqlGo.EmitPointersForNullTypes,
132+
EmitEnumValidMethod: sqlGo.EmitEnumValidMethod,
133+
EmitAllEnumValues: sqlGo.EmitAllEnumValues,
134+
JsonTagsCaseStyle: sqlGo.JSONTagsCaseStyle,
135+
Package: sqlGo.Package,
136+
Out: sqlGo.Out,
137+
Overrides: overrides,
138+
// Rename intentionally omitted
139+
SqlPackage: sqlGo.SQLPackage,
140+
SqlDriver: sqlGo.SQLDriver,
141+
OutputBatchFileName: sqlGo.OutputBatchFileName,
142+
OutputDbFileName: sqlGo.OutputDBFileName,
143+
OutputModelsFileName: sqlGo.OutputModelsFileName,
144+
OutputQuerierFileName: sqlGo.OutputQuerierFileName,
145+
OutputCopyfromFileName: sqlGo.OutputCopyFromFileName,
146+
OutputFilesSuffix: sqlGo.OutputFilesSuffix,
147+
InflectionExcludeTableNames: sqlGo.InflectionExcludeTableNames,
148+
QueryParameterLimit: sqlGo.QueryParameterLimit,
149+
OmitUnusedStructs: sqlGo.OmitUnusedStructs,
150+
BuildTags: sqlGo.BuildTags,
151+
}
152+
}
153+
118154
func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
119155
var schemas []*plugin.Schema
120156
for _, s := range c.Schemas {

internal/codegen/golang/go_type.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ 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 _, oride := range options.Overrides {
1313
if oride.GoType.StructTags == nil {
1414
continue
1515
}
16-
if !sdk.Matches(oride, col.Table, req.Catalog.DefaultSchema) {
16+
if !oride.Matches(col.Table, req.Catalog.DefaultSchema) {
1717
// Different table.
1818
continue
1919
}
@@ -34,15 +34,15 @@ func addExtraGoStructTags(tags map[string]string, req *plugin.CodeGenRequest, co
3434

3535
func goType(req *plugin.CodeGenRequest, options *opts.Options, col *plugin.Column) string {
3636
// Check if the column's type has been overridden
37-
for _, oride := range req.Settings.Overrides {
37+
for _, oride := range options.Overrides {
3838
if oride.GoType.TypeName == "" {
3939
continue
4040
}
4141
cname := col.Name
4242
if col.OriginalName != "" {
4343
cname = col.OriginalName
4444
}
45-
sameTable := sdk.Matches(oride, col.Table, req.Catalog.DefaultSchema)
45+
sameTable := oride.Matches(col.Table, req.Catalog.DefaultSchema)
4646
if oride.Column != "" && sdk.MatchString(oride.ColumnName, cname) && sameTable {
4747
if col.IsSqlcSlice {
4848
return "[]" + oride.GoType.TypeName
@@ -65,7 +65,7 @@ func goInnerType(req *plugin.CodeGenRequest, options *opts.Options, col *plugin.
6565
notNull := col.NotNull || col.IsArray
6666

6767
// package overrides have a higher precedence
68-
for _, oride := range req.Settings.Overrides {
68+
for _, oride := range options.Overrides {
6969
if oride.GoType.TypeName == "" {
7070
continue
7171
}

internal/codegen/golang/imports.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ var pqtypeTypes = map[string]struct{}{
157157
"pqtype.NullRawMessage": {},
158158
}
159159

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

@@ -201,7 +201,7 @@ func buildImports(settings *plugin.Settings, options *opts.Options, queries []Qu
201201
}
202202

203203
overrideTypes := map[string]string{}
204-
for _, o := range settings.Overrides {
204+
for _, o := range options.Overrides {
205205
if o.GoType.BasicType || o.GoType.TypeName == "" {
206206
continue
207207
}
@@ -226,7 +226,7 @@ func buildImports(settings *plugin.Settings, options *opts.Options, queries []Qu
226226
}
227227

228228
// Custom imports
229-
for _, o := range settings.Overrides {
229+
for _, o := range options.Overrides {
230230
if o.GoType.BasicType || o.GoType.TypeName == "" {
231231
continue
232232
}
@@ -241,7 +241,7 @@ func buildImports(settings *plugin.Settings, options *opts.Options, queries []Qu
241241
}
242242

243243
func (i *importer) interfaceImports() fileImports {
244-
std, pkg := buildImports(i.Settings, i.Options, i.Queries, func(name string) bool {
244+
std, pkg := buildImports(i.Options, i.Queries, func(name string) bool {
245245
for _, q := range i.Queries {
246246
if q.hasRetType() {
247247
if usesBatch([]Query{q}) {
@@ -266,7 +266,7 @@ func (i *importer) interfaceImports() fileImports {
266266
}
267267

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

271271
if len(i.Enums) > 0 {
272272
std["fmt"] = struct{}{}
@@ -305,7 +305,7 @@ func (i *importer) queryImports(filename string) fileImports {
305305
}
306306
}
307307

308-
std, pkg := buildImports(i.Settings, i.Options, gq, func(name string) bool {
308+
std, pkg := buildImports(i.Options, gq, func(name string) bool {
309309
for _, q := range gq {
310310
if q.hasRetType() {
311311
if q.Ret.EmitStruct() {
@@ -406,7 +406,7 @@ func (i *importer) copyfromImports() fileImports {
406406
copyFromQueries = append(copyFromQueries, q)
407407
}
408408
}
409-
std, pkg := buildImports(i.Settings, i.Options, copyFromQueries, func(name string) bool {
409+
std, pkg := buildImports(i.Options, copyFromQueries, func(name string) bool {
410410
for _, q := range copyFromQueries {
411411
if q.hasRetType() {
412412
if strings.HasPrefix(q.Ret.Type(), name) {
@@ -441,7 +441,7 @@ func (i *importer) batchImports() fileImports {
441441
batchQueries = append(batchQueries, q)
442442
}
443443
}
444-
std, pkg := buildImports(i.Settings, i.Options, batchQueries, func(name string) bool {
444+
std, pkg := buildImports(i.Options, batchQueries, func(name string) bool {
445445
for _, q := range batchQueries {
446446
if q.hasRetType() {
447447
if q.Ret.EmitStruct() {

internal/codegen/golang/opts/options.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,39 @@ import (
99
)
1010

1111
type Options struct {
12-
EmitInterface bool `json:"emit_interface"`
13-
EmitJsonTags bool `json:"emit_json_tags"`
14-
JsonTagsIdUppercase bool `json:"json_tags_id_uppercase"`
15-
EmitDbTags bool `json:"emit_db_tags"`
16-
EmitPreparedQueries bool `json:"emit_prepared_queries"`
17-
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty"`
18-
EmitEmptySlices bool `json:"emit_empty_slices,omitempty"`
19-
EmitExportedQueries bool `json:"emit_exported_queries"`
20-
EmitResultStructPointers bool `json:"emit_result_struct_pointers"`
21-
EmitParamsStructPointers bool `json:"emit_params_struct_pointers"`
22-
EmitMethodsWithDbArgument bool `json:"emit_methods_with_db_argument,omitempty"`
23-
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types"`
24-
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty"`
25-
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty"`
26-
JsonTagsCaseStyle string `json:"json_tags_case_style,omitempty"`
27-
Package string `json:"package"`
28-
Out string `json:"out"`
29-
SqlPackage string `json:"sql_package"`
30-
SqlDriver string `json:"sql_driver"`
31-
OutputBatchFileName string `json:"output_batch_file_name,omitempty"`
32-
OutputDbFileName string `json:"output_db_file_name,omitempty"`
33-
OutputModelsFileName string `json:"output_models_file_name,omitempty"`
34-
OutputQuerierFileName string `json:"output_querier_file_name,omitempty"`
35-
OutputCopyfromFileName string `json:"output_copyfrom_file_name,omitempty"`
36-
OutputFilesSuffix string `json:"output_files_suffix,omitempty"`
37-
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty"`
38-
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty"`
39-
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty"`
40-
BuildTags string `json:"build_tags,omitempty"`
12+
EmitInterface bool `json:"emit_interface"`
13+
EmitJsonTags bool `json:"emit_json_tags"`
14+
JsonTagsIdUppercase bool `json:"json_tags_id_uppercase"`
15+
EmitDbTags bool `json:"emit_db_tags"`
16+
EmitPreparedQueries bool `json:"emit_prepared_queries"`
17+
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty"`
18+
EmitEmptySlices bool `json:"emit_empty_slices,omitempty"`
19+
EmitExportedQueries bool `json:"emit_exported_queries"`
20+
EmitResultStructPointers bool `json:"emit_result_struct_pointers"`
21+
EmitParamsStructPointers bool `json:"emit_params_struct_pointers"`
22+
EmitMethodsWithDbArgument bool `json:"emit_methods_with_db_argument,omitempty"`
23+
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types"`
24+
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty"`
25+
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty"`
26+
JsonTagsCaseStyle string `json:"json_tags_case_style,omitempty"`
27+
Package string `json:"package"`
28+
Out string `json:"out"`
29+
Overrides []Override `json:"overrides,omitempty"`
30+
SqlPackage string `json:"sql_package"`
31+
SqlDriver string `json:"sql_driver"`
32+
OutputBatchFileName string `json:"output_batch_file_name,omitempty"`
33+
OutputDbFileName string `json:"output_db_file_name,omitempty"`
34+
OutputModelsFileName string `json:"output_models_file_name,omitempty"`
35+
OutputQuerierFileName string `json:"output_querier_file_name,omitempty"`
36+
OutputCopyfromFileName string `json:"output_copyfrom_file_name,omitempty"`
37+
OutputFilesSuffix string `json:"output_files_suffix,omitempty"`
38+
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty"`
39+
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty"`
40+
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty"`
41+
BuildTags string `json:"build_tags,omitempty"`
4142

4243
// Unused but included in marshaled json we receive
43-
Overrides json.RawMessage `json:"overrides,omitempty"`
44-
Rename json.RawMessage `json:"rename,omitempty"`
44+
Rename json.RawMessage `json:"rename,omitempty"`
4545
}
4646

4747
func ParseOpts(req *plugin.CodeGenRequest) (*Options, error) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package opts
2+
3+
import (
4+
"github.com/sqlc-dev/sqlc/internal/codegen/sdk"
5+
"github.com/sqlc-dev/sqlc/internal/plugin"
6+
)
7+
8+
type Override struct {
9+
CodeType string `json:"code_type"`
10+
DbType string `json:"db_type"`
11+
Nullable bool `json:"nullable"`
12+
Column string `json:"column"`
13+
Table *plugin.Identifier `json:"table"`
14+
ColumnName string `json:"column_name"`
15+
GoType *ParsedGoType `json:"go_type"`
16+
Unsigned bool `json:"unsigned"`
17+
}
18+
19+
type ParsedGoType struct {
20+
ImportPath string `json:"import_path"`
21+
Package string `json:"package"`
22+
TypeName string `json:"type_name"`
23+
BasicType bool `json:"basic_type"`
24+
StructTags map[string]string `json:"struct_tags"`
25+
}
26+
27+
func (o *Override) Matches(n *plugin.Identifier, defaultSchema string) bool {
28+
if n == nil {
29+
return false
30+
}
31+
schema := n.Schema
32+
if n.Schema == "" {
33+
schema = defaultSchema
34+
}
35+
if o.Table.Catalog != "" && !sdk.MatchString(o.Table.Catalog, n.Catalog) {
36+
return false
37+
}
38+
if o.Table.Schema == "" && schema != "" {
39+
return false
40+
}
41+
if o.Table.Schema != "" && !sdk.MatchString(o.Table.Schema, schema) {
42+
return false
43+
}
44+
if o.Table.Name == "" && n.Name != "" {
45+
return false
46+
}
47+
if o.Table.Name != "" && !sdk.MatchString(o.Table.Name, n.Name) {
48+
return false
49+
}
50+
return true
51+
}

internal/codegen/golang/result.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func buildStructs(req *plugin.CodeGenRequest, options *opts.Options) []Struct {
9292
if options.EmitJsonTags {
9393
tags["json"] = JSONTagName(column.Name, options)
9494
}
95-
addExtraGoStructTags(tags, req, column)
95+
addExtraGoStructTags(tags, req, options, column)
9696
s.Fields = append(s.Fields, Field{
9797
Name: StructName(column.Name, req.Settings),
9898
Type: goType(req, options, column),
@@ -370,7 +370,7 @@ func columnsToStruct(req *plugin.CodeGenRequest, options *opts.Options, name str
370370
if options.EmitJsonTags {
371371
tags["json"] = JSONTagName(tagName, options)
372372
}
373-
addExtraGoStructTags(tags, req, c.Column)
373+
addExtraGoStructTags(tags, req, options, c.Column)
374374
f := Field{
375375
Name: fieldName,
376376
DBName: colName,

0 commit comments

Comments
 (0)