Skip to content

golang: Output NullUUID when necessary #1137

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

Merged
merged 3 commits into from
Aug 19, 2021
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
12 changes: 12 additions & 0 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ func (i *importer) interfaceImports() fileImports {
if uses("uuid.UUID") && !overrideUUID {
pkg[ImportSpec{Path: "github.com/google/uuid"}] = struct{}{}
}
_, overrideNullUUID := overrideTypes["uuid.NullUUID"]
if uses("uuid.NullUUID") && !overrideNullUUID {
pkg[ImportSpec{Path: "github.com/google/uuid"}] = struct{}{}
}

// Custom imports
for _, o := range i.Settings.Overrides {
Expand Down Expand Up @@ -263,6 +267,10 @@ func (i *importer) modelImports() fileImports {
if i.usesType("uuid.UUID") && !overrideUUID {
pkg[ImportSpec{Path: "github.com/google/uuid"}] = struct{}{}
}
_, overrideNullUUID := overrideTypes["uuid.NullUUID"]
if i.usesType("uuid.NullUUID") && !overrideNullUUID {
pkg[ImportSpec{Path: "github.com/google/uuid"}] = struct{}{}
}

for _, o := range i.Settings.Overrides {
if o.GoBasicType || o.GoTypeName == "" {
Expand Down Expand Up @@ -408,6 +416,10 @@ func (i *importer) queryImports(filename string) fileImports {
if uses("uuid.UUID") && !overrideUUID {
pkg[ImportSpec{Path: "github.com/google/uuid"}] = struct{}{}
}
_, overrideNullUUID := overrideTypes["uuid.NullUUID"]
if uses("uuid.NullUUID") && !overrideNullUUID {
pkg[ImportSpec{Path: "github.com/google/uuid"}] = struct{}{}
}

// Custom imports
for _, o := range i.Settings.Overrides {
Expand Down
5 changes: 4 additions & 1 deletion internal/codegen/golang/postgresql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ func postgresType(r *compiler.Result, col *compiler.Column, settings config.Comb
return "sql.NullString"

case "uuid":
return "uuid.UUID"
if notNull {
return "uuid.UUID"
}
return "uuid.NullUUID"

case "inet", "cidr":
return "net.IP"
Expand Down
2 changes: 1 addition & 1 deletion internal/endtoend/testdata/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/gofrs/uuid v4.0.0+incompatible
github.com/google/uuid v1.2.0
github.com/google/uuid v1.3.0
github.com/jackc/pgconn v1.5.1-0.20200601181101-fa742c524853
github.com/jackc/pgtype v1.6.2
github.com/jackc/pgx/v4 v4.6.1-0.20200606145419-4e5062306904
Expand Down
2 changes: 2 additions & 0 deletions internal/endtoend/testdata/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRx
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
Expand Down
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/types_uuid/postgresql/stdlib/go/db.go

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.

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

11 changes: 11 additions & 0 deletions internal/endtoend/testdata/types_uuid/postgresql/stdlib/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE foo (
description text,
bar uuid,
baz uuid not null
);

-- name: List :many
SELECT * FROM foo;

-- name: Find :one
SELECT bar FROM foo WHERE baz = $1;
11 changes: 11 additions & 0 deletions internal/endtoend/testdata/types_uuid/postgresql/stdlib/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}