mirror of
https://github.com/ollama/ollama.git
synced 2025-11-11 00:08:00 +01:00
there's a common pattern where request fields may need to differentiate between an unset value and a value set to the type's zero value. this is commonly used to apply a different default value, e.g. stream, or to omit a field entirely, e.g. think. similar to sql.Null[T], types.Null[T] simplifies this by providing utilities to quickly and easily apply this pattern to any type using generics.
54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package types
|
|
|
|
import (
|
|
"encoding/json"
|
|
)
|
|
|
|
// Null represents a value of any type T that may be null.
|
|
type Null[T any] struct {
|
|
value T
|
|
valid bool
|
|
}
|
|
|
|
// NullWithValue creates a new, valid Null[T].
|
|
func NullWithValue[T any](value T) Null[T] {
|
|
return Null[T]{value: value, valid: true}
|
|
}
|
|
|
|
// Value returns the value of the Type[T] if set, otherwise it returns the provided default value or the zero value of T.
|
|
func (n Null[T]) Value(defaultValue ...T) T {
|
|
if n.valid {
|
|
return n.value
|
|
}
|
|
if len(defaultValue) > 0 {
|
|
return defaultValue[0]
|
|
}
|
|
var zero T
|
|
return zero
|
|
}
|
|
|
|
// SetValue sets the value of the Type[T].
|
|
func (n *Null[T]) SetValue(t T) {
|
|
n.value = t
|
|
n.valid = true
|
|
}
|
|
|
|
// MarshalJSON implements [json.Marshaler].
|
|
func (n Null[T]) MarshalJSON() ([]byte, error) {
|
|
if n.valid {
|
|
return json.Marshal(n.value)
|
|
}
|
|
return []byte("null"), nil
|
|
}
|
|
|
|
// UnmarshalJSON implements [json.Unmarshaler].
|
|
func (n *Null[T]) UnmarshalJSON(data []byte) error {
|
|
if string(data) != "null" {
|
|
if err := json.Unmarshal(data, &n.value); err != nil {
|
|
return err
|
|
}
|
|
n.valid = true
|
|
}
|
|
return nil
|
|
}
|