opt

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package opt implements the Of monad for Go.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrScanOverflow         = errors.New("value overflows target type")
	ErrScanNegativeUnsigned = errors.New("cannot scan negative value into unsigned type")
	ErrScanType             = errors.New("incompatible types")
	ErrScanUnsupported      = errors.New("unsupported target type")
)

Functions

This section is empty.

Types

type Of

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

Of[T] is either a present value (Some) or absent (Nil).

func Morph

func Morph[T, U any](o Of[T], f func(T) Of[U]) Of[U]

Morph maps Of[T] to Of[U] using a function that returns an option. Returns Nil[U] if the option is invalid or if f returns Nil.

func Nil

func Nil[T any]() (_ Of[T])

Nil returns the zero Of[T], representing absence.

func Some

func Some[T comparable](t T) Of[T]

Some returns Of[T], valid only if t is non-zero.

func SomeAny

func SomeAny[T any](t T) Of[T]

SomeAny returns an always-valid Of[T], bypassing the zero check.

func SomeAnyReflect

func SomeAnyReflect[T any](t T) Of[T]

SomeAnyReflect returns Of[T] using reflection or known interfaces to determine validity. Prefer Some or SomeAny when possible; reflection is 25–50× slower.

func SomePtr

func SomePtr[T any](t *T) Of[T]

SomePtr returns Of[T] from a pointer. Valid if and only if the pointer is non-nil.

func To

func To[T, U any](o Of[T], tf func(T) U) Of[U]

To maps Of[T] to Of[U] by applying tf to the value. Returns Nil[U] if the option is invalid.

func (Of[T]) FlatMap

func (o Of[T]) FlatMap(f func(T) Of[T]) Of[T]

FlatMap applies f to the value if valid, propagating Nil otherwise.

func (Of[T]) Fold

func (o Of[T]) Fold(onNil func() T, onVal func(T) T) T

Fold returns onVal(t) if valid, otherwise onNil().

func (Of[T]) MarshalJSON

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

MarshalJSON implements [json.Marshaler]. Marshals the value if valid, otherwise marshals null.

func (Of[T]) Ptr

func (o Of[T]) Ptr() *T

Ptr returns a pointer to the value, or nil if invalid.

func (*Of[T]) Scan

func (o *Of[T]) Scan(src any) error

Scan implements sql.Scanner for Of[T].

func (*Of[T]) UnmarshalJSON

func (o *Of[T]) UnmarshalJSON(ba []byte) error

UnmarshalJSON implements [json.Unmarshaler].

func (Of[T]) Unpack

func (o Of[T]) Unpack() (T, bool)

Unpack returns the value and validity flag.

func (Of[T]) Val

func (o Of[T]) Val() T

Val returns the held value. Zero if invalid.

func (Of[T]) Valid

func (o Of[T]) Valid() bool

Valid reports whether the option holds a value.

func (Of[T]) Value

func (o Of[T]) Value() (driver.Value, error)

Value implements driver.Valuer for Of[T].

type ScanError

type ScanError struct {
	Src    any
	Target string
	Cause  error
}

ScanError describes an error that occurred while scanning a SQL value.

func (*ScanError) Error

func (e *ScanError) Error() string

func (*ScanError) Unwrap

func (e *ScanError) Unwrap() error

Jump to

Keyboard shortcuts

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