Skip to content

Commit fc7c862

Browse files
authored
feat(sdk): Add the plugin SDK package (#1463)
Collect a few commonly used functions and put them into one package. Move the utils.go file from the codegen package here as well.
1 parent aade087 commit fc7c862

File tree

15 files changed

+126
-183
lines changed

15 files changed

+126
-183
lines changed

internal/codegen/golang/compat.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

internal/codegen/golang/gen.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"strings"
1010
"text/template"
1111

12-
"github.com/kyleconroy/sqlc/internal/codegen"
12+
"github.com/kyleconroy/sqlc/internal/codegen/sdk"
1313
"github.com/kyleconroy/sqlc/internal/metadata"
1414
"github.com/kyleconroy/sqlc/internal/plugin"
1515
)
@@ -58,9 +58,9 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
5858
}
5959

6060
funcMap := template.FuncMap{
61-
"lowerTitle": codegen.LowerTitle,
62-
"comment": codegen.DoubleSlashComment,
63-
"escape": codegen.EscapeBacktick,
61+
"lowerTitle": sdk.LowerTitle,
62+
"comment": sdk.DoubleSlashComment,
63+
"escape": sdk.EscapeBacktick,
6464
"imports": i.Imports,
6565
"hasPrefix": strings.HasPrefix,
6666
}

internal/codegen/golang/go_type.go

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,18 @@
11
package golang
22

33
import (
4-
"github.com/kyleconroy/sqlc/internal/pattern"
4+
"github.com/kyleconroy/sqlc/internal/codegen/sdk"
55
"github.com/kyleconroy/sqlc/internal/plugin"
66
)
77

8-
// XXX: These are copied from python codegen.
9-
func matchString(pat, target string) bool {
10-
matcher, err := pattern.MatchCompile(pat)
11-
if err != nil {
12-
panic(err)
13-
}
14-
return matcher.MatchString(target)
15-
}
16-
17-
func matches(o *plugin.Override, n *plugin.Identifier, defaultSchema string) bool {
18-
if n == nil {
19-
return false
20-
}
21-
22-
schema := n.Schema
23-
if n.Schema == "" {
24-
schema = defaultSchema
25-
}
26-
27-
if o.Table.Catalog != "" && !matchString(o.Table.Catalog, n.Catalog) {
28-
return false
29-
}
30-
31-
if o.Table.Schema == "" && schema != "" {
32-
return false
33-
}
34-
35-
if o.Table.Schema != "" && !matchString(o.Table.Schema, schema) {
36-
return false
37-
}
38-
39-
if o.Table.Name == "" && n.Name != "" {
40-
return false
41-
}
42-
43-
if o.Table.Name != "" && !matchString(o.Table.Name, n.Name) {
44-
return false
45-
}
46-
47-
return true
48-
}
49-
508
func goType(req *plugin.CodeGenRequest, col *plugin.Column) string {
519
// Check if the column's type has been overridden
5210
for _, oride := range req.Settings.Overrides {
5311
if oride.GoType.TypeName == "" {
5412
continue
5513
}
56-
sameTable := matches(oride, col.Table, req.Catalog.DefaultSchema)
57-
if oride.Column != "" && matchString(oride.ColumnName, col.Name) && sameTable {
14+
sameTable := sdk.Matches(oride, col.Table, req.Catalog.DefaultSchema)
15+
if oride.Column != "" && sdk.MatchString(oride.ColumnName, col.Name) && sameTable {
5816
return oride.GoType.TypeName
5917
}
6018
}
@@ -66,7 +24,7 @@ func goType(req *plugin.CodeGenRequest, col *plugin.Column) string {
6624
}
6725

6826
func goInnerType(req *plugin.CodeGenRequest, col *plugin.Column) string {
69-
columnType := dataType(col.Type)
27+
columnType := sdk.DataType(col.Type)
7028
notNull := col.NotNull || col.IsArray
7129

7230
// package overrides have a higher precedence

internal/codegen/golang/mysql_type.go

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

6+
"github.com/kyleconroy/sqlc/internal/codegen/sdk"
67
"github.com/kyleconroy/sqlc/internal/debug"
78
"github.com/kyleconroy/sqlc/internal/plugin"
89
)
910

10-
func dataType(n *plugin.Identifier) string {
11-
if n.Schema != "" {
12-
return n.Schema + "." + n.Name
13-
} else {
14-
return n.Name
15-
}
16-
}
17-
1811
func mysqlType(req *plugin.CodeGenRequest, col *plugin.Column) string {
19-
columnType := dataType(col.Type)
12+
columnType := sdk.DataType(col.Type)
2013
notNull := col.NotNull || col.IsArray
2114

2215
switch columnType {

internal/codegen/golang/postgresql_type.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
package golang
22

33
import (
4+
"fmt"
45
"log"
6+
"strings"
57

6-
"github.com/kyleconroy/sqlc/internal/compiler"
8+
"github.com/kyleconroy/sqlc/internal/codegen/sdk"
79
"github.com/kyleconroy/sqlc/internal/debug"
810
"github.com/kyleconroy/sqlc/internal/plugin"
911
)
1012

13+
func parseIdentifierString(name string) (*plugin.Identifier, error) {
14+
parts := strings.Split(name, ".")
15+
switch len(parts) {
16+
case 1:
17+
return &plugin.Identifier{
18+
Name: parts[0],
19+
}, nil
20+
case 2:
21+
return &plugin.Identifier{
22+
Schema: parts[0],
23+
Name: parts[1],
24+
}, nil
25+
case 3:
26+
return &plugin.Identifier{
27+
Catalog: parts[0],
28+
Schema: parts[1],
29+
Name: parts[2],
30+
}, nil
31+
default:
32+
return nil, fmt.Errorf("invalid name: %s", name)
33+
}
34+
}
35+
1136
func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
12-
columnType := dataType(col.Type)
37+
columnType := sdk.DataType(col.Type)
1338
notNull := col.NotNull || col.IsArray
1439
driver := parseDriver(req.Settings)
1540

@@ -239,7 +264,7 @@ func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
239264
return "interface{}"
240265

241266
default:
242-
rel, err := compiler.ParseRelationString(columnType)
267+
rel, err := parseIdentifierString(columnType)
243268
if err != nil {
244269
// TODO: Should this actually return an error here?
245270
return "interface{}"

internal/codegen/golang/result.go

Lines changed: 5 additions & 5 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/codegen"
8+
"github.com/kyleconroy/sqlc/internal/codegen/sdk"
99
"github.com/kyleconroy/sqlc/internal/inflection"
1010
"github.com/kyleconroy/sqlc/internal/plugin"
1111
)
@@ -140,15 +140,15 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
140140

141141
var constantName string
142142
if req.Settings.Go.EmitExportedQueries {
143-
constantName = codegen.Title(query.Name)
143+
constantName = sdk.Title(query.Name)
144144
} else {
145-
constantName = codegen.LowerTitle(query.Name)
145+
constantName = sdk.LowerTitle(query.Name)
146146
}
147147

148148
gq := Query{
149149
Cmd: query.Cmd,
150150
ConstantName: constantName,
151-
FieldName: codegen.LowerTitle(query.Name) + "Stmt",
151+
FieldName: sdk.LowerTitle(query.Name) + "Stmt",
152152
MethodName: query.Name,
153153
SourceName: query.Filename,
154154
SQL: query.Text,
@@ -209,7 +209,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
209209
c := query.Columns[i]
210210
sameName := f.Name == StructName(columnName(c, i), req.Settings)
211211
sameType := f.Type == goType(req, c)
212-
sameTable := sameTableName(c.Table, &s.Table, req.Catalog.DefaultSchema)
212+
sameTable := sdk.SameTableName(c.Table, &s.Table, req.Catalog.DefaultSchema)
213213
if !sameName || !sameType || !sameTable {
214214
same = false
215215
}

internal/codegen/golang/sqlite_type.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"log"
55
"strings"
66

7+
"github.com/kyleconroy/sqlc/internal/codegen/sdk"
78
"github.com/kyleconroy/sqlc/internal/plugin"
89
)
910

1011
func sqliteType(req *plugin.CodeGenRequest, col *plugin.Column) string {
11-
dt := strings.ToLower(dataType(col.Type))
12+
dt := strings.ToLower(sdk.DataType(col.Type))
1213
notNull := col.NotNull || col.IsArray
1314

1415
switch dt {

internal/codegen/kotlin/gen.go

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,12 @@ import (
1010
"strings"
1111
"text/template"
1212

13-
"github.com/kyleconroy/sqlc/internal/codegen"
13+
"github.com/kyleconroy/sqlc/internal/codegen/sdk"
1414
"github.com/kyleconroy/sqlc/internal/inflection"
1515
"github.com/kyleconroy/sqlc/internal/metadata"
1616
"github.com/kyleconroy/sqlc/internal/plugin"
1717
)
1818

19-
func sameTableName(n, f *plugin.Identifier) bool {
20-
if n == nil {
21-
return false
22-
}
23-
schema := n.Schema
24-
if n.Schema == "" {
25-
schema = "public"
26-
}
27-
return n.Catalog == n.Catalog && schema == f.Schema && n.Name == f.Name
28-
}
29-
3019
var ktIdentPattern = regexp.MustCompile("[^a-zA-Z0-9_]+")
3120

3221
type Constant struct {
@@ -269,7 +258,7 @@ func dataClassName(name string, settings *plugin.Settings) string {
269258
}
270259

271260
func memberName(name string, settings *plugin.Settings) string {
272-
return codegen.LowerTitle(dataClassName(name, settings))
261+
return sdk.LowerTitle(dataClassName(name, settings))
273262
}
274263

275264
func buildDataClasses(req *plugin.CodeGenRequest) []Struct {
@@ -365,7 +354,7 @@ func makeType(req *plugin.CodeGenRequest, col *plugin.Column) ktType {
365354
IsEnum: isEnum,
366355
IsArray: col.IsArray,
367356
IsNull: !col.NotNull,
368-
DataType: dataType(col.Type),
357+
DataType: sdk.DataType(col.Type),
369358
Engine: req.Settings.Engine,
370359
}
371360
}
@@ -468,9 +457,9 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
468457
gq := Query{
469458
Cmd: query.Cmd,
470459
ClassName: strings.Title(query.Name),
471-
ConstantName: codegen.LowerTitle(query.Name),
472-
FieldName: codegen.LowerTitle(query.Name) + "Stmt",
473-
MethodName: codegen.LowerTitle(query.Name),
460+
ConstantName: sdk.LowerTitle(query.Name),
461+
FieldName: sdk.LowerTitle(query.Name) + "Stmt",
462+
MethodName: sdk.LowerTitle(query.Name),
474463
SourceName: query.Filename,
475464
SQL: jdbcSQL(query.Text, req.Settings.Engine),
476465
Comments: query.Comments,
@@ -507,7 +496,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
507496
c := query.Columns[i]
508497
sameName := f.Name == memberName(ktColumnName(c, i), req.Settings)
509498
sameType := f.Type == makeType(req, c)
510-
sameTable := sameTableName(c.Table, &s.Table)
499+
sameTable := sdk.SameTableName(c.Table, &s.Table, req.Catalog.DefaultSchema)
511500

512501
if !sameName || !sameType || !sameTable {
513502
same = false
@@ -779,8 +768,8 @@ func Generate(req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {
779768
}
780769

781770
funcMap := template.FuncMap{
782-
"lowerTitle": codegen.LowerTitle,
783-
"comment": codegen.DoubleSlashComment,
771+
"lowerTitle": sdk.LowerTitle,
772+
"comment": sdk.DoubleSlashComment,
784773
"imports": i.Imports,
785774
"offset": Offset,
786775
}

internal/codegen/kotlin/mysql_type.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,13 @@ package kotlin
33
import (
44
"log"
55

6+
"github.com/kyleconroy/sqlc/internal/codegen/sdk"
67
"github.com/kyleconroy/sqlc/internal/debug"
78
"github.com/kyleconroy/sqlc/internal/plugin"
89
)
910

10-
func dataType(n *plugin.Identifier) string {
11-
if n.Schema != "" {
12-
return n.Schema + "." + n.Name
13-
} else {
14-
return n.Name
15-
}
16-
}
17-
1811
func mysqlType(req *plugin.CodeGenRequest, col *plugin.Column) (string, bool) {
19-
columnType := dataType(col.Type)
12+
columnType := sdk.DataType(col.Type)
2013

2114
switch columnType {
2215

internal/codegen/kotlin/postgresql_type.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package kotlin
33
import (
44
"log"
55

6+
"github.com/kyleconroy/sqlc/internal/codegen/sdk"
67
"github.com/kyleconroy/sqlc/internal/plugin"
78
)
89

910
func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) (string, bool) {
10-
columnType := dataType(col.Type)
11+
columnType := sdk.DataType(col.Type)
1112

1213
switch columnType {
1314
case "serial", "pg_catalog.serial4":

0 commit comments

Comments
 (0)