Documentation ¶
Index ¶
- type Opt
- func Null[T any]() Opt[T]
- func Of[T any](v T) Opt[T]
- func OfBool(v bool) Opt[bool]
- func OfBuilder[T any](build func() T) Opt[T]
- func OfCond[T any](v T, cond func(v *T) bool) Opt[T]
- func OfNullable[T any](v *T) Opt[T]
- func OfNumeric[T basic_utils.Numeric](v T) Opt[T]
- func OfString(v string) Opt[string]
- func OfUnix[T basic_utils.SignedNumeric](v T) Opt[time.Time]
- func (o Opt[T]) Get() *T
- func (o Opt[T]) GetAs(mapping func(t *T) any) any
- func (o Opt[T]) IfPresent(f func(t T))
- func (o Opt[T]) MarshalJSON() ([]byte, error)
- func (o Opt[T]) OrElse(v T) T
- func (o Opt[T]) Present() bool
- func (o *Opt[T]) Scan(src interface{}) error
- func (o *Opt[T]) Set(v *T)
- func (o *Opt[T]) UnmarshalJSON(bytes []byte) error
- func (o Opt[T]) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Opt ¶
type Opt[T any] struct { // contains filtered or unexported fields }
Opt represents a generic container for optional values. An Opt can either contain a value of type T or no value at all. It provides methods to check the presence of a value and to retrieve it. This struct offers a safer way to handle potentially absent values, avoiding nil dereferences. Using Opt ensures that the user must explicitly handle both the present and absent cases, thus preventing unintentional null pointer errors.
It's similar in principle to "Optional" in other languages like Java's java.util.Optional.
The internal 'v' field is a pointer to a value of type T. If 'v' is nil, it means the Opt contains no value. Otherwise, 'v' points to the contained value.
func OfBuilder ¶
OfBuilder creates an Opt by invoking the provided builder function to generate a value.
func OfNullable ¶
OfNullable creates an Opt that may or may not contain a value based on the provided pointer.
func OfNumeric ¶
func OfNumeric[T basic_utils.Numeric](v T) Opt[T]
OfNumeric creates an Opt containing a numeric value, or a null Opt if the value is 0.
func OfUnix ¶
func OfUnix[T basic_utils.SignedNumeric](v T) Opt[time.Time]
OfUnix creates an Opt containing a time.Time value based on a Unix timestamp.
func (Opt[T]) IfPresent ¶
func (o Opt[T]) IfPresent(f func(t T))
IfPresent invokes the provided function if the Opt contains a value.
func (Opt[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface for the Opt type.
func (Opt[T]) OrElse ¶
func (o Opt[T]) OrElse(v T) T
OrElse retrieves the value within the Opt or a provided default if the Opt is null.
func (*Opt[T]) Scan ¶
Scan implements the sql.Scanner interface for the Opt type, reading a SQL value into the Opt.
func (*Opt[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface for the Opt type.