Skip to content

Commit 90dd838

Browse files
committed
properly handle nullable and other db_type match parameters
1 parent 9c42b32 commit 90dd838

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

internal/codegen/golang/go_type.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ func addExtraGoStructTags(tags map[string]string, req *plugin.GenerateRequest, o
1414
if oride.GoType.StructTags == nil {
1515
continue
1616
}
17-
if oride.DbType != "" {
18-
columnType := sdk.DataType(col.Type)
19-
if columnType != oride.DbType {
20-
continue
21-
}
17+
if override.MatchesColumn(col) {
2218
for k, v := range oride.GoType.StructTags {
2319
tags[k] = v
2420
}
@@ -74,16 +70,13 @@ func goType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Colu
7470
}
7571

7672
func goInnerType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
77-
columnType := sdk.DataType(col.Type)
78-
notNull := col.NotNull || col.IsArray
79-
8073
// package overrides have a higher precedence
8174
for _, override := range options.Overrides {
8275
oride := override.ShimOverride
8376
if oride.GoType.TypeName == "" {
8477
continue
8578
}
86-
if oride.DbType != "" && oride.DbType == columnType && oride.Nullable != notNull && oride.Unsigned == col.Unsigned {
79+
if override.MatchesColumn(col) {
8780
return oride.GoType.TypeName
8881
}
8982
}

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 != "" {

0 commit comments

Comments
 (0)