sqlc
sqlc copied to clipboard
feat(sqlc): Support custom generic nullable types (i.e. `sql.Null[string]`)
Support wrapping any nullable field with a generic type provided by the user. The config looks like this:
sql:
- gen:
go:
...
generic_null_type:
import: "database/sql"
type: "Null"
and the generated models look like this:
type Foo struct {
ID int64
SometimesBlankText sql.Null[string]
SometimesBlankNumeric sql.Null[int64]
}
I only use sqlite but I added support & tests for Postgres and MySQL.
~The test cases use sql.Null
even though the project isn't on Go 1.22 yet, but that doesn't seem to be an issue since the generated test package doesn't seem to be built/run anywhere?~
~nvm it has indeed been upgraded already.~
nvm it was only upgraded in the Dockerfile. I'll make a dummy type for the tests.
I'm not supper happy about how this works (or rather doesn't work) with overrides. Ideally I would specify a custom override type without specifying its nullability, and then with this new option set it would wrap with the nullable type automatically (i.e. sql.Null[MyCustomOverride]
).
Superseded by #3279