optional

package module
v0.0.0-...-ffc6068 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2024 License: MIT Imports: 1 Imported by: 2

README

optional

Optionals implementation for Golang

Could help to reduce amount of annoying

result, err := doSomething()
if err != nil {
  return nil, err
}
nextResult, nextErr := doSomethingElse(result)
if nextErr != nil {
  return nil, nextErr
}
finalResult, finalErr := doSomethingOneMoreTime(nextResult)
if finalErr != nil {
  return nil, finalErr
}
return finalResult

to

return optional.MapErr(
  optional.MapErr(
    optional.OfError(doSomething()),
    doSomethingElse,
  ),
  doSomethingOneMoreTime).Get()

Documentation

Overview

Package optional provides

Index

Constants

This section is empty.

Variables

View Source
var ErrNoSuchElement = errors.New("Nothing does not contain value")

Functions

func False

func False[T any](value T) bool

func True

func True[T any](value T) bool

Types

type Error

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

Optional subtype that contains an error

func (Error[T]) Get

func (e Error[T]) Get() (T, error)

func (Error[T]) GetError

func (e Error[T]) GetError() error

func (Error[T]) If

func (e Error[T]) If(present func(T), err func(error), nothing func())

func (Error[T]) IfError

func (e Error[T]) IfError(f func(error)) Optional[T]

func (Error[T]) IfNothing

func (e Error[T]) IfNothing(func()) Optional[T]

func (Error[T]) IfPresent

func (e Error[T]) IfPresent(func(T)) Optional[T]

type Function

type Function[T any, U any] func(arg T) U

type Just

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

Optional subtype that contains a value

func (Just[T]) Filter

func (j Just[T]) Filter(p Predicate[T]) Optional[T]

func (Just[T]) Get

func (j Just[T]) Get() (T, error)

func (Just[T]) GetError

func (j Just[T]) GetError() error

func (Just[T]) If

func (j Just[T]) If(present func(T), err func(error), nothing func())

func (Just[T]) IfError

func (j Just[T]) IfError(func(error)) Optional[T]

func (Just[T]) IfNothing

func (j Just[T]) IfNothing(func()) Optional[T]

func (Just[T]) IfPresent

func (j Just[T]) IfPresent(f func(T)) Optional[T]

func (Just[T]) IsPresent

func (j Just[T]) IsPresent() bool

func (Just[T]) Map

func (j Just[T]) Map(f func(T) T) Optional[T]

func (Just[T]) OrElse

func (j Just[T]) OrElse(other T) T

type Nothing

type Nothing[T any] struct {
}

Optional subtype that contains nothing

func (Nothing[T]) Filter

func (n Nothing[T]) Filter(p Predicate[T]) Optional[T]

func (Nothing[T]) Get

func (n Nothing[T]) Get() (T, error)

func (Nothing[T]) GetError

func (n Nothing[T]) GetError() error

func (Nothing[T]) If

func (n Nothing[T]) If(present func(T), err func(error), nothing func())

func (Nothing[T]) IfError

func (n Nothing[T]) IfError(func(error)) Optional[T]

func (Nothing[T]) IfNothing

func (n Nothing[T]) IfNothing(f func()) Optional[T]

func (Nothing[T]) IfPresent

func (n Nothing[T]) IfPresent(func(T)) Optional[T]

func (Nothing[T]) IsPresent

func (n Nothing[T]) IsPresent() bool

func (Nothing[T]) Map

func (n Nothing[T]) Map(func(T) T) Optional[T]

func (Nothing[T]) OrElse

func (n Nothing[T]) OrElse(other T) T

type Optional

type Optional[T any] interface {
	IsPresent() bool
	IfPresent(func(T)) Optional[T]
	IfError(func(error)) Optional[T]
	IfNothing(func()) Optional[T]
	If(present func(T), err func(error), nothing func())
	Filter(p Predicate[T]) Optional[T]
	OrElse(other T) T
	GetError() error
	Get() (T, error)
	Map(func(T) T) Optional[T]
}

Optional is a wrapper that can contain value, nothing or error.

func Map

func Map[A any, B any](opt Optional[A], f func(A) B) Optional[B]

Applies a function to an optional if the function returns value

func MapErr

func MapErr[A any, B any](opt Optional[A], f func(A) (B, error)) Optional[B]

Applies a function to an optional if the function returns value and error

func Of

func Of[T any](value T) Optional[T]

Constructs an Optional from a value type

func OfError

func OfError[T any](value T, err error) Optional[T]

Constructs an Optional from a value and error (e.g. to wrap a function result)

func OfErrorNullable

func OfErrorNullable[T any](value *T, err error) Optional[*T]

Constructs an Optional from a pointer type (Nothing if value is nil)

func OfNullable

func OfNullable[T any](value *T) Optional[*T]

Constructs an Optional from a pointer type (Nothing if value is nil)

type Predicate

type Predicate[T any] Function[T, bool]

Jump to

Keyboard shortcuts

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