optioner

package
v1.0.31 Latest Latest
Warning

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

Go to latest
Published: May 10, 2024 License: MIT Imports: 2 Imported by: 1

Documentation

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[T] is a container for zero or one element of a given type.

func FirstOf

func FirstOf[T any](options ...Option[T]) Option[T]

func FlatMap

func FlatMap[A any, B any](a Option[A], fn func(A) Option[B]) Option[B]

func LastOf

func LastOf[T any](options ...Option[T]) Option[T]

func Map

func Map[A any, B any](a Option[A], fn func(A) B) Option[B]

func None

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

None[T] creates an Option[T] with no value.

func OfFn

func OfFn[T any](v T, fn func(v T) bool) Option[T]

OfFn[T] creates a Option[T] that may or may not have a value. If fn(V) is false, the returned Option[T] is empty (equivalent to None[T]()), If fn(V) is true, the returned Option[T] will contain the value v (equivalent to Some[T](v)).

func OfMap

func OfMap[K comparable, V any](v map[K]V) Option[map[K]V]

OfMap[map[K]V] creates a Option[map[K]V] that may or may not have a value.

If len(map[K]V) > 0 the returned Option[map[K]V] will contain the value v, equivalent to Some[map[K]V](v)

Otherwise the returned Option[map[K]V] is empty, equivalent to None[map[K]V]().

func OfOk

func OfOk[T any](v T, ok bool) Option[T]

OfOk[T] creates a Option[T] that may or may not have a value. If ok is false, the returned Option[T] is empty (equivalent to None[T]()), If ok is true, the returned Option[T] will contain the value v (equivalent to Some[T](v)).

func OfOptions

func OfOptions[T any](items ...Option[T]) Option[[]T]

func OfPtr

func OfPtr[T any](v *T) Option[T]

OfPtr[T] creates a Option[T] that may or may not have a value. If *T is nil the returned Option[T] is empty (equivalent to None[T]()), If *T is non-nil returned Option[T] will contain a value (equivalent to Some[T](v)).

func OfSlice

func OfSlice[T any](v []T) Option[[]T]

OfSlice[[]T] creates a Option[[]T] that may or may not have a value.

If len([]T) > 0 the returned Option[[]T] will contain the value v (equivalent to Some[[]T](v))

Otherwise the returned Option[[]T] is empty, equivalent to None[[]T]().

func OfZero

func OfZero[T comparable](v T) Option[T]

OfZero[T] creates a Option[T] that may or may not have a value. If T is the zero value, the returned Option[T] is empty (equivalent to None[T]()), If T is not a zero, the returned Option[T] will contain a value (equivalent to Some[T](v)).

func Some

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

Some[T] creates an Option[T] with the given value.

func (Option[T]) Defined

func (t Option[T]) Defined() bool

Defined returns true if the contained value is not empty, false otherwise

Inverse of Empty()

func (Option[T]) Empty

func (t Option[T]) Empty() bool

Empty returns true if the contained value is empty, false otherwise

Inverse of Defined()

func (Option[T]) Get

func (t Option[T]) Get() T

Get returns the contained value.

NOTE: If called and Empty() is true, the value will be the type's zero value.

func (Option[T]) MarshalJSON

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

MarshalJSON implements the encoding/json#Marshaler interface

func (Option[T]) MarshalYAML

func (t Option[T]) MarshalYAML() (interface{}, error)

MarshalYAML implements the gopkg.in/yaml.v2#Marshal interface

func (Option[T]) OrElse

func (t Option[T]) OrElse(v T) T

OrElse returns the contained value if Defined() is true or returns the provided argument.

func (Option[T]) String

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

String implments the Stringer interface.

func (*Option[T]) UnmarshalJSON

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

UnmarshalJSON implements the encoding/json#Unmarshaler interface

func (*Option[T]) UnmarshalYAML

func (t *Option[T]) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements the gopkg.in/yaml.v2#Unmarshal interface

Jump to

Keyboard shortcuts

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