optional

package
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2024 License: MIT Imports: 5 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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.

func ToNullTime

func ToNullTime(o Optional[time.Time]) sql.NullTime

ToNullTime 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

func ConvertNumeric[X Numeric, Y Numeric](o Optional[X]) Optional[Y]

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

func FromNullInt64(v sql.NullInt64) Optional[int64]

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

func FromNullTime(v sql.NullTime) Optional[time.Time]

FromNullTime converts a sql.Null variable to it's Optional equivalent and returns it.

func New

func New[T any](v T) Optional[T]

New returns a new Optional with a value.

func (*Optional[T]) Clear

func (o *Optional[T]) Clear()

Clear removes any value.

func (Optional[T]) IsEmpty

func (o Optional[T]) IsEmpty() bool

IsEmpty reports wether an Optional is empty.

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

func (o *Optional[T]) Set(v T)

Set sets a new value.

func (Optional[T]) String

func (o Optional[T]) String() string

String returns a string representation of an Optional.

func (Optional[T]) Value

func (o Optional[T]) Value() (T, error)

Value returns the value of an Optional.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL