opt

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package opt defines types for working with opt values, inspired by Java's `Optional` class. An opt value either contains a value of a certain type (Some), or it is empty (None). This package provides a way to safely handle such values without the risk of null ptr dereferences. It includes functions for creating opt values, checking if they are present, and retrieving the value if it is present.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type None

type None[V any] struct {
}

None represents the absence of a value.

func (None[V]) Filter

func (n None[V]) Filter(_ func(V) bool) Optional[V]

func (None[V]) Get

func (n None[V]) Get() (V, bool)

func (None[V]) GetOrDefault

func (n None[V]) GetOrDefault(defaultValue V) V

func (None[V]) GetOrFunc

func (n None[V]) GetOrFunc(f func() V) V

func (None[V]) GetOrZero

func (n None[V]) GetOrZero() V

func (None[V]) IfPresent

func (n None[V]) IfPresent(_ func(V)) bool

func (None[V]) IfPresentElse

func (n None[V]) IfPresentElse(_ func(V), f func()) bool

func (None[V]) Present

func (n None[V]) Present() bool

func (None[V]) String

func (n None[V]) String() string

func (None[V]) Tap

func (n None[V]) Tap(_ func(V)) Optional[V]

type Optional

type Optional[V any] interface {
	fmt.Stringer

	// Present returns true if the Optional contains a value, false otherwise.
	Present() bool

	// Get returns the value contained in the Optional and an indicator of whether the Optional is empty.
	// If the Optional is empty, the value returned is the zero value of type V.
	Get() (V, bool)

	// GetOrZero returns the value contained in the Optional, or the zero value of type V if the Optional is empty.
	GetOrZero() V

	// GetOrDefault returns the value contained in the Optional, or the provided default value if the Optional is empty.
	GetOrDefault(defaultValue V) V

	// GetOrFunc returns the value contained in the Optional, or the result of calling the provided function if the Optional is empty.
	GetOrFunc(func() V) V

	// IfPresent calls the provided function with the value contained in the Optional if the Optional is not empty.
	// Returns true if the function was called, false otherwise.
	IfPresent(func(V)) bool

	// IfPresentElse calls the first function with the value contained in the Optional if the Optional is not empty, or the second function otherwise.
	// Returns true if the first function was called, false otherwise.
	IfPresentElse(func(V), func()) bool

	// Filter returns an Optional containing the value contained in the Optional if the provided predicate returns true for that value.
	// If the Optional is empty, an empty Optional is returned.
	Filter(func(V) bool) Optional[V]

	// Tap calls the provided function with the value contained in the Optional if the Optional is not empty.
	// Returns the Optional itself for chaining.
	Tap(func(V)) Optional[V]
}

Optional is a generic type that takes one type parameter V and represents a value that may or may not be present. It is similar to Java's Optional type.

func Any

func Any[V any](options ...Optional[V]) Optional[V]

Any returns the first non-empty Optional from the provided options. If all options are empty, an empty Optional is returned.

func Empty

func Empty[V any]() Optional[V]

Empty returns an empty Optional (None).

func Map

func Map[V, U any](o Optional[V], mapper func(V) U) Optional[U]

Map returns an Optional containing the result of applying the provided mapper function to the value contained in the provided Optional. If the provided Optional is empty, an empty Optional is returned; otherwise, a non-empty Optional is returned.

Example usage:

o := opt.Map(
  opt.Of(1),
  func(i int) string { return fmt.Sprintf("%d", i) },
) // opt.Some("1")

o = opt.Map(
  opt.Empty[int](),
  func(i int) string { return fmt.Sprintf("%d", i) },
) // opt.None()

func Maybe

func Maybe[V any](value V, ok bool) Optional[V]

Maybe returns a non-empty Optional wrapping the provided value if ok is true; an empty Optional, otherwise.

func MaybeMap

func MaybeMap[V, U any](o Optional[V], mapper func(V) (U, bool)) Optional[U]

MaybeMap returns an Optional containing the result of applying the provided mapper function to the value contained in the provided Optional. If the provided Optional is empty, or if the mapper returns false, an empty Optional is returned; otherwise, a non-empty Optional is returned.

Example usage:

o := opt.MaybeMap(
  opt.Of(1),
  func(i int) (string, bool) { return fmt.Sprintf("%d", i), true },
) // opt.Some("1")

o = opt.MaybeMap(
  opt.Of(1),
  func(i int) (string, bool) { return fmt.Sprintf("%d", i), false },
) // opt.None()

o = opt.MaybeMap(
  opt.Empty[int](),
  func(i int) (string, bool) { return fmt.Sprintf("%d", i), true },
) // opt.None()

func Of

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

Of returns a non-empty Optional (Some) wrapping the provided value.

func OptionalMap

func OptionalMap[V, U any](o Optional[V], mapper func(V) Optional[U]) Optional[U]

OptionalMap returns an Optional containing the result of applying the provided mapper function to the value contained in the provided Optional. If the provided Optional is empty, of if the mapper returns an empty Optional, an empty Optional is returned; otherwise, a non-empty Optional is returned.

Example usage:

o := opt.OptionalMap(
  opt.Of(1),
  func(i int) opt.Optional[string] { return opt.Of(fmt.Sprintf("%d", i)) },
) // opt.Some("1")

o = opt.OptionalMap(
  opt.Of(1),
  func(i int) opt.Optional[string] { return opt.Empty[string]() },
) // opt.None()

o = opt.OptionalMap(
  opt.Empty[int](),
  func(i int) opt.Optional[string] { return opt.Of(fmt.Sprintf("%d", i)) },
) // opt.None()

type Some

type Some[V any] struct {
	Value V
}

Some represents the presence of a value.

func (Some[V]) Filter

func (s Some[V]) Filter(f func(V) bool) Optional[V]

func (Some[V]) Get

func (s Some[V]) Get() (V, bool)

func (Some[V]) GetOrDefault

func (s Some[V]) GetOrDefault(_ V) V

func (Some[V]) GetOrFunc

func (s Some[V]) GetOrFunc(_ func() V) V

func (Some[V]) GetOrZero

func (s Some[V]) GetOrZero() V

func (Some[V]) IfPresent

func (s Some[V]) IfPresent(f func(V)) bool

func (Some[V]) IfPresentElse

func (s Some[V]) IfPresentElse(f func(V), _ func()) bool

func (Some[V]) Present

func (s Some[V]) Present() bool

func (Some[V]) String

func (s Some[V]) String() string

func (Some[V]) Tap

func (s Some[V]) Tap(f func(V)) Optional[V]

Jump to

Keyboard shortcuts

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