Skip to content

fix(codegen/golang): handle go_struct_tag for db_type overrides #4055

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

Merged
merged 4 commits into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions internal/codegen/golang/go_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ func addExtraGoStructTags(tags map[string]string, req *plugin.GenerateRequest, o
if oride.GoType.StructTags == nil {
continue
}
if override.MatchesColumn(col) {
for k, v := range oride.GoType.StructTags {
tags[k] = v
}
continue
}
if !override.Matches(col.Table, req.Catalog.DefaultSchema) {
// Different table.
continue
Expand Down Expand Up @@ -64,16 +70,13 @@ func goType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Colu
}

func goInnerType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
columnType := sdk.DataType(col.Type)
notNull := col.NotNull || col.IsArray

// package overrides have a higher precedence
for _, override := range options.Overrides {
oride := override.ShimOverride
if oride.GoType.TypeName == "" {
continue
}
if oride.DbType != "" && oride.DbType == columnType && oride.Nullable != notNull && oride.Unsigned == col.Unsigned {
if override.MatchesColumn(col) {
return oride.GoType.TypeName
}
}
Expand Down
7 changes: 7 additions & 0 deletions internal/codegen/golang/opts/override.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"

"github.com/sqlc-dev/sqlc/internal/codegen/sdk"
"github.com/sqlc-dev/sqlc/internal/pattern"
"github.com/sqlc-dev/sqlc/internal/plugin"
)
Expand Down Expand Up @@ -76,6 +77,12 @@ func (o *Override) Matches(n *plugin.Identifier, defaultSchema string) bool {
return true
}

func (o *Override) MatchesColumn(col *plugin.Column) bool {
columnType := sdk.DataType(col.Type)
notNull := col.NotNull || col.IsArray
return o.DBType != "" && o.DBType == columnType && o.Nullable != notNull && o.Unsigned == col.Unsigned
}

func (o *Override) parse(req *plugin.GenerateRequest) (err error) {
// validate deprecated postgres_type field
if o.Deprecated_PostgresType != "" {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CREATE TABLE foo (
other text NOT NULL,
tagged text NOT NULL
tagged text NOT NULL,
nulltext text
);

CREATE TABLE bar (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
{
"go_struct_tag": "also:\"tagged\"",
"column": "*.also_tagged"
},
{
"db_type": "text",
"go_struct_tag": "utype:\"notnull_text\""
},
{
"db_type": "text",
"go_type": "string",
"nullable": true,
"go_struct_tag": "utype:\"nullable_text\""
}
]
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ CREATE TABLE foo (
id text,
other_id text,
about text,
other text
other text,
notnulltext text not null
);

CREATE TABLE bar (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
{
"column": "foo.other",
"go_struct_tag": "type:\"this\""
},
{
"db_type": "text",
"go_struct_tag": "utype:\"notnull_text\""
},
{
"db_type": "text",
"go_type": "string",
"nullable": true,
"go_struct_tag": "utype:\"nullable_text\""
}
]
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ CREATE TABLE foo (
id text,
other_id text,
about text,
other text
other text,
notnulltext text not null
);

CREATE TABLE bar (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
{
"column": "foo.other",
"go_struct_tag": "type:\"this\""
},
{
"db_type": "text",
"go_struct_tag": "utype:\"notnull_text\""
},
{
"db_type": "text",
"go_type": "string",
"nullable": true,
"go_struct_tag": "utype:\"nullable_text\""
}
]
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
CREATE TABLE foo (
id text,
other_id text
other_id text,
notnulltext text not null
);
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
{
"column": "foo.id",
"go_struct_tag": "x:\"y\""
},
{
"db_type": "text",
"go_struct_tag": "utype:\"notnull_text\""
},
{
"db_type": "text",
"go_type": "string",
"nullable": true,
"go_struct_tag": "utype:\"nullable_text\""
}
]
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
CREATE TABLE foo (
other text NOT NULL,
tagged text NOT NULL,
tag3 text NOT NULL
tag3 text NOT NULL,
nulltext text
);

CREATE TABLE bar (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
{
"go_struct_tag": "tag_with_space:\" it's legal!\"",
"column": "*.tag3"
},
{
"db_type": "text",
"go_struct_tag": "utype:\"notnull_text\""
},
{
"db_type": "text",
"go_type": "string",
"nullable": true,
"go_struct_tag": "utype:\"nullable_text\""
}
]
}
Expand Down
Loading