Skip to content

Commit d0cf1e5

Browse files
authored
golang: Output NullUUID when necessary (#1137)
* golang: Output NullUUID when necessary * Upgrade to the latest version of google/uuid * Make sure that overrides continue to work
1 parent b36e368 commit d0cf1e5

File tree

19 files changed

+133
-2
lines changed

19 files changed

+133
-2
lines changed

internal/codegen/golang/imports.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ func (i *importer) interfaceImports() fileImports {
202202
if uses("uuid.UUID") && !overrideUUID {
203203
pkg[ImportSpec{Path: "github.com/google/uuid"}] = struct{}{}
204204
}
205+
_, overrideNullUUID := overrideTypes["uuid.NullUUID"]
206+
if uses("uuid.NullUUID") && !overrideNullUUID {
207+
pkg[ImportSpec{Path: "github.com/google/uuid"}] = struct{}{}
208+
}
205209

206210
// Custom imports
207211
for _, o := range i.Settings.Overrides {
@@ -263,6 +267,10 @@ func (i *importer) modelImports() fileImports {
263267
if i.usesType("uuid.UUID") && !overrideUUID {
264268
pkg[ImportSpec{Path: "github.com/google/uuid"}] = struct{}{}
265269
}
270+
_, overrideNullUUID := overrideTypes["uuid.NullUUID"]
271+
if i.usesType("uuid.NullUUID") && !overrideNullUUID {
272+
pkg[ImportSpec{Path: "github.com/google/uuid"}] = struct{}{}
273+
}
266274

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

412424
// Custom imports
413425
for _, o := range i.Settings.Overrides {

internal/codegen/golang/postgresql_type.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ func postgresType(r *compiler.Result, col *compiler.Column, settings config.Comb
103103
return "sql.NullString"
104104

105105
case "uuid":
106-
return "uuid.UUID"
106+
if notNull {
107+
return "uuid.UUID"
108+
}
109+
return "uuid.NullUUID"
107110

108111
case "inet", "cidr":
109112
return "net.IP"

internal/endtoend/testdata/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.16
44

55
require (
66
github.com/gofrs/uuid v4.0.0+incompatible
7-
github.com/google/uuid v1.2.0
7+
github.com/google/uuid v1.3.0
88
github.com/jackc/pgconn v1.5.1-0.20200601181101-fa742c524853
99
github.com/jackc/pgtype v1.6.2
1010
github.com/jackc/pgx/v4 v4.6.1-0.20200606145419-4e5062306904

internal/endtoend/testdata/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRx
1616
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
1717
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
1818
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
19+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
20+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1921
github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=
2022
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
2123
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=

0 commit comments

Comments
 (0)