diff --git a/internal/codegen/golang/go_type.go b/internal/codegen/golang/go_type.go index 0d72621efe..c4aac84dd6 100644 --- a/internal/codegen/golang/go_type.go +++ b/internal/codegen/golang/go_type.go @@ -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 @@ -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 } } diff --git a/internal/codegen/golang/opts/override.go b/internal/codegen/golang/opts/override.go index 4a1b6f2903..6916c0c7f3 100644 --- a/internal/codegen/golang/opts/override.go +++ b/internal/codegen/golang/opts/override.go @@ -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" ) @@ -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 != "" { diff --git a/internal/endtoend/testdata/overrides_go_struct_tags/mysql/go/models.go b/internal/endtoend/testdata/overrides_go_struct_tags/mysql/go/models.go index 5cb2b2ffda..cd05bed063 100644 --- a/internal/endtoend/testdata/overrides_go_struct_tags/mysql/go/models.go +++ b/internal/endtoend/testdata/overrides_go_struct_tags/mysql/go/models.go @@ -5,16 +5,17 @@ package override type Bar struct { - Other string - AlsoTagged string `also:"tagged"` + Other string `utype:"notnull_text"` + AlsoTagged string `also:"tagged" utype:"notnull_text"` } type Baz struct { - Other string - AlsoTagged string `also:"tagged"` + Other string `utype:"notnull_text"` + AlsoTagged string `also:"tagged" utype:"notnull_text"` } type Foo struct { - Other string - Tagged string `a:"b" x:"y,z"` + Other string `utype:"notnull_text"` + Tagged string `a:"b" utype:"notnull_text" x:"y,z"` + Nulltext string `utype:"nullable_text"` } diff --git a/internal/endtoend/testdata/overrides_go_struct_tags/mysql/schema.sql b/internal/endtoend/testdata/overrides_go_struct_tags/mysql/schema.sql index 4d5233cc37..5c9ce85545 100644 --- a/internal/endtoend/testdata/overrides_go_struct_tags/mysql/schema.sql +++ b/internal/endtoend/testdata/overrides_go_struct_tags/mysql/schema.sql @@ -1,6 +1,7 @@ CREATE TABLE foo ( other text NOT NULL, - tagged text NOT NULL + tagged text NOT NULL, + nulltext text ); CREATE TABLE bar ( diff --git a/internal/endtoend/testdata/overrides_go_struct_tags/mysql/sqlc.json b/internal/endtoend/testdata/overrides_go_struct_tags/mysql/sqlc.json index 7d92e65e69..697fe054ea 100644 --- a/internal/endtoend/testdata/overrides_go_struct_tags/mysql/sqlc.json +++ b/internal/endtoend/testdata/overrides_go_struct_tags/mysql/sqlc.json @@ -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\"" } ] } diff --git a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v4/go/models.go b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v4/go/models.go index 76b9f6a98b..6b094dc77b 100644 --- a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v4/go/models.go +++ b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v4/go/models.go @@ -4,20 +4,17 @@ package override -import ( - "database/sql" -) - type Bar struct { - ID sql.NullString `type:"id"` - OtherID sql.NullString `type:"other_id"` - About sql.NullString - Other sql.NullString `type:"other"` + ID string `type:"id" utype:"nullable_text"` + OtherID string `type:"other_id" utype:"nullable_text"` + About string `utype:"nullable_text"` + Other string `type:"other" utype:"nullable_text"` } type Foo struct { - ID sql.NullString `source:"foo" type:"id"` - OtherID sql.NullString `type:"other_id"` - About sql.NullString `type:"about"` - Other sql.NullString `type:"this"` + ID string `source:"foo" type:"id" utype:"nullable_text"` + OtherID string `type:"other_id" utype:"nullable_text"` + About string `type:"about" utype:"nullable_text"` + Other string `type:"this" utype:"nullable_text"` + Notnulltext string `utype:"notnull_text"` } diff --git a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v4/schema.sql b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v4/schema.sql index 53739ddbb1..30b718298d 100644 --- a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v4/schema.sql +++ b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v4/schema.sql @@ -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 ( diff --git a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v4/sqlc.json b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v4/sqlc.json index a01427c202..bc583cdad4 100644 --- a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v4/sqlc.json +++ b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v4/sqlc.json @@ -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\"" } ] } diff --git a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v5/go/models.go b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v5/go/models.go index 2f13a354c5..6b094dc77b 100644 --- a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v5/go/models.go +++ b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v5/go/models.go @@ -4,20 +4,17 @@ package override -import ( - "github.com/jackc/pgx/v5/pgtype" -) - type Bar struct { - ID pgtype.Text `type:"id"` - OtherID pgtype.Text `type:"other_id"` - About pgtype.Text - Other pgtype.Text `type:"other"` + ID string `type:"id" utype:"nullable_text"` + OtherID string `type:"other_id" utype:"nullable_text"` + About string `utype:"nullable_text"` + Other string `type:"other" utype:"nullable_text"` } type Foo struct { - ID pgtype.Text `source:"foo" type:"id"` - OtherID pgtype.Text `type:"other_id"` - About pgtype.Text `type:"about"` - Other pgtype.Text `type:"this"` + ID string `source:"foo" type:"id" utype:"nullable_text"` + OtherID string `type:"other_id" utype:"nullable_text"` + About string `type:"about" utype:"nullable_text"` + Other string `type:"this" utype:"nullable_text"` + Notnulltext string `utype:"notnull_text"` } diff --git a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v5/schema.sql b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v5/schema.sql index 53739ddbb1..30b718298d 100644 --- a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v5/schema.sql +++ b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v5/schema.sql @@ -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 ( diff --git a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v5/sqlc.json b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v5/sqlc.json index da86badcc7..df8c9de3b0 100644 --- a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v5/sqlc.json +++ b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/pgx/v5/sqlc.json @@ -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\"" } ] } diff --git a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/stdlib/go/models.go b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/stdlib/go/models.go index 4d4b68eba2..5e47d85126 100644 --- a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/stdlib/go/models.go +++ b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/stdlib/go/models.go @@ -4,11 +4,8 @@ package override -import ( - "database/sql" -) - type Foo struct { - ID sql.NullString `x:"y"` - OtherID sql.NullString + ID string `utype:"nullable_text" x:"y"` + OtherID string `utype:"nullable_text"` + Notnulltext string `utype:"notnull_text"` } diff --git a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/stdlib/schema.sql b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/stdlib/schema.sql index f860a58030..82698ec836 100644 --- a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/stdlib/schema.sql +++ b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/stdlib/schema.sql @@ -1,4 +1,5 @@ CREATE TABLE foo ( id text, - other_id text + other_id text, + notnulltext text not null ); diff --git a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/stdlib/sqlc.json b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/stdlib/sqlc.json index 2c6f9e8c7c..f1171fcc11 100644 --- a/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/stdlib/sqlc.json +++ b/internal/endtoend/testdata/overrides_go_struct_tags/postgresql/stdlib/sqlc.json @@ -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\"" } ] } diff --git a/internal/endtoend/testdata/overrides_go_struct_tags/sqlite/go/models.go b/internal/endtoend/testdata/overrides_go_struct_tags/sqlite/go/models.go index 897282f3be..99f59d608d 100644 --- a/internal/endtoend/testdata/overrides_go_struct_tags/sqlite/go/models.go +++ b/internal/endtoend/testdata/overrides_go_struct_tags/sqlite/go/models.go @@ -5,19 +5,20 @@ package override type Bar struct { - Other string - AlsoTagged string `also:"tagged"` - Tag3 string `tag_with_space:" it's legal!"` + Other string `utype:"notnull_text"` + AlsoTagged string `also:"tagged" utype:"notnull_text"` + Tag3 string `tag_with_space:" it's legal!" utype:"notnull_text"` } type Baz struct { - Other string - AlsoTagged string `also:"tagged"` - Tag3 string `tag_with_space:" it's legal!"` + Other string `utype:"notnull_text"` + AlsoTagged string `also:"tagged" utype:"notnull_text"` + Tag3 string `tag_with_space:" it's legal!" utype:"notnull_text"` } type Foo struct { - Other string - Tagged string `a:"b" x:"y,z"` - Tag3 string `tag_with_space:" it's legal!"` + Other string `utype:"notnull_text"` + Tagged string `a:"b" utype:"notnull_text" x:"y,z"` + Tag3 string `tag_with_space:" it's legal!" utype:"notnull_text"` + Nulltext string `utype:"nullable_text"` } diff --git a/internal/endtoend/testdata/overrides_go_struct_tags/sqlite/schema.sql b/internal/endtoend/testdata/overrides_go_struct_tags/sqlite/schema.sql index f908060db1..e9aa9bd37a 100644 --- a/internal/endtoend/testdata/overrides_go_struct_tags/sqlite/schema.sql +++ b/internal/endtoend/testdata/overrides_go_struct_tags/sqlite/schema.sql @@ -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 ( diff --git a/internal/endtoend/testdata/overrides_go_struct_tags/sqlite/sqlc.json b/internal/endtoend/testdata/overrides_go_struct_tags/sqlite/sqlc.json index a4d54cc3df..a4ca853b6a 100644 --- a/internal/endtoend/testdata/overrides_go_struct_tags/sqlite/sqlc.json +++ b/internal/endtoend/testdata/overrides_go_struct_tags/sqlite/sqlc.json @@ -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\"" } ] }