opt

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Optional

type Optional[T any] struct {
	// contains filtered or unexported fields
}

func If

func If[T any, R any](optional Optional[T], handler func(T) R) Optional[R]

If allows you to handle an optional if it exists, otherwise return

func New

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

New creates a new Optional without a value.

func Of

func Of[T any](value T) Optional[T]

Of creates a new Optional with a value.

func OfPtr

func OfPtr[T any](value T) *Optional[*T]

func (Optional[T]) Exists

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

Returns true if the optional value has been set

func (Optional[T]) Get

func (o Optional[T]) Get() (T, bool)

Get returns the value and whether it exists. It's invalid to use the returned value if the bool is false.

func (Optional[Value]) GetOrElse

func (o Optional[Value]) GetOrElse(defaultValue Value) Value

GetOrElse returns the value if it exists and returns defaultValue otherwise.

func (Optional[Value]) MarshalJSON

func (o Optional[Value]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface. Optionals wil marshall and unmarshall as a nullable json field. Value type must also implement json.Marshaller.

func (Optional[Value]) MustGet

func (o Optional[Value]) MustGet() Value

MustGet returns the value if it exists and panics otherwise.

func (*Optional[Value]) UnmarshalJSON

func (o *Optional[Value]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. Optionals will marshall and unmarshall as a nullable json field. Value type must also implement json.Unmarshaler.

func (Optional[Value]) Value

func (o Optional[Value]) Value() Value

Value implements the Valuer interface.

Jump to

Keyboard shortcuts

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