Skip to content

Commit d7d59c3

Browse files
EliSauderCopilot
andcommitted
fix(golang): give a domain column the struct tags of its base's override
An override on a domain's base type supplied the column's Go type, but its go_struct_tag was matched only against the domain's own name and so was dropped. Walk the domain to its base for tags the way goInnerType does for the type, stopping where an override names the type, and apply the domain's and column's own tags over the base's. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c7f4217 commit d7d59c3

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎internal/codegen/golang/go_type.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010
)
1111

1212
func addExtraGoStructTags(tags map[string]string, req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) {
13+
// A domain generated as its base takes the base's tags, under its own.
14+
if base := domainColumn(req, col); base != nil && !typeOverridden(options, col) {
15+
addExtraGoStructTags(tags, req, options, base)
16+
}
1317
for _, override := range options.Overrides {
1418
oride := override.ShimOverride
1519
if oride.GoType.StructTags == nil {
@@ -36,6 +40,17 @@ func addExtraGoStructTags(tags map[string]string, req *plugin.GenerateRequest, o
3640
}
3741
}
3842

43+
// typeOverridden reports whether an override names col's type, which
44+
// goInnerType then generates it as.
45+
func typeOverridden(options *opts.Options, col *plugin.Column) bool {
46+
for _, override := range options.Overrides {
47+
if override.ShimOverride.GoType.TypeName != "" && override.MatchesColumn(col) {
48+
return true
49+
}
50+
}
51+
return false
52+
}
53+
3954
// goType is the Go type of a column's value: what a query's row scans
4055
// into, or what a table's model holds.
4156
func goType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {

‎internal/endtoend/testdata/domain_types/postgresql/go/models.go‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/domain_types/postgresql/sqlc.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ sql:
1414
nullable: true
1515
- db_type: "pg_catalog.numeric"
1616
go_type: "github.com/shopspring/decimal.Decimal"
17+
go_struct_tag: 'money:"true"'

0 commit comments

Comments
 (0)