Skip to content

Commit aade087

Browse files
authored
refactor(codegen): Port Go to plugin types (#1460)
* convert to spaces in proto, fix up insert_into_table * remove use of core.FQN from sameTableName
1 parent 20817d7 commit aade087

File tree

17 files changed

+1045
-450
lines changed

17 files changed

+1045
-450
lines changed

internal/cmd/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer
193193
switch {
194194
case sql.Gen.Go != nil:
195195
out = combo.Go.Out
196-
files, err = golang.Generate(result, combo)
196+
resp, err = golang.Generate(codeGenRequest(result, combo))
197197
case sql.Gen.Kotlin != nil:
198198
out = combo.Kotlin.Out
199199
resp, err = kotlin.Generate(codeGenRequest(result, combo))

internal/cmd/shim.go

Lines changed: 79 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func pluginOverride(o config.Override) *plugin.Override {
3939
ColumnName: column,
4040
Table: &table,
4141
PythonType: pluginPythonType(o.PythonType),
42+
GoType: pluginGoType(o),
4243
}
4344
}
4445

@@ -56,6 +57,7 @@ func pluginSettings(cs config.CombinedSettings) *plugin.Settings {
5657
Rename: cs.Rename,
5758
Python: pluginPythonCode(cs.Python),
5859
Kotlin: pluginKotlinCode(cs.Kotlin),
60+
Go: pluginGoCode(cs.Go),
5961
}
6062
}
6163

@@ -69,6 +71,42 @@ func pluginPythonCode(s config.SQLPython) *plugin.PythonCode {
6971
}
7072
}
7173

74+
func pluginGoCode(s config.SQLGo) *plugin.GoCode {
75+
return &plugin.GoCode{
76+
EmitInterface: s.EmitInterface,
77+
EmitJsonTags: s.EmitJSONTags,
78+
EmitDbTags: s.EmitDBTags,
79+
EmitPreparedQueries: s.EmitPreparedQueries,
80+
EmitExactTableNames: s.EmitExactTableNames,
81+
EmitEmptySlices: s.EmitEmptySlices,
82+
EmitExportedQueries: s.EmitExportedQueries,
83+
EmitResultStructPointers: s.EmitResultStructPointers,
84+
EmitParamsStructPointers: s.EmitParamsStructPointers,
85+
EmitMethodsWithDbArgument: s.EmitMethodsWithDBArgument,
86+
JsonTagsCaseStyle: s.JSONTagsCaseStyle,
87+
Package: s.Package,
88+
Out: s.Out,
89+
SqlPackage: s.SQLPackage,
90+
OutputDbFileName: s.OutputDBFileName,
91+
OutputModelsFileName: s.OutputModelsFileName,
92+
OutputQuerierFileName: s.OutputQuerierFileName,
93+
OutputFilesSuffix: s.OutputFilesSuffix,
94+
}
95+
}
96+
97+
func pluginGoType(o config.Override) *plugin.ParsedGoType {
98+
// Note that there is a slight mismatch between this and the
99+
// proto api. The GoType on the override is the unparsed type,
100+
// which could be a qualified path or an object, as per
101+
// https://docs.sqlc.dev/en/latest/reference/config.html#renaming-struct-fields
102+
return &plugin.ParsedGoType{
103+
ImportPath: o.GoImportPath,
104+
Package: o.GoPackage,
105+
TypeName: o.GoTypeName,
106+
BasicType: o.GoBasicType,
107+
}
108+
}
109+
72110
func pluginPythonType(pt config.PythonType) *plugin.PythonType {
73111
return &plugin.PythonType{
74112
Module: pt.Module,
@@ -88,16 +126,21 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
88126
var schemas []*plugin.Schema
89127
for _, s := range c.Schemas {
90128
var enums []*plugin.Enum
129+
var cts []*plugin.CompositeType
91130
for _, typ := range s.Types {
92-
enum, ok := typ.(*catalog.Enum)
93-
if !ok {
94-
continue
131+
switch typ := typ.(type) {
132+
case *catalog.Enum:
133+
enums = append(enums, &plugin.Enum{
134+
Name: typ.Name,
135+
Comment: typ.Comment,
136+
Vals: typ.Vals,
137+
})
138+
case *catalog.CompositeType:
139+
cts = append(cts, &plugin.CompositeType{
140+
Name: typ.Name,
141+
Comment: typ.Comment,
142+
})
95143
}
96-
enums = append(enums, &plugin.Enum{
97-
Name: enum.Name,
98-
Comment: enum.Comment,
99-
Vals: enum.Vals,
100-
})
101144
}
102145
var tables []*plugin.Table
103146
for _, t := range s.Tables {
@@ -136,10 +179,11 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
136179
})
137180
}
138181
schemas = append(schemas, &plugin.Schema{
139-
Comment: s.Comment,
140-
Name: s.Name,
141-
Tables: tables,
142-
Enums: enums,
182+
Comment: s.Comment,
183+
Name: s.Name,
184+
Tables: tables,
185+
Enums: enums,
186+
CompositeTypes: cts,
143187
})
144188
}
145189
return &plugin.Catalog{
@@ -161,14 +205,23 @@ func pluginQueries(r *compiler.Result) []*plugin.Query {
161205
for _, p := range q.Params {
162206
params = append(params, pluginQueryParam(p))
163207
}
208+
var iit *plugin.Identifier
209+
if q.InsertIntoTable != nil {
210+
iit = &plugin.Identifier{
211+
Catalog: q.InsertIntoTable.Catalog,
212+
Schema: q.InsertIntoTable.Schema,
213+
Name: q.InsertIntoTable.Name,
214+
}
215+
}
164216
out = append(out, &plugin.Query{
165-
Name: q.Name,
166-
Cmd: q.Cmd,
167-
Text: q.SQL,
168-
Comments: q.Comments,
169-
Columns: columns,
170-
Params: params,
171-
Filename: q.Filename,
217+
Name: q.Name,
218+
Cmd: q.Cmd,
219+
Text: q.SQL,
220+
Comments: q.Comments,
221+
Columns: columns,
222+
Params: params,
223+
Filename: q.Filename,
224+
InsertIntoTable: iit,
172225
})
173226
}
174227
return out
@@ -180,11 +233,13 @@ func pluginQueryColumn(c *compiler.Column) *plugin.Column {
180233
l = *c.Length
181234
}
182235
out := &plugin.Column{
183-
Name: c.Name,
184-
Comment: c.Comment,
185-
NotNull: c.NotNull,
186-
IsArray: c.IsArray,
187-
Length: int32(l),
236+
Name: c.Name,
237+
Comment: c.Comment,
238+
NotNull: c.NotNull,
239+
IsArray: c.IsArray,
240+
Length: int32(l),
241+
IsNamedParam: c.IsNamedParam,
242+
IsFuncCall: c.IsFuncCall,
188243
}
189244

190245
if c.Type != nil {

internal/codegen/golang/compat.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package golang
22

33
import (
4-
"github.com/kyleconroy/sqlc/internal/core"
5-
"github.com/kyleconroy/sqlc/internal/sql/ast"
4+
"github.com/kyleconroy/sqlc/internal/plugin"
65
)
76

8-
func sameTableName(n *ast.TableName, f core.FQN, defaultSchema string) bool {
9-
if n == nil {
7+
func sameTableName(tableID, f *plugin.Identifier, defaultSchema string) bool {
8+
if tableID == nil {
109
return false
1110
}
12-
schema := n.Schema
13-
if n.Schema == "" {
11+
schema := tableID.Schema
12+
if tableID.Schema == "" {
1413
schema = defaultSchema
1514
}
16-
return n.Catalog == f.Catalog && schema == f.Schema && n.Name == f.Rel
15+
return tableID.Catalog == f.Catalog && schema == f.Schema && tableID.Name == f.Name
1716
}

internal/codegen/golang/driver.go

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

3-
import "github.com/kyleconroy/sqlc/internal/config"
3+
import (
4+
"github.com/kyleconroy/sqlc/internal/plugin"
5+
)
46

57
type SQLDriver int
68

@@ -9,8 +11,8 @@ const (
911
SQLDriverLibPQ
1012
)
1113

12-
func parseDriver(settings config.CombinedSettings) SQLDriver {
13-
if settings.Go.SQLPackage == "pgx/v4" {
14+
func parseDriver(settings *plugin.Settings) SQLDriver {
15+
if settings.Go.SqlPackage == "pgx/v4" {
1416
return SQLDriverPGXV4
1517
} else {
1618
return SQLDriverLibPQ

internal/codegen/golang/field.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"sort"
66
"strings"
77

8-
"github.com/kyleconroy/sqlc/internal/config"
8+
"github.com/kyleconroy/sqlc/internal/plugin"
99
)
1010

1111
type Field struct {
@@ -28,8 +28,8 @@ func (gf Field) Tag() string {
2828
return strings.Join(tags, " ")
2929
}
3030

31-
func JSONTagName(name string, settings config.CombinedSettings) string {
32-
style := settings.Go.JSONTagsCaseStyle
31+
func JSONTagName(name string, settings *plugin.Settings) string {
32+
style := settings.Go.JsonTagsCaseStyle
3333
if style == "" || style == "none" {
3434
return name
3535
} else {

internal/codegen/golang/gen.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,17 @@ import (
1010
"text/template"
1111

1212
"github.com/kyleconroy/sqlc/internal/codegen"
13-
"github.com/kyleconroy/sqlc/internal/compiler"
14-
"github.com/kyleconroy/sqlc/internal/config"
1513
"github.com/kyleconroy/sqlc/internal/metadata"
14+
"github.com/kyleconroy/sqlc/internal/plugin"
1615
)
1716

18-
type Generateable interface {
19-
Structs(settings config.CombinedSettings) []Struct
20-
GoQueries(settings config.CombinedSettings) []Query
21-
Enums(settings config.CombinedSettings) []Enum
22-
}
23-
2417
type tmplCtx struct {
2518
Q string
2619
Package string
2720
SQLPackage SQLPackage
2821
Enums []Enum
2922
Structs []Struct
3023
GoQueries []Query
31-
Settings config.Config
3224

3325
// TODO: Race conditions
3426
SourceName string
@@ -47,19 +39,19 @@ func (t *tmplCtx) OutputQuery(sourceName string) bool {
4739
return t.SourceName == sourceName
4840
}
4941

50-
func Generate(r *compiler.Result, settings config.CombinedSettings) (map[string]string, error) {
51-
enums := buildEnums(r, settings)
52-
structs := buildStructs(r, settings)
53-
queries, err := buildQueries(r, settings, structs)
42+
func Generate(req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {
43+
enums := buildEnums(req)
44+
structs := buildStructs(req)
45+
queries, err := buildQueries(req, structs)
5446
if err != nil {
5547
return nil, err
5648
}
57-
return generate(settings, enums, structs, queries)
49+
return generate(req, enums, structs, queries)
5850
}
5951

60-
func generate(settings config.CombinedSettings, enums []Enum, structs []Struct, queries []Query) (map[string]string, error) {
52+
func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, queries []Query) (*plugin.CodeGenResponse, error) {
6153
i := &importer{
62-
Settings: settings,
54+
Settings: req.Settings,
6355
Queries: queries,
6456
Enums: enums,
6557
Structs: structs,
@@ -83,18 +75,17 @@ func generate(settings config.CombinedSettings, enums []Enum, structs []Struct,
8375
),
8476
)
8577

86-
golang := settings.Go
78+
golang := req.Settings.Go
8779
tctx := tmplCtx{
88-
Settings: settings.Global,
8980
EmitInterface: golang.EmitInterface,
90-
EmitJSONTags: golang.EmitJSONTags,
91-
EmitDBTags: golang.EmitDBTags,
81+
EmitJSONTags: golang.EmitJsonTags,
82+
EmitDBTags: golang.EmitDbTags,
9283
EmitPreparedQueries: golang.EmitPreparedQueries,
9384
EmitEmptySlices: golang.EmitEmptySlices,
94-
EmitMethodsWithDBArgument: golang.EmitMethodsWithDBArgument,
85+
EmitMethodsWithDBArgument: golang.EmitMethodsWithDbArgument,
9586
UsesCopyFrom: usesCopyFrom(queries),
9687
UsesBatch: usesBatch(queries),
97-
SQLPackage: SQLPackageFromString(golang.SQLPackage),
88+
SQLPackage: SQLPackageFromString(golang.SqlPackage),
9889
Q: "`",
9990
Package: golang.Package,
10091
GoQueries: queries,
@@ -139,8 +130,8 @@ func generate(settings config.CombinedSettings, enums []Enum, structs []Struct,
139130
}
140131

141132
dbFileName := "db.go"
142-
if golang.OutputDBFileName != "" {
143-
dbFileName = golang.OutputDBFileName
133+
if golang.OutputDbFileName != "" {
134+
dbFileName = golang.OutputDbFileName
144135
}
145136
modelsFileName := "models.go"
146137
if golang.OutputModelsFileName != "" {
@@ -187,7 +178,16 @@ func generate(settings config.CombinedSettings, enums []Enum, structs []Struct,
187178
return nil, err
188179
}
189180
}
190-
return output, nil
181+
resp := plugin.CodeGenResponse{}
182+
183+
for filename, code := range output {
184+
resp.Files = append(resp.Files, &plugin.File{
185+
Name: filename,
186+
Contents: []byte(code),
187+
})
188+
}
189+
190+
return &resp, nil
191191
}
192192

193193
func usesCopyFrom(queries []Query) bool {

0 commit comments

Comments
 (0)