opt

package
v2.27.0 Latest Latest
Warning

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

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

Documentation

Overview

Package opt provides the generic Maybe type for representing optional values without pointers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Maybe

type Maybe[V any] struct {
	// contains filtered or unexported fields
}

Maybe is a simple implementation of an optional value type.

func FromPtr

func FromPtr[V any](ptr *V) Maybe[V]

FromPtr returns a Maybe that has a defined value of *ptr if ptr is non-nil, or no value if ptr is nil.

func None

func None[V any]() Maybe[V]

None returns a Maybe with no value.

func Some

func Some[V any](value V) Maybe[V]

Some returns a Maybe that has a defined value.

func (Maybe[V]) AsPtr

func (m Maybe[V]) AsPtr() *V

AsPtr returns a pointer to the value if the value is defined, or nil otherwise.

func (Maybe[V]) IsDefined

func (m Maybe[V]) IsDefined() bool

IsDefined returns true if the Maybe has a value.

func (Maybe[V]) MarshalJSON

func (m Maybe[V]) MarshalJSON() ([]byte, error)

MarshalJSON produces whatever JSON representation would normally be produced for the value if a value is defined, or a JSON null otherwise.

func (Maybe[V]) OrElse

func (m Maybe[V]) OrElse(valueIfUndefined V) V

OrElse returns the value of the Maybe if any, or the valueIfUndefined otherwise.

func (Maybe[V]) String

func (m Maybe[V]) String() string

String returns a string representation of the value, or "[none]" if undefined. The string representation of a value is either its own String() if it has such a method, or else the result of fmt.Sprintf with "%v".

func (*Maybe[V]) UnmarshalJSON

func (m *Maybe[V]) UnmarshalJSON(data []byte) error

UnmarshalJSON sets the Maybe to None[V] if the data is a JSON null, or else unmarshals a value of type V as usual and sets the Maybe to Some(value).

func (Maybe[V]) Value

func (m Maybe[V]) Value() V

Value returns the value if a value is defined, or the zero value for the type otherwise.

Jump to

Keyboard shortcuts

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