Skip to content

Commit 5ef20c6

Browse files
refactor(codegen): Remove golang and json settings from plugin proto (sqlc-dev#2822)
* refactor(codegen): remove golang and json settings from plugin proto In order to simplify the codegen plugin proto, I've removed the Go and JSON plugin settings and instead pass those settings encoded as JSON to their respective plugins. * clean up proto * update sqlc-gen-json tests * put json plugin settings in `PluginOptions`
1 parent 1b242a2 commit 5ef20c6

File tree

18 files changed

+1188
-3879
lines changed

18 files changed

+1188
-3879
lines changed

devenv.nix

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
pkgs.git-cliff
1010
pkgs.govulncheck
1111
pkgs.gopls
12+
pkgs.golint
1213
pkgs.mysql-shell
1314
pkgs.postgresql_15
1415
pkgs.python311

internal/cmd/generate.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"bytes"
55
"context"
6+
"encoding/json"
67
"errors"
78
"fmt"
89
"io"
@@ -16,7 +17,7 @@ import (
1617
"google.golang.org/grpc/status"
1718

1819
"github.com/sqlc-dev/sqlc/internal/codegen/golang"
19-
"github.com/sqlc-dev/sqlc/internal/codegen/json"
20+
genjson "github.com/sqlc-dev/sqlc/internal/codegen/json"
2021
"github.com/sqlc-dev/sqlc/internal/compiler"
2122
"github.com/sqlc-dev/sqlc/internal/config"
2223
"github.com/sqlc-dev/sqlc/internal/config/convert"
@@ -166,7 +167,7 @@ func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer
166167
Gen: config.SQLGen{JSON: sql.Gen.JSON},
167168
})
168169
}
169-
for i, _ := range sql.Codegen {
170+
for i := range sql.Codegen {
170171
pairs = append(pairs, outPair{
171172
SQL: sql,
172173
Plugin: &sql.Codegen[i],
@@ -399,10 +400,20 @@ func codegen(ctx context.Context, combo config.CombinedSettings, sql outPair, re
399400
case sql.Gen.Go != nil:
400401
out = combo.Go.Out
401402
handler = ext.HandleFunc(golang.Generate)
403+
opts, err := json.Marshal(sql.Gen.Go)
404+
if err != nil {
405+
return "", nil, fmt.Errorf("opts marshal failed: %w", err)
406+
}
407+
req.PluginOptions = opts
402408

403409
case sql.Gen.JSON != nil:
404410
out = combo.JSON.Out
405-
handler = ext.HandleFunc(json.Generate)
411+
handler = ext.HandleFunc(genjson.Generate)
412+
opts, err := json.Marshal(sql.Gen.JSON)
413+
if err != nil {
414+
return "", nil, fmt.Errorf("opts marshal failed: %w", err)
415+
}
416+
req.PluginOptions = opts
406417

407418
default:
408419
return "", nil, fmt.Errorf("missing language backend")

internal/cmd/shim.go

-49
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ func pluginSettings(r *compiler.Result, cs config.CombinedSettings) *plugin.Sett
5858
Overrides: over,
5959
Rename: cs.Rename,
6060
Codegen: pluginCodegen(cs.Codegen),
61-
Go: pluginGoCode(cs.Go),
62-
Json: pluginJSONCode(cs.JSON),
6361
}
6462
}
6563

@@ -75,45 +73,6 @@ func pluginCodegen(s config.Codegen) *plugin.Codegen {
7573
}
7674
}
7775

78-
func pluginGoCode(s config.SQLGo) *plugin.GoCode {
79-
if s.QueryParameterLimit == nil {
80-
s.QueryParameterLimit = new(int32)
81-
*s.QueryParameterLimit = 1
82-
}
83-
84-
return &plugin.GoCode{
85-
EmitInterface: s.EmitInterface,
86-
EmitJsonTags: s.EmitJSONTags,
87-
JsonTagsIdUppercase: s.JsonTagsIDUppercase,
88-
EmitDbTags: s.EmitDBTags,
89-
EmitPreparedQueries: s.EmitPreparedQueries,
90-
EmitExactTableNames: s.EmitExactTableNames,
91-
EmitEmptySlices: s.EmitEmptySlices,
92-
EmitExportedQueries: s.EmitExportedQueries,
93-
EmitResultStructPointers: s.EmitResultStructPointers,
94-
EmitParamsStructPointers: s.EmitParamsStructPointers,
95-
EmitMethodsWithDbArgument: s.EmitMethodsWithDBArgument,
96-
EmitPointersForNullTypes: s.EmitPointersForNullTypes,
97-
EmitEnumValidMethod: s.EmitEnumValidMethod,
98-
EmitAllEnumValues: s.EmitAllEnumValues,
99-
JsonTagsCaseStyle: s.JSONTagsCaseStyle,
100-
Package: s.Package,
101-
Out: s.Out,
102-
SqlPackage: s.SQLPackage,
103-
SqlDriver: s.SQLDriver,
104-
OutputDbFileName: s.OutputDBFileName,
105-
OutputBatchFileName: s.OutputBatchFileName,
106-
OutputModelsFileName: s.OutputModelsFileName,
107-
OutputQuerierFileName: s.OutputQuerierFileName,
108-
OutputCopyfromFileName: s.OutputCopyFromFileName,
109-
OutputFilesSuffix: s.OutputFilesSuffix,
110-
InflectionExcludeTableNames: s.InflectionExcludeTableNames,
111-
QueryParameterLimit: s.QueryParameterLimit,
112-
OmitUnusedStructs: s.OmitUnusedStructs,
113-
BuildTags: s.BuildTags,
114-
}
115-
}
116-
11776
func pluginGoType(o config.Override) *plugin.ParsedGoType {
11877
// Note that there is a slight mismatch between this and the
11978
// proto api. The GoType on the override is the unparsed type,
@@ -128,14 +87,6 @@ func pluginGoType(o config.Override) *plugin.ParsedGoType {
12887
}
12988
}
13089

131-
func pluginJSONCode(s config.SQLJSON) *plugin.JSONCode {
132-
return &plugin.JSONCode{
133-
Out: s.Out,
134-
Indent: s.Indent,
135-
Filename: s.Filename,
136-
}
137-
}
138-
13990
func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
14091
var schemas []*plugin.Schema
14192
for _, s := range c.Schemas {

internal/codegen/golang/field.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ func TagsToString(tags map[string]string) string {
4040
return strings.Join(tagParts, " ")
4141
}
4242

43-
func JSONTagName(name string, settings *plugin.Settings) string {
44-
style := settings.Go.JsonTagsCaseStyle
45-
idUppercase := settings.Go.JsonTagsIdUppercase
43+
func JSONTagName(name string, options *opts) string {
44+
style := options.JsonTagsCaseStyle
45+
idUppercase := options.JsonTagsIdUppercase
4646
if style == "" || style == "none" {
4747
return name
4848
} else {

internal/codegen/golang/gen.go

+39-34
Original file line numberDiff line numberDiff line change
@@ -103,55 +103,60 @@ func (t *tmplCtx) codegenQueryRetval(q Query) (string, error) {
103103
}
104104

105105
func Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {
106-
enums := buildEnums(req)
107-
structs := buildStructs(req)
108-
queries, err := buildQueries(req, structs)
106+
options, err := parseOpts(req)
109107
if err != nil {
110108
return nil, err
111109
}
112110

113-
if req.Settings.Go.OmitUnusedStructs {
111+
enums := buildEnums(req, options)
112+
structs := buildStructs(req, options)
113+
queries, err := buildQueries(req, options, structs)
114+
if err != nil {
115+
return nil, err
116+
}
117+
118+
if options.OmitUnusedStructs {
114119
enums, structs = filterUnusedStructs(enums, structs, queries)
115120
}
116121

117-
return generate(req, enums, structs, queries)
122+
return generate(req, options, enums, structs, queries)
118123
}
119124

120-
func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, queries []Query) (*plugin.CodeGenResponse, error) {
125+
func generate(req *plugin.CodeGenRequest, options *opts, enums []Enum, structs []Struct, queries []Query) (*plugin.CodeGenResponse, error) {
121126
i := &importer{
122127
Settings: req.Settings,
128+
Options: options,
123129
Queries: queries,
124130
Enums: enums,
125131
Structs: structs,
126132
}
127133

128-
golang := req.Settings.Go
129134
tctx := tmplCtx{
130-
EmitInterface: golang.EmitInterface,
131-
EmitJSONTags: golang.EmitJsonTags,
132-
JsonTagsIDUppercase: golang.JsonTagsIdUppercase,
133-
EmitDBTags: golang.EmitDbTags,
134-
EmitPreparedQueries: golang.EmitPreparedQueries,
135-
EmitEmptySlices: golang.EmitEmptySlices,
136-
EmitMethodsWithDBArgument: golang.EmitMethodsWithDbArgument,
137-
EmitEnumValidMethod: golang.EmitEnumValidMethod,
138-
EmitAllEnumValues: golang.EmitAllEnumValues,
135+
EmitInterface: options.EmitInterface,
136+
EmitJSONTags: options.EmitJsonTags,
137+
JsonTagsIDUppercase: options.JsonTagsIdUppercase,
138+
EmitDBTags: options.EmitDbTags,
139+
EmitPreparedQueries: options.EmitPreparedQueries,
140+
EmitEmptySlices: options.EmitEmptySlices,
141+
EmitMethodsWithDBArgument: options.EmitMethodsWithDbArgument,
142+
EmitEnumValidMethod: options.EmitEnumValidMethod,
143+
EmitAllEnumValues: options.EmitAllEnumValues,
139144
UsesCopyFrom: usesCopyFrom(queries),
140145
UsesBatch: usesBatch(queries),
141-
SQLDriver: parseDriver(golang.SqlPackage),
146+
SQLDriver: parseDriver(options.SqlPackage),
142147
Q: "`",
143-
Package: golang.Package,
148+
Package: options.Package,
144149
Enums: enums,
145150
Structs: structs,
146151
SqlcVersion: req.SqlcVersion,
147-
BuildTags: golang.BuildTags,
152+
BuildTags: options.BuildTags,
148153
}
149154

150-
if tctx.UsesCopyFrom && !tctx.SQLDriver.IsPGX() && golang.SqlDriver != SQLDriverGoSQLDriverMySQL {
155+
if tctx.UsesCopyFrom && !tctx.SQLDriver.IsPGX() && options.SqlDriver != SQLDriverGoSQLDriverMySQL {
151156
return nil, errors.New(":copyfrom is only supported by pgx and github.com/go-sql-driver/mysql")
152157
}
153158

154-
if tctx.UsesCopyFrom && golang.SqlDriver == SQLDriverGoSQLDriverMySQL {
159+
if tctx.UsesCopyFrom && options.SqlDriver == SQLDriverGoSQLDriverMySQL {
155160
if err := checkNoTimesForMySQLCopyFrom(queries); err != nil {
156161
return nil, err
157162
}
@@ -208,8 +213,8 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
208213
return fmt.Errorf("source error: %w", err)
209214
}
210215

211-
if templateName == "queryFile" && golang.OutputFilesSuffix != "" {
212-
name += golang.OutputFilesSuffix
216+
if templateName == "queryFile" && options.OutputFilesSuffix != "" {
217+
name += options.OutputFilesSuffix
213218
}
214219

215220
if !strings.HasSuffix(name, ".go") {
@@ -220,25 +225,25 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
220225
}
221226

222227
dbFileName := "db.go"
223-
if golang.OutputDbFileName != "" {
224-
dbFileName = golang.OutputDbFileName
228+
if options.OutputDbFileName != "" {
229+
dbFileName = options.OutputDbFileName
225230
}
226231
modelsFileName := "models.go"
227-
if golang.OutputModelsFileName != "" {
228-
modelsFileName = golang.OutputModelsFileName
232+
if options.OutputModelsFileName != "" {
233+
modelsFileName = options.OutputModelsFileName
229234
}
230235
querierFileName := "querier.go"
231-
if golang.OutputQuerierFileName != "" {
232-
querierFileName = golang.OutputQuerierFileName
236+
if options.OutputQuerierFileName != "" {
237+
querierFileName = options.OutputQuerierFileName
233238
}
234239
copyfromFileName := "copyfrom.go"
235-
if golang.OutputCopyfromFileName != "" {
236-
copyfromFileName = golang.OutputCopyfromFileName
240+
if options.OutputCopyfromFileName != "" {
241+
copyfromFileName = options.OutputCopyfromFileName
237242
}
238243

239244
batchFileName := "batch.go"
240-
if golang.OutputBatchFileName != "" {
241-
batchFileName = golang.OutputBatchFileName
245+
if options.OutputBatchFileName != "" {
246+
batchFileName = options.OutputBatchFileName
242247
}
243248

244249
if err := execute(dbFileName, "dbFile"); err != nil {
@@ -247,7 +252,7 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
247252
if err := execute(modelsFileName, "modelsFile"); err != nil {
248253
return nil, err
249254
}
250-
if golang.EmitInterface {
255+
if options.EmitInterface {
251256
if err := execute(querierFileName, "interfaceFile"); err != nil {
252257
return nil, err
253258
}

internal/codegen/golang/go_type.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func addExtraGoStructTags(tags map[string]string, req *plugin.CodeGenRequest, co
3131
}
3232
}
3333

34-
func goType(req *plugin.CodeGenRequest, col *plugin.Column) string {
34+
func goType(req *plugin.CodeGenRequest, options *opts, col *plugin.Column) string {
3535
// Check if the column's type has been overridden
3636
for _, oride := range req.Settings.Overrides {
3737
if oride.GoType.TypeName == "" {
@@ -49,7 +49,7 @@ func goType(req *plugin.CodeGenRequest, col *plugin.Column) string {
4949
return oride.GoType.TypeName
5050
}
5151
}
52-
typ := goInnerType(req, col)
52+
typ := goInnerType(req, options, col)
5353
if col.IsSqlcSlice {
5454
return "[]" + typ
5555
}
@@ -59,7 +59,7 @@ func goType(req *plugin.CodeGenRequest, col *plugin.Column) string {
5959
return typ
6060
}
6161

62-
func goInnerType(req *plugin.CodeGenRequest, col *plugin.Column) string {
62+
func goInnerType(req *plugin.CodeGenRequest, options *opts, col *plugin.Column) string {
6363
columnType := sdk.DataType(col.Type)
6464
notNull := col.NotNull || col.IsArray
6565

@@ -78,7 +78,7 @@ func goInnerType(req *plugin.CodeGenRequest, col *plugin.Column) string {
7878
case "mysql":
7979
return mysqlType(req, col)
8080
case "postgresql":
81-
return postgresType(req, col)
81+
return postgresType(req, options, col)
8282
case "sqlite":
8383
return sqliteType(req, col)
8484
default:

0 commit comments

Comments
 (0)