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 basicutils.Numeric](v T) Opt[T]
- func OfString(v string) Opt[string]
- func OfUnix[T basicutils.SignedNumeric](v T) Opt[time.Time]
- func (o Opt[T]) Def() T
- 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)
- type OptBool
- type OptByte
- type OptComplex128
- type OptComplex64
- type OptDuration
- type OptFloat32
- type OptFloat64
- type OptInt
- type OptInt16
- type OptInt32
- type OptInt64
- type OptInt8
- type OptRune
- type OptString
- type OptUint
- type OptUint16
- type OptUint32
- type OptUint64
- type OptUint8
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 basicutils.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 basicutils.SignedNumeric](v T) Opt[time.Time]
OfUnix creates an Opt containing a time.Time value based on a Unix timestamp.
func (Opt[T]) Def ¶
func (o Opt[T]) Def() T
Def behaves as Get, but the returns default value if value is not present, Def is an alias to OrElse(*new(T))
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.
type OptComplex128 ¶ added in v1.10.0
type OptComplex128 = Opt[complex128]
func NullComplex128 ¶
func NullComplex128() OptComplex128
type OptComplex64 ¶ added in v1.10.0
func NullComplex64 ¶
func NullComplex64() OptComplex64
type OptDuration ¶ added in v1.10.0
func NullDuration ¶ added in v1.10.0
func NullDuration() OptDuration
type OptFloat32 ¶ added in v1.10.0
func NullFloat32 ¶
func NullFloat32() OptFloat32
type OptFloat64 ¶ added in v1.10.0
func NullFloat64 ¶
func NullFloat64() OptFloat64
type OptString ¶ added in v1.10.0
func NullString ¶
func NullString() OptString
type OptUint16 ¶ added in v1.10.0
func NullUint16 ¶
func NullUint16() OptUint16
type OptUint32 ¶ added in v1.10.0
func NullUint32 ¶
func NullUint32() OptUint32
type OptUint64 ¶ added in v1.10.0
func NullUint64 ¶
func NullUint64() OptUint64