Skip to content

Commit 0201d70

Browse files
fix(codegen/golang): handle go_struct_tag for db_type overrides (#4055)
* fix(codegen/golang): handle `go_struct_tag` for `db_type` overrides Resolves #3143 * update tests * properly handle nullable and other db_type match parameters * and update tests
1 parent 419c2a2 commit 0201d70

File tree

17 files changed

+112
-54
lines changed

17 files changed

+112
-54
lines changed

internal/codegen/golang/go_type.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ func addExtraGoStructTags(tags map[string]string, req *plugin.GenerateRequest, o
1414
if oride.GoType.StructTags == nil {
1515
continue
1616
}
17+
if override.MatchesColumn(col) {
18+
for k, v := range oride.GoType.StructTags {
19+
tags[k] = v
20+
}
21+
continue
22+
}
1723
if !override.Matches(col.Table, req.Catalog.DefaultSchema) {
1824
// Different table.
1925
continue
@@ -64,16 +70,13 @@ func goType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Colu
6470
}
6571

6672
func goInnerType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
67-
columnType := sdk.DataType(col.Type)
68-
notNull := col.NotNull || col.IsArray
69-
7073
// package overrides have a higher precedence
7174
for _, override := range options.Overrides {
7275
oride := override.ShimOverride
7376
if oride.GoType.TypeName == "" {
7477
continue
7578
}
76-
if oride.DbType != "" && oride.DbType == columnType && oride.Nullable != notNull && oride.Unsigned == col.Unsigned {
79+
if override.MatchesColumn(col) {
7780
return oride.GoType.TypeName
7881
}
7982
}

internal/codegen/golang/opts/override.go

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

8+
"github.com/sqlc-dev/sqlc/internal/codegen/sdk"
89
"github.com/sqlc-dev/sqlc/internal/pattern"
910
"github.com/sqlc-dev/sqlc/internal/plugin"
1011
)
@@ -76,6 +77,12 @@ func (o *Override) Matches(n *plugin.Identifier, defaultSchema string) bool {
7677
return true
7778
}
7879

80+
func (o *Override) MatchesColumn(col *plugin.Column) bool {
81+
columnType := sdk.DataType(col.Type)
82+
notNull := col.NotNull || col.IsArray
83+
return o.DBType != "" && o.DBType == columnType && o.Nullable != notNull && o.Unsigned == col.Unsigned
84+
}
85+
7986
func (o *Override) parse(req *plugin.GenerateRequest) (err error) {
8087
// validate deprecated postgres_type field
8188
if o.Deprecated_PostgresType != "" {

internal/endtoend/testdata/overrides_go_struct_tags/mysql/go/models.go

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/overrides_go_struct_tags/mysql/schema.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CREATE TABLE foo (
22
other text NOT NULL,
3-
tagged text NOT NULL
3+
tagged text NOT NULL,
4+
nulltext text
45
);
56

67
CREATE TABLE bar (

internal/endtoend/testdata/overrides_go_struct_tags/mysql/sqlc.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515
{
1616
"go_struct_tag": "also:\"tagged\"",
1717
"column": "*.also_tagged"
18+
},
19+
{
20+
"db_type": "text",
21+
"go_struct_tag": "utype:\"notnull_text\""
22+
},
23+
{
24+
"db_type": "text",
25+
"go_type": "string",
26+
"nullable": true,
27+
"go_struct_tag": "utype:\"nullable_text\""
1828
}
1929
]
2030
}

internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v4/go/models.go

Lines changed: 9 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v4/schema.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ CREATE TABLE foo (
22
id text,
33
other_id text,
44
about text,
5-
other text
5+
other text,
6+
notnulltext text not null
67
);
78

89
CREATE TABLE bar (

internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v4/sqlc.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@
3232
{
3333
"column": "foo.other",
3434
"go_struct_tag": "type:\"this\""
35+
},
36+
{
37+
"db_type": "text",
38+
"go_struct_tag": "utype:\"notnull_text\""
39+
},
40+
{
41+
"db_type": "text",
42+
"go_type": "string",
43+
"nullable": true,
44+
"go_struct_tag": "utype:\"nullable_text\""
3545
}
3646
]
3747
}

internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v5/go/models.go

Lines changed: 9 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v5/schema.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ CREATE TABLE foo (
22
id text,
33
other_id text,
44
about text,
5-
other text
5+
other text,
6+
notnulltext text not null
67
);
78

89
CREATE TABLE bar (

0 commit comments

Comments
 (0)