Skip to content

Commit f73d6a2

Browse files
EliSauderCopilot
andcommitted
fix(golang): keep an array's element in a resolved domain's flat type
The PostgreSQL mappers read the flat type, so naming it after the resolved expression made an array of a domain the type array, and generated []any. Describe the expression the way coreResultCatalog does: the innermost element's name and one dimension per array. Addresses review comment r4556992392 on #2. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 799ef80 commit f73d6a2

8 files changed

Lines changed: 115 additions & 1 deletion

File tree

‎internal/codegen/golang/domain_type.go‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ func domainColumn(req *plugin.GenerateRequest, col *plugin.Column) *plugin.Colum
1919
}
2020
out := proto.Clone(col).(*plugin.Column)
2121
out.TypeExpr = t
22-
out.Type = &plugin.Identifier{Name: t.GetName()}
22+
name, dims := flatType(t)
23+
out.Type = &plugin.Identifier{Name: name}
24+
out.IsArray, out.ArrayDims = dims > 0, dims
2325
return out
2426
}
2527
d := findDomain(req, col.Type.GetSchema(), col.Type.GetName())
@@ -31,6 +33,18 @@ func domainColumn(req *plugin.GenerateRequest, col *plugin.Column) *plugin.Colum
3133
return out
3234
}
3335

36+
// flatType is t as the flat description reads it, the way
37+
// coreResultCatalog writes one: the innermost element's name, with one
38+
// dimension per array around it.
39+
func flatType(t *plugin.TypeExpr) (string, int32) {
40+
var dims int32
41+
for typeExprName(t) == "array" && typeExprArg(t, 0) != nil {
42+
t = typeExprArg(t, 0)
43+
dims++
44+
}
45+
return t.GetName(), dims
46+
}
47+
3448
// resolveDomains replaces each domain in t with its base, keeping the
3549
// nullability t says each position has.
3650
func resolveDomains(req *plugin.GenerateRequest, t *plugin.TypeExpr) (*plugin.TypeExpr, bool) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"contexts": ["base"],
3+
"env": {
4+
"SQLCEXPERIMENT": "coreanalyzer"
5+
}
6+
}

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

Lines changed: 32 additions & 0 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/models.go‎

Lines changed: 10 additions & 0 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: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: ListShelves :many
2+
SELECT * FROM shelves;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE DOMAIN title AS varchar(100) NOT NULL;
2+
3+
CREATE TABLE shelves (
4+
id serial PRIMARY KEY,
5+
titles title[] NOT NULL
6+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "2"
2+
sql:
3+
- engine: "postgresql"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"
10+
sql_package: "pgx/v5"

0 commit comments

Comments
 (0)