Skip to content

Commit b4fac24

Browse files
authored
Use "nullable" instead of "null" in configuration (sqlc-dev#571)
* config: Add the nullable field to overrides Deprecate the problematic null field
1 parent fa7660c commit b4fac24

File tree

9 files changed

+125
-4
lines changed

9 files changed

+125
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ Each override document has the following keys:
330330
- The PostgreSQL type to override. Find the full list of supported types in [gen.go](https://github.com/kyleconroy/sqlc/blob/master/internal/dinosql/gen.go#L438).
331331
- `go_type`:
332332
- A fully qualified name to a Go type to use in the generated code.
333-
- `null`:
333+
- `nullable`:
334334
- If true, use this type when a column is nullable. Defaults to `false`.
335335

336336
### Per-Column Type Overrides

internal/codegen/golang/go_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func goInnerType(r *compiler.Result, col *compiler.Column, settings config.Combi
2828

2929
// package overrides have a higher precedence
3030
for _, oride := range settings.Overrides {
31-
if oride.DBType != "" && oride.DBType == columnType && oride.Null != notNull {
31+
if oride.DBType != "" && oride.DBType == columnType && oride.Nullable != notNull {
3232
return oride.GoTypeName
3333
}
3434
}

internal/config/config.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ type Override struct {
140140
Engine Engine `json:"engine,omitempty" yaml:"engine"`
141141

142142
// True if the GoType should override if the maching postgres type is nullable
143-
Null bool `json:"null" yaml:"null"`
143+
Nullable bool `json:"nullable" yaml:"nullable"`
144+
// Deprecated. Use the `nullable` property instead
145+
Deprecated_Null bool `json:"null" yaml:"null"`
144146

145147
// fully qualified name of the column, e.g. `accounts.id`
146148
Column string `json:"column" yaml:"column"`
@@ -163,6 +165,12 @@ func (o *Override) Parse() error {
163165
o.DBType = o.Deprecated_PostgresType
164166
}
165167

168+
// validate deprecated null field
169+
if o.Deprecated_Null {
170+
fmt.Fprintf(os.Stderr, "WARNING: \"null\" is deprecated. Instead, use the \"nullable\" field.\n")
171+
o.Nullable = true
172+
}
173+
166174
// validate option combinations
167175
switch {
168176
case o.Column != "" && o.DBType != "":

internal/endtoend/testdata/overrides_nullable/go/db.go

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

internal/endtoend/testdata/overrides_nullable/go/models.go

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

internal/endtoend/testdata/overrides_nullable/go/query.sql.go

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE TABLE foo (
2+
bar text,
3+
bam name,
4+
baz name not null
5+
);
6+
7+
-- name: ListFoo :many
8+
SELECT * FROM foo;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"engine": "postgresql",
6+
"path": "go",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
],
12+
"overrides": [
13+
{
14+
"db_type": "name",
15+
"go_type": "github.com/jackc/pgtype.Name"
16+
},
17+
{
18+
"db_type": "name",
19+
"go_type": "github.com/jackc/pgtype.Name",
20+
"nullable": true
21+
},
22+
{
23+
"db_type": "text",
24+
"go_type": "github.com/jackc/pgtype.Text",
25+
"null": true
26+
}
27+
]
28+
}

internal/mysql/gen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func (pGen PackageGenerator) goTypeCol(col Column) string {
232232
colName := col.Name.String()
233233

234234
for _, oride := range pGen.Overrides {
235-
shouldOverride := (oride.DBType != "" && oride.DBType == mySQLType && oride.Null != notNull) ||
235+
shouldOverride := (oride.DBType != "" && oride.DBType == mySQLType && oride.Nullable != notNull) ||
236236
(oride.ColumnName != "" && oride.ColumnName == colName && oride.Table.Rel == col.Table)
237237
if shouldOverride {
238238
return oride.GoTypeName

0 commit comments

Comments
 (0)