opt

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: MIT Imports: 8 Imported by: 1

Documentation

Index

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 Null

func Null[T any]() Opt[T]

Null creates an Opt with no value.

func Of

func Of[T any](v T) Opt[T]

Of creates an Opt with a value.

func OfBool

func OfBool(v bool) Opt[bool]

OfBool creates an Opt containing a boolean, or a null Opt if the boolean is false.

func OfBuilder

func OfBuilder[T any](build func() T) Opt[T]

OfBuilder creates an Opt by invoking the provided builder function to generate a value.

func OfCond

func OfCond[T any](v T, cond func(v *T) bool) Opt[T]

OfCond creates an Opt containing a value if a given condition is met.

func OfNullable

func OfNullable[T any](v *T) Opt[T]

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 OfString

func OfString(v string) Opt[string]

OfString creates an Opt containing a string, or a null Opt if the string is empty.

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

func (o Opt[T]) Get() *T

Get retrieves the value within the Opt as a pointer.

func (Opt[T]) GetAs

func (o Opt[T]) GetAs(mapping func(t *T) any) any

GetAs retrieves the value within the Opt after applying a mapping function.

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

func (o Opt[T]) MarshalJSON() ([]byte, error)

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

func (o Opt[T]) Present() bool

Present checks if the Opt contains a value.

func (*Opt[T]) Scan

func (o *Opt[T]) Scan(src interface{}) error

Scan implements the sql.Scanner interface for the Opt type, reading a SQL value into the Opt.

func (*Opt[T]) Set

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

Set sets the value within the Opt.

func (*Opt[T]) UnmarshalJSON

func (o *Opt[T]) UnmarshalJSON(bytes []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for the Opt type.

func (Opt[T]) Value

func (o Opt[T]) Value() (driver.Value, error)

Value implements the driver.Valuer interface for the Opt type, converting its value to a SQL value.

Jump to

Keyboard shortcuts

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