forgejo/modules/optional/option.go
Mathieu Fenniak d442f83a09 chore: support Option[T] as a type on database schema structs (#11553)
Adds support for `optional.Option[T]` to be used on an xorm schema struct to represent nullable fields.  The `optional.None[T]()` value will be stored in the database as `NULL`.

```go
type OptionString struct {
	ID     int64 `xorm:"pk autoincr"`
	StringField optional.Option[string]
}
```

Before this change, it is possible to represent a nullable field in two reasonable ways: , or as a `sql.Null[T]` (eg. `StringField sql.Null[string]`).  The problems with these are:
- as a pointer (eg. `StringField *string`) -- but this introduces the risk of panics when `nil` values are dereferenced, and makes it difficult to use literals in structure creation (although `new()` in Go 1.26 would reduce this issue when Forgejo is upgraded to it)
- as a `sql.Null[T]` -- but this "leaks" references to the `database/sql` package for anything that interacts with Forgejo models, and it's API is awkward as nothing gates you into checking the `Valid` field before you access and use the `V` field

`optional.Option[T]` addresses these points and provides a single way to use an optional primitive type, with a safe check-before-access interface, which can be used consistently throughout model code and other application code.  Figuring out the best way to handle this became a blocker to me for [adding foreign keys to nullable fields](https://codeberg.org/forgejo/discussions/issues/385#issuecomment-10218316) in database models, which is what drove me to implement this solution.

## Notes: Filtering on `Option[T]` Fields

It is supported and functional to perform queries with xorm beans with non-None `Option` values.  For example:
```go
cond := &OptionString{
	StringField: optional.Some("hello"),
}
err := db.GetEngine(t.Context()).Find(&arr, cond)
```
will generate a database query `WHERE string_field = 'hello'`, and correctly filter the records.

It is **not** supported to perform queries with `None` values, for two reasons:
- xorm cannot distinguish between an explicit `&OptionString{ StringField: optional.None[string]() }`, and `&OptionString{}`.  Both of them have the `StringField` field set to the zero-value of `Option[String]`.
- For this SQL query to be formatted correctly, it would require `WHERE string_field IS NOT NULL`, not `WHERE string_field = NULL`.  This is not how xorm generated bean-based queries.

This is similar to the risk that exists with any other field querying on its zero-value with xorm.  It's an unfortunate structural limitation of xorm, and can lead to developers believing database queries are performing filtering that they are not.

(perhaps we can mitigate this risk with semgrep or other automated tooling in the future)

## Checklist

The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).

### Tests for Go changes

(can be removed for JavaScript changes)

- I added test coverage for Go changes...
  - [x] in their respective `*_test.go` for unit tests.
  - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I ran...
  - [x] `make pr-go` before pushing

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [ ] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change.
- [x] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11553
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2026-03-08 03:36:32 +01:00

113 lines
2.4 KiB
Go

// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package optional
import (
"database/sql"
"database/sql/driver"
"reflect"
"strconv"
"xorm.io/xorm/schemas"
)
type Option[T any] []T
func None[T any]() Option[T] {
return nil
}
func Some[T any](v T) Option[T] {
return Option[T]{v}
}
func FromPtr[T any](v *T) Option[T] {
if v == nil {
return None[T]()
}
return Some(*v)
}
func FromNonDefault[T comparable](v T) Option[T] {
var zero T
if v == zero {
return None[T]()
}
return Some(v)
}
func (o Option[T]) Has() bool {
return o != nil
}
func (o Option[T]) Get() (has bool, value T) {
if o != nil {
has = true
value = o[0]
}
return has, value
}
func (o Option[T]) ValueOrZeroValue() T {
var zeroValue T
return o.ValueOrDefault(zeroValue)
}
func (o Option[T]) ValueOrDefault(v T) T {
if o.Has() {
return o[0]
}
return v
}
// ParseBool get the corresponding optional.Option[bool] of a string using strconv.ParseBool
func ParseBool(s string) Option[bool] {
v, e := strconv.ParseBool(s)
if e != nil {
return None[bool]()
}
return Some(v)
}
// Option[T] can be used in an xorm bean as a field type for a nullable column. Multiple interfaces must be implemented
// for this to work correctly and won't be checked at compile-time of the bean struct, so they're asserted here in case
// the interface definitions change:
var (
_ sql.Scanner = (*Option[bool])(nil) // read data from DB
_ driver.Valuer = None[bool]() // write data to DB
_ schemas.SQLTypeDelegator = None[bool]() // represent column field type correctly
)
// Convert database data into an Option[T]. sql.Null[T] has all the necessary logic to perform Value(), so it is used as
// an implementation.
func (o *Option[T]) Scan(value any) error {
var n sql.Null[T]
if err := n.Scan(value); err != nil {
return err
}
if n.Valid {
*o = Some(n.V)
} else {
*o = None[T]()
}
return nil
}
// Convert Option[T] into the necessary database data to represent it. sql.Null[T] has all the necessary logic to
// perform Value(), so it is used as an implementation.
func (o Option[T]) Value() (driver.Value, error) {
var n sql.Null[T]
if o.Has() {
n.V = o[0]
n.Valid = true
} else {
n.Valid = false
}
return n.Value()
}
// Make xorm use whatever SQLType is appropriate for T to represent Option[T] in the database table
func (o Option[T]) DelegateSQLType() reflect.Type {
return reflect.TypeFor[T]()
}