monad

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Maybe

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

Represents an optional value of type T. It implements some infrastructure interfaces such as JSON (un)marshalling and database convert.

This is much more clean than using pointers to express optional values.

func None

func None[T any]() (m Maybe[T])

Instantiates an empty monad for the given type.

func Value

func Value[T any](value T) (m Maybe[T])

Instantiates a monad with a defined value.

func (Maybe[T]) Get

func (m Maybe[T]) Get(fallback T) T

Retrieve the inner value or the fallback if it doesn't have one.

func (Maybe[T]) HasValue

func (m Maybe[T]) HasValue() bool

Has a value been set on this monad?

func (Maybe[T]) IsZero added in v1.0.1

func (m Maybe[T]) IsZero() bool

func (Maybe[T]) MarshalJSON

func (m Maybe[T]) MarshalJSON() ([]byte, error)

func (Maybe[T]) MarshalYAML added in v1.0.1

func (m Maybe[T]) MarshalYAML() (any, error)

func (Maybe[T]) MustGet

func (m Maybe[T]) MustGet() T

Retrieve the inner value and panic if none is set.

func (Maybe[T]) None

func (m Maybe[T]) None() Maybe[T]

Unset this monad value.

func (*Maybe[T]) Scan

func (m *Maybe[T]) Scan(value any) error

Implements the db scanner interface to retrieve it easily from the storage.

func (Maybe[T]) TryGet added in v1.2.0

func (m Maybe[T]) TryGet() (T, bool)

Get the inner value and a boolean indicating if it has been set.

func (*Maybe[T]) UnmarshalEnvironmentValue added in v1.0.1

func (m *Maybe[T]) UnmarshalEnvironmentValue(data string) error

func (*Maybe[T]) UnmarshalJSON

func (m *Maybe[T]) UnmarshalJSON(data []byte) error

func (*Maybe[T]) UnmarshalYAML added in v1.0.1

func (m *Maybe[T]) UnmarshalYAML(value *yaml.Node) error

func (Maybe[T]) Value

func (m Maybe[T]) Value() (driver.Value, error)

Implements the db valuer interface to persist it easily. If you want the value of the maybe, you may check Get instead.

func (Maybe[T]) WithValue

func (m Maybe[T]) WithValue(value T) Maybe[T]

Assign the given value and returns the monad.

type Patch

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

Type used to patch data. Those values could be set, nil or have a value.

func Nil

func Nil[T any]() (p Patch[T])

Builds a nil patch.

func PatchValue

func PatchValue[T any](value T) (p Patch[T])

Builds a new patch with the given value.

func (Patch[T]) IsNil

func (p Patch[T]) IsNil() bool

func (Patch[T]) IsSet

func (p Patch[T]) IsSet() bool

func (Patch[T]) TryGet added in v1.2.0

func (p Patch[T]) TryGet() (Maybe[T], bool)

Try to get the inner optional value for this Patch structure. The boolean returns if the patch has been set so the returned value may represent a nil value.

func (*Patch[T]) UnmarshalJSON

func (p *Patch[T]) UnmarshalJSON(data []byte) error

Implements the UnmarshalJSON interface.

Jump to

Keyboard shortcuts

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