uopt

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 8 Imported by: 0

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 basicutils.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 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]) 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.

type OptBool added in v1.10.0

type OptBool = Opt[bool]

func NullBool

func NullBool() OptBool

type OptByte added in v1.10.0

type OptByte = Opt[byte]

func NullByte

func NullByte() OptByte

type OptComplex128 added in v1.10.0

type OptComplex128 = Opt[complex128]

func NullComplex128

func NullComplex128() OptComplex128

type OptComplex64 added in v1.10.0

type OptComplex64 = Opt[complex64]

func NullComplex64

func NullComplex64() OptComplex64

type OptDuration added in v1.10.0

type OptDuration = Opt[time.Duration]

func NullDuration added in v1.10.0

func NullDuration() OptDuration

type OptFloat32 added in v1.10.0

type OptFloat32 = Opt[float32]

func NullFloat32

func NullFloat32() OptFloat32

type OptFloat64 added in v1.10.0

type OptFloat64 = Opt[float64]

func NullFloat64

func NullFloat64() OptFloat64

type OptInt added in v1.10.0

type OptInt = Opt[int]

func NullInt

func NullInt() OptInt

type OptInt16 added in v1.10.0

type OptInt16 = Opt[int16]

func NullInt16

func NullInt16() OptInt16

type OptInt32 added in v1.10.0

type OptInt32 = Opt[int32]

func NullInt32

func NullInt32() OptInt32

type OptInt64 added in v1.10.0

type OptInt64 = Opt[int64]

func NullInt64

func NullInt64() OptInt64

type OptInt8 added in v1.10.0

type OptInt8 = Opt[int8]

func NullInt8

func NullInt8() OptInt8

type OptRune added in v1.10.0

type OptRune = Opt[rune]

func NullRune

func NullRune() OptRune

type OptString added in v1.10.0

type OptString = Opt[string]

func NullString

func NullString() OptString

type OptUint added in v1.10.0

type OptUint = Opt[uint]

func NullUint

func NullUint() OptUint

type OptUint16 added in v1.10.0

type OptUint16 = Opt[uint16]

func NullUint16

func NullUint16() OptUint16

type OptUint32 added in v1.10.0

type OptUint32 = Opt[uint32]

func NullUint32

func NullUint32() OptUint32

type OptUint64 added in v1.10.0

type OptUint64 = Opt[uint64]

func NullUint64

func NullUint64() OptUint64

type OptUint8 added in v1.10.0

type OptUint8 = Opt[uint8]

func NullUint8

func NullUint8() OptUint8

Jump to

Keyboard shortcuts

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