optional

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2022 License: MIT Imports: 5 Imported by: 7

README

optional.go

optional.go is a simple and generic wrapper around a value and a presence flag inspired by C++'s std::optional.

Installation

go get github.com/JonasMuehlmann/optional.go

How to use

This project exports the package optional, whose only exported entity is the generic Optional struct.

The Optional type implements several convenience methods and standard library interfaces( e.g json and SQL conversions).

To see the implemented methods and interfaces, refer to the documentation available at https://pkg.go.dev/github.com/JonasMuehlmann/optional.go.

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 {
	Wrappee  T    `json:"wrapee" db:"wrapee"`
	HasValue bool `json:"has_value" db:"has_value"`
}

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

func Make added in v1.1.0

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

Make creates an optional holding the specified wrappee and sets the HasValue flag to true.

func (Optional[T]) GetOrAlternative added in v1.5.0

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

GetOrAlternative returns the Wrapee if the HasValue flag is true, otherwise alternative is returned.

func (Optional[T]) GetOrDefault added in v1.5.0

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

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

func (Optional[T]) GetOrGenerate added in v1.5.0

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

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

func (Optional[T]) GetTransformedOrEmpty added in v1.5.0

func (optional Optional[T]) GetTransformedOrEmpty(transformer func(T) any) Optional[any]

GetTransformedOrEmpty returns transformer(Wrappee) if HasValue == true, otherwise returns an empty Optional.

func (Optional[T]) GetTransformedOrSelf added in v1.5.0

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

GetTransformedOrEmpty returns transformer(Wrappee) if HasValue == true, otherwise returns self.

func (Optional[T]) IsZero added in v1.6.0

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

func (Optional[T]) MarshalCSV added in v1.2.0

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

MarshalCSV implements the gocarina/gocsv.TypeMarshaller interface.

func (Optional[T]) MarshalJSON added in v1.6.0

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

func (*Optional[T]) MarshalText added in v1.2.0

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

MarshalText implements the encoding.TextMarshaller interface.

func (Optional[T]) Match added in v1.5.0

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

Match executes someHandler if HasValue is true and noneHandler otherwise.

func (*Optional[T]) Scan

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

Scan implements the database.sql.Scanner interface.

func (*Optional[T]) Set added in v1.5.0

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]) UnmarshalCSV added in v1.2.0

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 added in v1.2.0

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

UnmarshalText implements the encoding.TextUnmarshaller interface.

func (*Optional[T]) Unset added in v1.5.0

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