Skip to content

Commit 84fe9c1

Browse files
author
Zephaniah E. Loss-Cutler-Hull
committed
Add support for globs in overrides.
This lets us say things like '*.*_ksuid' should be of type 'github.com/segmentio/ksuid.KSUID', instead of having to list every single column in every single table. We can even say '*.*.*_ksuid' for every column, in every table, in every schema. Now, ideally we would do a type replace on 'binary(20)', however that gets turned into a char for at least the mysql code, which means that we never get to see 'binary(20)', and so can't do a replacement on it. This makes use of the github.com/gobwas/glob package for the globbing, and it pretty much Just Works the way you expect it to.
1 parent df30bbf commit 84fe9c1

File tree

8 files changed

+26
-15
lines changed

8 files changed

+26
-15
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/antlr/antlr4 v0.0.0-20200209180723-1177c0b58d07
77
github.com/davecgh/go-spew v1.1.1
88
github.com/go-sql-driver/mysql v1.5.0
9+
github.com/gobwas/glob v0.2.3
910
github.com/google/go-cmp v0.5.4
1011
github.com/jackc/pgx/v4 v4.10.1
1112
github.com/jinzhu/inflection v1.0.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V
5959
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
6060
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
6161
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
62+
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
63+
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
6264
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
6365
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
6466
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=

internal/codegen/golang/compat.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ func sameTableName(n *ast.TableName, f core.FQN, defaultSchema string) bool {
1313
if n.Schema == "" {
1414
schema = defaultSchema
1515
}
16-
return n.Catalog == f.Catalog && schema == f.Schema && n.Name == f.Rel
16+
return n.Catalog == f.Catalog && ((f.Schema != nil && f.Schema.Match(schema)) || schema == "") && ((f.Rel != nil && f.Rel.Match(n.Name)) || n.Name == "")
1717
}

internal/codegen/golang/go_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ func goType(r *compiler.Result, col *compiler.Column, settings config.CombinedSe
99
// package overrides have a higher precedence
1010
for _, oride := range settings.Overrides {
1111
sameTable := sameTableName(col.Table, oride.Table, r.Catalog.DefaultSchema)
12-
if oride.Column != "" && oride.ColumnName == col.Name && sameTable {
12+
if oride.Column != "" && oride.ColumnName.Match(col.Name) && sameTable {
1313
return oride.GoTypeName
1414
}
1515
}

internal/codegen/golang/result.go

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

8+
"github.com/gobwas/glob"
9+
810
"github.com/kyleconroy/sqlc/internal/codegen"
911
"github.com/kyleconroy/sqlc/internal/compiler"
1012
"github.com/kyleconroy/sqlc/internal/config"
@@ -68,7 +70,7 @@ func buildStructs(r *compiler.Result, settings config.CombinedSettings) []Struct
6870
structName = inflection.Singular(structName)
6971
}
7072
s := Struct{
71-
Table: core.FQN{Schema: schema.Name, Rel: table.Rel.Name},
73+
Table: core.FQN{Schema: glob.MustCompile(schema.Name), Rel: glob.MustCompile(table.Rel.Name)},
7274
Name: StructName(structName, settings),
7375
Comment: table.Comment,
7476
}

internal/codegen/kotlin/gen.go

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

12+
"github.com/gobwas/glob"
13+
1214
"github.com/kyleconroy/sqlc/internal/codegen"
1315
"github.com/kyleconroy/sqlc/internal/compiler"
1416
"github.com/kyleconroy/sqlc/internal/config"
@@ -26,7 +28,7 @@ func sameTableName(n *ast.TableName, f core.FQN) bool {
2628
if n.Schema == "" {
2729
schema = "public"
2830
}
29-
return n.Catalog == n.Catalog && schema == f.Schema && n.Name == f.Rel
31+
return n.Catalog == f.Catalog && ((f.Schema != nil && f.Schema.Match(schema)) || schema == "") && ((f.Rel != nil && f.Rel.Match(n.Name)) || n.Name == "")
3032
}
3133

3234
var ktIdentPattern = regexp.MustCompile("[^a-zA-Z0-9_]+")
@@ -286,7 +288,7 @@ func buildDataClasses(r *compiler.Result, settings config.CombinedSettings) []St
286288
structName = inflection.Singular(structName)
287289
}
288290
s := Struct{
289-
Table: core.FQN{Schema: schema.Name, Rel: table.Rel.Name},
291+
Table: core.FQN{Schema: glob.MustCompile(schema.Name), Rel: glob.MustCompile(table.Rel.Name)},
290292
Name: structName,
291293
Comment: table.Comment,
292294
}

internal/config/config.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111

1212
yaml "gopkg.in/yaml.v3"
1313

14+
"github.com/gobwas/glob"
15+
1416
"github.com/kyleconroy/sqlc/internal/core"
1517
)
1618

@@ -146,7 +148,7 @@ type Override struct {
146148
// fully qualified name of the column, e.g. `accounts.id`
147149
Column string `json:"column" yaml:"column"`
148150

149-
ColumnName string
151+
ColumnName glob.Glob
150152
Table core.FQN
151153
GoImportPath string
152154
GoPackage string
@@ -184,16 +186,16 @@ func (o *Override) Parse() error {
184186
colParts := strings.Split(o.Column, ".")
185187
switch len(colParts) {
186188
case 2:
187-
o.ColumnName = colParts[1]
188-
o.Table = core.FQN{Schema: "public", Rel: colParts[0]}
189+
o.ColumnName = glob.MustCompile(colParts[1])
190+
o.Table = core.FQN{Schema: glob.MustCompile("public"), Rel: glob.MustCompile(colParts[0])}
189191
case 3:
190-
o.ColumnName = colParts[2]
191-
o.Table = core.FQN{Schema: colParts[0], Rel: colParts[1]}
192+
o.ColumnName = glob.MustCompile(colParts[2])
193+
o.Table = core.FQN{Schema: glob.MustCompile(colParts[0]), Rel: glob.MustCompile(colParts[1])}
192194
case 4:
193-
o.ColumnName = colParts[3]
194-
o.Table = core.FQN{Catalog: colParts[0], Schema: colParts[1], Rel: colParts[2]}
195+
o.ColumnName = glob.MustCompile(colParts[3])
196+
o.Table = core.FQN{Catalog: colParts[0], Schema: glob.MustCompile(colParts[1]), Rel: glob.MustCompile(colParts[2])}
195197
default:
196-
return fmt.Errorf("Override `column` specifier %q is not the proper format, expected '[catalog.][schema.]colname.tablename'", o.Column)
198+
return fmt.Errorf("Override `column` specifier %q is not the proper format, expected '[catalog.][schema.]tablename.colname'", o.Column)
197199
}
198200
}
199201

internal/core/fqn.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package core
22

3+
import "github.com/gobwas/glob"
4+
35
// TODO: This is the last struct left over from the old architecture. Figure
46
// out how to remove it at some point
57
type FQN struct {
68
Catalog string
7-
Schema string
8-
Rel string
9+
Schema glob.Glob
10+
Rel glob.Glob
911
}
1012

1113
func (f FQN) String() string {

0 commit comments

Comments
 (0)