Documentation ¶
Overview ¶
Package optional provides the generic type Optional, which represents an optional value.
It also includes helpers to convert variables of sql.NullX type to Optional type and vice versa.
Index ¶
- Variables
- func ToNullFloat64[T constraints.Float](o Optional[T]) sql.NullFloat64
- func ToNullInt64[T constraints.Integer](o Optional[T]) sql.NullInt64
- func ToNullString(o Optional[string]) sql.NullString
- func ToNullTime(o Optional[time.Time]) sql.NullTime
- type Numeric
- type Optional
- func ConvertNumeric[X Numeric, Y Numeric](o Optional[X]) Optional[Y]
- func FromNullFloat64(v sql.NullFloat64) Optional[float64]
- func FromNullInt64(v sql.NullInt64) Optional[int64]
- func FromNullInt64ToInteger[T constraints.Integer](v sql.NullInt64) Optional[T]
- func FromNullString(v sql.NullString) Optional[string]
- func FromNullTime(v sql.NullTime) Optional[time.Time]
- func New[T any](v T) Optional[T]
Constants ¶
This section is empty.
Variables ¶
var ErrIsEmpty = errors.New("optional is empty")
Functions ¶
func ToNullFloat64 ¶
func ToNullFloat64[T constraints.Float](o Optional[T]) sql.NullFloat64
ToNullFloat64 converts an Optional variable to it's sql.Null equivalent and returns it.
func ToNullInt64 ¶
func ToNullInt64[T constraints.Integer](o Optional[T]) sql.NullInt64
ToNullInt64 converts an Optional variable to it's sql.Null equivalent and returns it.
func ToNullString ¶
func ToNullString(o Optional[string]) sql.NullString
ToNullString converts an Optional variable to it's sql.Null equivalent and returns it.
Types ¶
type Numeric ¶
type Numeric interface { constraints.Integer | constraints.Float }
type Optional ¶
type Optional[T any] struct { // contains filtered or unexported fields }
Optional represents a variable that may contain a value or not.
Note that the zero value of an Optional is a an empty Optional.
func ConvertNumeric ¶
ConvertNumeric converts between numeric optionals.
func FromNullFloat64 ¶
func FromNullFloat64(v sql.NullFloat64) Optional[float64]
FromNullFloat64 converts a sql.Null variable to it's Optional equivalent and returns it.
func FromNullInt64 ¶
FromNullInt64 converts a sql.Null variable to it's Optional equivalent and returns it.
func FromNullInt64ToInteger ¶
func FromNullInt64ToInteger[T constraints.Integer](v sql.NullInt64) Optional[T]
FromNullInt64ToInteger converts an sql.Null variable to an Optional of a different integer type and returns it.
func FromNullString ¶
func FromNullString(v sql.NullString) Optional[string]
FromNullInt64 converts a sql.Null variable to it's Optional equivalent and returns it.
func FromNullTime ¶
FromNullTime converts a sql.Null variable to it's Optional equivalent and returns it.
func (Optional[T]) MustValue ¶
func (o Optional[T]) MustValue() T
MustValue returns the value of an Optional or panics if it is empty.
func (Optional[T]) ValueOrFallback ¶
func (o Optional[T]) ValueOrFallback(fallback T) T
ValueOrFallback returns the value of an Optional or a fallback if it is empty.
func (Optional[T]) ValueOrZero ¶
func (o Optional[T]) ValueOrZero() T
ValueOrZero returns the value of an Optional or it's type's zero value if it is empty.