sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

feat(sqlc): Support custom generic nullable types (i.e. `sql.Null[string]`)

Open gregoryjjb opened this issue 1 year ago • 1 comments

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.

gregoryjjb avatar Feb 14 '24 23:02 gregoryjjb

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]).

gregoryjjb avatar Feb 14 '24 23:02 gregoryjjb

Superseded by #3279

gregoryjjb avatar Mar 17 '24 20:03 gregoryjjb