option

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2022 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package option provides a way to represent optional values. It basically is a struct with a pointer to the value, but with a lot of little helper methods. It supports JSON marshaling and unmarshaling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

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

Option is a container for an optional value of type T. It uses nil to represent the absence of a value.

func None

func None[T any]() Option[T]

None builds an Option when value is absent.

func Some

func Some[T any](value T) Option[T]

Some builds an Option when value is present.

func TupleToOption

func TupleToOption[T any](value T, ok bool) Option[T]

TupleToOption converts a tuple to an Option.

func (*Option[T]) Apply

func (option *Option[T]) Apply(f func(T) T)

Apply applies the given function to the value of the Option.

func (Option[T]) GetValue added in v1.4.0

func (option Option[T]) GetValue() (T, bool)

GetValue returns value and presence. If value is absent, returns zero value and false.

func (Option[T]) IsAbsent

func (option Option[T]) IsAbsent() bool

IsAbsent returns true when value is present.

func (Option[T]) IsPresent

func (option Option[T]) IsPresent() bool

IsPresent returns true when value is absent.

func (Option[T]) MarshalJSON

func (option Option[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Option[T]) MustValue

func (option Option[T]) MustValue() T

MustValue returns value if present or panics instead.

func (*Option[T]) Scan added in v1.4.0

func (t *Option[T]) Scan(v interface{}) error

Scan implements the sql.Scanner interface. It means that the Option can be scanned from a database value.

func (Option[T]) String

func (option Option[T]) String() string

String returns a string representation of the Option.

func (*Option[T]) UnmarshalJSON

func (option *Option[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Option[T]) Update

func (option *Option[T]) Update(value T)

Update updates the value of the Option.

func (Option[T]) Value

func (t Option[T]) Value() (driver.Value, error)

Value implements the driver.Valuer interface. To safely get the value of an Option, use GetValue().

func (Option[T]) ValueOrDefault

func (option Option[T]) ValueOrDefault(fallback T) T

ValueOrDefault returns value if present or the default value given instead.

Jump to

Keyboard shortcuts

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