Skip to content

Commit 7f1c349

Browse files
EliSauderCopilot
andcommitted
fix(golang): generate a domain over an array as an array
The flat type kept only the base's name, so a domain over text[] lost its element and dimensions and generated any. Add the base's dimensions to the column's own, and let the domain's base decide how many slices the Go type has, since the column alone does not say. Addresses review comment r4556992433 on #2. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f73d6a2 commit 7f1c349

8 files changed

Lines changed: 40 additions & 13 deletions

File tree

‎internal/codegen/golang/domain_type.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ func domainColumn(req *plugin.GenerateRequest, col *plugin.Column) *plugin.Colum
2828
if baseless(req, d) {
2929
return nil
3030
}
31+
// A domain over an array adds its dimensions to the column's own.
3132
out := proto.Clone(col).(*plugin.Column)
32-
out.Type = &plugin.Identifier{Name: d.Base.Name}
33+
name, dims := flatType(d.Base)
34+
out.Type = &plugin.Identifier{Name: name}
35+
if dims > 0 {
36+
out.IsArray, out.ArrayDims = true, out.ArrayDims+dims
37+
}
3338
return out
3439
}
3540

‎internal/codegen/golang/go_type.go‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,14 @@ func goInnerType(req *plugin.GenerateRequest, options *opts.Options, col *plugin
9696
}
9797

9898
// A domain is generated as the type it stands on, unless an override
99-
// above named the domain itself.
99+
// above named the domain itself. The base says how many dimensions the
100+
// column has, since a domain may stand on an array.
100101
if base := domainColumn(req, col); base != nil {
101-
return goInnerType(req, options, base, param)
102+
typ, complete := goInnerType(req, options, base, param)
103+
if base.IsArray && !complete {
104+
typ = strings.Repeat("[]", int(base.ArrayDims)) + typ
105+
}
106+
return typ, true
102107
}
103108

104109
// TODO: Extend the engine interface to handle types

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

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

‎internal/endtoend/testdata/domain_types/postgresql/go/query.sql.go‎

Lines changed: 3 additions & 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/schema.sql‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
CREATE DOMAIN tags AS text[];
12
CREATE DOMAIN title AS varchar(100) NOT NULL DEFAULT '';
23
CREATE DOMAIN price AS numeric(10, 2) CHECK (VALUE >= 0);
34
CREATE SCHEMA billing;
@@ -17,6 +18,8 @@ CREATE TABLE books (
1718
CREATE TABLE shelves (
1819
id serial PRIMARY KEY,
1920
titles title[] NOT NULL,
21+
tags tags NOT NULL,
22+
tag_sets tags[] NOT NULL,
2023
label title,
2124
note text
2225
);

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

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

‎internal/endtoend/testdata/domain_types/postgresql_core/go/query.sql.go‎

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
CREATE DOMAIN tags AS text[];
12
CREATE DOMAIN title AS varchar(100) NOT NULL;
23

34
CREATE TABLE shelves (
45
id serial PRIMARY KEY,
5-
titles title[] NOT NULL
6+
titles title[] NOT NULL,
7+
tags tags NOT NULL,
8+
tag_sets tags[] NOT NULL
69
);

0 commit comments

Comments
 (0)