optional

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2024 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package optional contains safer code to handle optional values. This package is taken from probe-cli/internal/optional. Copyright 2024, Simone Basso

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Value

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

Value is an optional value. The zero value of this structure is equivalent to the one you get when calling None.

func None

func None[T any]() Value[T]

None constructs an empty value.

func Some

func Some[T any](value T) Value[T]

Some constructs a some value unless T is a pointer and points to nil, in which case Some is equivalent to None.

func (Value[T]) IsNone

func (v Value[T]) IsNone() bool

IsNone returns whether this Value is empty.

func (Value[T]) MarshalJSON

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

MarshalJSON implements json.Marshaler. An empty value serializes to `null` and otherwise we serialize the underluing value.

func (*Value[T]) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler. Note that a `null` JSON value always leads to an empty Value.

func (Value[T]) Unwrap

func (v Value[T]) Unwrap() T

Unwrap returns the underlying value or panics. In case of panic, the value passed to panic is an error.

func (Value[T]) UnwrapOr

func (v Value[T]) UnwrapOr(fallback T) T

UnwrapOr returns the fallback if the Value is empty.

Jump to

Keyboard shortcuts

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