-
Notifications
You must be signed in to change notification settings - Fork 881
Refactor go codegen to plugin types #1460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d3de3a8
9367b82
286eb40
433c059
03dcc21
0e89ceb
32f4c96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ func pluginOverride(o config.Override) *plugin.Override { | |
ColumnName: column, | ||
Table: &table, | ||
PythonType: pluginPythonType(o.PythonType), | ||
GoType: pluginGoType(o), | ||
} | ||
} | ||
|
||
|
@@ -56,6 +57,7 @@ func pluginSettings(cs config.CombinedSettings) *plugin.Settings { | |
Rename: cs.Rename, | ||
Python: pluginPythonCode(cs.Python), | ||
Kotlin: pluginKotlinCode(cs.Kotlin), | ||
Go: pluginGoCode(cs.Go), | ||
} | ||
} | ||
|
||
|
@@ -69,6 +71,42 @@ func pluginPythonCode(s config.SQLPython) *plugin.PythonCode { | |
} | ||
} | ||
|
||
func pluginGoCode(s config.SQLGo) *plugin.GoCode { | ||
return &plugin.GoCode{ | ||
EmitInterface: s.EmitInterface, | ||
EmitJsonTags: s.EmitJSONTags, | ||
EmitDbTags: s.EmitDBTags, | ||
EmitPreparedQueries: s.EmitPreparedQueries, | ||
EmitExactTableNames: s.EmitExactTableNames, | ||
EmitEmptySlices: s.EmitEmptySlices, | ||
EmitExportedQueries: s.EmitExportedQueries, | ||
EmitResultStructPointers: s.EmitResultStructPointers, | ||
EmitParamsStructPointers: s.EmitParamsStructPointers, | ||
EmitMethodsWithDbArgument: s.EmitMethodsWithDBArgument, | ||
JsonTagsCaseStyle: s.JSONTagsCaseStyle, | ||
Package: s.Package, | ||
Out: s.Out, | ||
SqlPackage: s.SQLPackage, | ||
OutputDbFileName: s.OutputDBFileName, | ||
OutputModelsFileName: s.OutputModelsFileName, | ||
OutputQuerierFileName: s.OutputQuerierFileName, | ||
OutputFilesSuffix: s.OutputFilesSuffix, | ||
} | ||
} | ||
|
||
func pluginGoType(o config.Override) *plugin.ParsedGoType { | ||
// Note that there is a slight mismatch between this and the | ||
// proto api. The GoType on the override is the unparsed type, | ||
// which could be a qualified path or an object, as per | ||
// https://docs.sqlc.dev/en/latest/reference/config.html#renaming-struct-fields | ||
kyleconroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return &plugin.ParsedGoType{ | ||
ImportPath: o.GoImportPath, | ||
Package: o.GoPackage, | ||
TypeName: o.GoTypeName, | ||
BasicType: o.GoBasicType, | ||
} | ||
} | ||
|
||
func pluginPythonType(pt config.PythonType) *plugin.PythonType { | ||
return &plugin.PythonType{ | ||
Module: pt.Module, | ||
|
@@ -88,16 +126,21 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog { | |
var schemas []*plugin.Schema | ||
for _, s := range c.Schemas { | ||
var enums []*plugin.Enum | ||
var cts []*plugin.CompositeType | ||
for _, typ := range s.Types { | ||
enum, ok := typ.(*catalog.Enum) | ||
if !ok { | ||
continue | ||
switch typ := typ.(type) { | ||
case *catalog.Enum: | ||
enums = append(enums, &plugin.Enum{ | ||
Name: typ.Name, | ||
Comment: typ.Comment, | ||
Vals: typ.Vals, | ||
}) | ||
case *catalog.CompositeType: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i assume the proto has There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you're correct that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah thanks, i missed the existing usage of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fine to keep this for now |
||
cts = append(cts, &plugin.CompositeType{ | ||
Name: typ.Name, | ||
Comment: typ.Comment, | ||
}) | ||
} | ||
enums = append(enums, &plugin.Enum{ | ||
Name: enum.Name, | ||
Comment: enum.Comment, | ||
Vals: enum.Vals, | ||
}) | ||
} | ||
var tables []*plugin.Table | ||
for _, t := range s.Tables { | ||
|
@@ -136,10 +179,11 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog { | |
}) | ||
} | ||
schemas = append(schemas, &plugin.Schema{ | ||
Comment: s.Comment, | ||
Name: s.Name, | ||
Tables: tables, | ||
Enums: enums, | ||
Comment: s.Comment, | ||
Name: s.Name, | ||
Tables: tables, | ||
Enums: enums, | ||
CompositeTypes: cts, | ||
}) | ||
} | ||
return &plugin.Catalog{ | ||
|
@@ -161,14 +205,23 @@ func pluginQueries(r *compiler.Result) []*plugin.Query { | |
for _, p := range q.Params { | ||
params = append(params, pluginQueryParam(p)) | ||
} | ||
var iit *plugin.Identifier | ||
if q.InsertIntoTable != nil { | ||
iit = &plugin.Identifier{ | ||
stephen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Catalog: q.InsertIntoTable.Catalog, | ||
Schema: q.InsertIntoTable.Schema, | ||
Name: q.InsertIntoTable.Name, | ||
} | ||
} | ||
out = append(out, &plugin.Query{ | ||
Name: q.Name, | ||
Cmd: q.Cmd, | ||
Text: q.SQL, | ||
Comments: q.Comments, | ||
Columns: columns, | ||
Params: params, | ||
Filename: q.Filename, | ||
Name: q.Name, | ||
Cmd: q.Cmd, | ||
Text: q.SQL, | ||
Comments: q.Comments, | ||
Columns: columns, | ||
Params: params, | ||
Filename: q.Filename, | ||
InsertIntoTable: iit, | ||
}) | ||
} | ||
return out | ||
|
@@ -180,11 +233,13 @@ func pluginQueryColumn(c *compiler.Column) *plugin.Column { | |
l = *c.Length | ||
} | ||
out := &plugin.Column{ | ||
Name: c.Name, | ||
Comment: c.Comment, | ||
NotNull: c.NotNull, | ||
IsArray: c.IsArray, | ||
Length: int32(l), | ||
Name: c.Name, | ||
Comment: c.Comment, | ||
NotNull: c.NotNull, | ||
IsArray: c.IsArray, | ||
Length: int32(l), | ||
IsNamedParam: c.IsNamedParam, | ||
IsFuncCall: c.IsFuncCall, | ||
} | ||
|
||
if c.Type != nil { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
package golang | ||
|
||
import ( | ||
"github.com/kyleconroy/sqlc/internal/core" | ||
"github.com/kyleconroy/sqlc/internal/sql/ast" | ||
"github.com/kyleconroy/sqlc/internal/plugin" | ||
) | ||
|
||
func sameTableName(n *ast.TableName, f core.FQN, defaultSchema string) bool { | ||
if n == nil { | ||
func sameTableName(tableID, f *plugin.Identifier, defaultSchema string) bool { | ||
if tableID == nil { | ||
return false | ||
} | ||
schema := n.Schema | ||
if n.Schema == "" { | ||
schema := tableID.Schema | ||
if tableID.Schema == "" { | ||
schema = defaultSchema | ||
} | ||
return n.Catalog == f.Catalog && schema == f.Schema && n.Name == f.Rel | ||
return tableID.Catalog == f.Catalog && schema == f.Schema && tableID.Name == f.Name | ||
} |
Uh oh!
There was an error while loading. Please reload this page.