Skip to content
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

fix(codegen/golang): fix sqlc.embed to work with pq.Array wrap #2544

Merged
merged 2 commits into from
Jul 30, 2023
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
2 changes: 1 addition & 1 deletion internal/codegen/golang/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Field struct {
Comment string
Column *plugin.Column
// EmbedFields contains the embedded fields that require scanning.
EmbedFields []string
EmbedFields []Field
}

func (gf Field) Tag() string {
Expand Down
5 changes: 5 additions & 0 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ func (i *importer) queryImports(filename string) fileImports {
if strings.HasPrefix(f.Type, "[]") && f.Type != "[]byte" {
return true
}
for _, embed := range f.EmbedFields {
if strings.HasPrefix(embed.Type, "[]") && embed.Type != "[]byte" {
return true
}
}
}
} else {
if strings.HasPrefix(q.Ret.Type(), "[]") && q.Ret.Type() != "[]byte" {
Expand Down
6 changes: 5 additions & 1 deletion internal/codegen/golang/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ func (v QueryValue) Scan() string {
// append any embedded fields
if len(f.EmbedFields) > 0 {
for _, embed := range f.EmbedFields {
out = append(out, "&"+v.Name+"."+f.Name+"."+embed)
if strings.HasPrefix(embed.Type, "[]") && embed.Type != "[]byte" && !v.SQLDriver.IsPGX() {
out = append(out, "pq.Array(&"+v.Name+"."+f.Name+"."+embed.Name+")")
} else {
out = append(out, "&"+v.Name+"."+f.Name+"."+embed.Name)
}
}
continue
}
Expand Down
6 changes: 3 additions & 3 deletions internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ type goColumn struct {
type goEmbed struct {
modelType string
modelName string
fields []string
fields []Field
}

// look through all the structs and attempt to find a matching one to embed
Expand All @@ -138,9 +138,9 @@ func newGoEmbed(embed *plugin.Identifier, structs []Struct, defaultSchema string
continue
}

fields := make([]string, len(s.Fields))
fields := make([]Field, len(s.Fields))
for i, f := range s.Fields {
fields[i] = f.Name
fields[i] = f
}

return &goEmbed{
Expand Down

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

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 @@ -8,7 +8,8 @@ CREATE TABLE users (

CREATE TABLE posts (
id integer NOT NULL PRIMARY KEY,
user_id integer NOT NULL
user_id integer NOT NULL,
likes integer[] NOT NULL
);

CREATE TABLE baz.users (
Expand Down
Loading