optional

package
v3.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

optional.go is a simple wrapper around a value and a presence flag

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
}

Optional holds a Wrappe and a flag indicating the existence or abscence of the Wrapee.

func None

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

None creates an optional holding the specified wrappee and sets the hasValue flag to false.

func Some

func Some[T any](wrappee T) Optional[T]

Some creates an optional holding the specified wrappee and sets the hasValue flag to true.

func (Optional[T]) AssertNone

func (optional Optional[T]) AssertNone()

func (Optional[T]) AssertSome

func (optional Optional[T]) AssertSome()

func (Optional[T]) GetElseAlt

func (optional Optional[T]) GetElseAlt(alternative T) T

GetElseAlt returns the Wrapee if the hasValue flag is true, otherwise alternative is returned.

func (Optional[T]) GetElseDefault

func (optional Optional[T]) GetElseDefault() T

GetOrDefualt returns the Wrapee if the hasValue flag is true, otherwise T's default value is returned.

func (Optional[T]) GetElseFrom

func (optional Optional[T]) GetElseFrom(alternative func() T) Optional[T]

GetElseFrom returns the Wrapee if the hasValue flag is true, otherwise the result of alternative() is returned.

func (Optional[T]) GetTransformedElseNone

func (optional Optional[T]) GetTransformedElseNone(transformer func(T) T) Optional[T]

GetTransformedOrEmpty returns transformer(wrappee) if hasValue == true, otherwise returns self.

func (Optional[T]) IsNone

func (optional Optional[T]) IsNone() bool

func (Optional[T]) IsSome

func (optional Optional[T]) IsSome() bool

func (Optional[T]) MarshalCSV

func (optional Optional[T]) MarshalCSV() (string, error)

MarshalCSV implements the gocarina/gocsv.TypeMarshaller interface.

func (Optional[T]) MarshalJSON

func (optional Optional[T]) MarshalJSON() ([]byte, error)

func (*Optional[T]) MarshalText

func (optional *Optional[T]) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaller interface.

func (Optional[T]) Match

func (optional Optional[T]) Match(someHandler func(T), noneHandler func(T))

Match executes someHandler if hasValue is true and noneHandler otherwise.

func (Optional[T]) MustGet

func (optional Optional[T]) MustGet() T

func (*Optional[T]) Scan

func (optional *Optional[T]) Scan(value any) error

Scan implements the database.sql.Scanner interface.

func (*Optional[T]) Set

func (optional *Optional[T]) Set(val T)

Set sets a wrappee to val value and the hasValue flag to true.

func (Optional[T]) String

func (optional Optional[T]) String() string

String implements the fmt.Stringer interface.

func (Optional[T]) TryGetElseFrom

func (optional Optional[T]) TryGetElseFrom(alternative func() (T, error)) result.Result[T]

func (*Optional[T]) UnmarshalCSV

func (optional *Optional[T]) UnmarshalCSV(val string) error

UnmarshalCSV implements the gocarina/gocsv.TypeUnmarshaller interface.

func (*Optional[T]) UnmarshalJSON

func (optional *Optional[T]) UnmarshalJSON(input []byte) error

func (*Optional[T]) UnmarshalText

func (optional *Optional[T]) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaller interface.

func (*Optional[T]) Unset

func (optional *Optional[T]) Unset() T

Unset sets the hasValue flag to false and returns the wrappee.

func (Optional[T]) Value

func (optional Optional[T]) Value() (driver.Value, error)

Value implements the database.sql.driver.Valuer interface.

Jump to

Keyboard shortcuts

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