Skip to content
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

Support sql.Null #3149

Open
kyleconroy opened this issue Jan 18, 2024 · 2 comments · May be fixed by #3279
Open

Support sql.Null #3149

kyleconroy opened this issue Jan 18, 2024 · 2 comments · May be fixed by #3279
Labels
enhancement New feature or request triage New issues that hasn't been reviewed

Comments

@kyleconroy
Copy link
Collaborator

What do you want to change?

Go 1.22 includes a generic Null type.

type Null[T [any](https://pkg.go.dev/builtin#any)] struct {
	V     T
	Valid [bool](https://pkg.go.dev/builtin#bool)
}

Null represents a value that may be null. Null implements the Scanner interface so it can be used as a scan destination:

var s Null[string]
err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s)
...
if s.Valid {
   // use s.V
} else {
   // NULL value
}

This would make our lives much easier when generating code

What database engines need to be changed?

No response

What programming language backends need to be changed?

No response

@kyleconroy kyleconroy added enhancement New feature or request triage New issues that hasn't been reviewed labels Jan 18, 2024
@gregoryjjb
Copy link

What if you could configure any generic type to wrap nulls? Something like:

go:
  ...
  generic_null_type:
    import: "my/custom/null"
    type: "Null"
  ...

and then a nullable string column would be generated as null.Null[string].

Use case: sql.Null doesn't appear to have good JSON marshaling, it would be nice to be able to choose an option that does.

This is kind of doable currently by overriding every single nullable type individually in the config.

@gregoryjjb
Copy link

Put together an implementation of my above suggestion in #3202

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request triage New issues that hasn't been reviewed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants