optional

package module
v0.0.0-...-1ef641e Latest Latest
Warning

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

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

README

optional

The optional package provides an option type, which can either be empty or hold a value. In that respect, it's very similar to an ordinary pointer type, except it has methods that make its possible emptiness more explicit.

The interface of the optional.Value type is modeled on Java's java.util.Optional type and C++'s std::optional.

Usage

go get github.com/rkennedy/optional
import github.com/rkennedy/optional
full := optional.New(42)
empty := optional.Value[int]{}

fmt.Printf("full: %v\n", full)  // Output: 42
fmt.Printf("empty: %v\n", empty) // Output: None

Documentation

Overview

Package optional provides an “option type,” which can either be empty or hold a value. In that respect, it's very similar to an ordinary pointer type, except it has methods that make its possible emptiness more explicit.

Index

Constants

This section is empty.

Variables

View Source
var ErrEmpty = errors.New("value not present")

ErrEmpty indicates that an optional value was empty when its value was requested.

Functions

This section is empty.

Types

type Value

type Value[T any] []T

Value is a type that may or may not hold a value. Its interface is modeled on Java's java.util.Optional type and C++'s std::optional.

Value implements the following interfaces:

func New

func New[T any](v T) Value[T]

New creates a new Value holding the given value.

func Transform

func Transform[T, U any](in Value[T], fn func(T) U) Value[U]

Transform applies the given function to the optional value if the input value is non-empty, and returns a new optional of the corresponding return type holding the returned value. Returns an empty value if the input is empty.

func TransformWithError

func TransformWithError[T, U any](in Value[T], fn func(T) (U, error)) (result Value[U], err error)

TransformWithError applies the given function to the optional value if the input value is non-empty, and returns a new optional of the corresponding return type holding the returned value. Returns an empty value if the input is empty. If the transform function returns an error, then an empty value and that error are returned.

func (Value[T]) Get

func (o Value[T]) Get() (result T, err error)

Get returns the current value, if there is one. If the Value is empty, then ErrEmpty is returned, and the value result is unspecified.

func (Value[T]) GoString

func (o Value[T]) GoString() string

GoString fornats the Value as Go code, providing an implementation for the %#v format string.

func (Value[T]) If

func (o Value[T]) If(fn func(T))

If calls the given function if the Value holds a value. If Value is empty, then If is a no-op.

func (Value[T]) MarshalJSON

func (o Value[T]) MarshalJSON() ([]byte, error)

MarshalJSON converts the value to a JSON value. If the Value is empty, then the JSON result is null.

func (Value[T]) MustGet

func (o Value[T]) MustGet() T

MustGet returns the current value, if there is one. If the Value is empty, then MustGet panics with ErrEmpty.

func (Value[T]) OrElse

func (o Value[T]) OrElse(v T) T

OrElse returns the stored value, if there is one. If the Value is empty, then OrElse returns the given argument.

func (Value[T]) OrElseGet

func (o Value[T]) OrElseGet(calculateFallback func() T) T

OrElseGet returns the stored value, if there is one. If the Value is empty, then OrElseGet calls the given function and returns the result. Use this instead of OrElse when calculation of the fallback value is relatively expensive. It will only be calculated when needed.

func (Value[_]) Present

func (o Value[_]) Present() bool

Present returns true if there is a value stored, false if the Value is empty.

func (Value[T]) String

func (o Value[T]) String() string

String returns the string representation of the stored value, if present. Otherwise, it returns None.

func (*Value[T]) UnmarshalJSON

func (o *Value[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON converts the given JSON value to an optional Value[T]. If the JSON value is null, then the result is empty. Otherwise, the JSON is unmarshaled in the same way values of type T are unmarshaled.

Directories

Path Synopsis
Package testing provides Gomega matchers for use with [optional.Value] values.
Package testing provides Gomega matchers for use with [optional.Value] values.

Jump to

Keyboard shortcuts

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