omitnull

package
v0.0.0-...-aa8ab86 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 License: MIT Imports: 9 Imported by: 1

Documentation

Overview

Package omitnull exposes a Val(ue) type that wraps a regular value with the ability to be 'omitted/unset' or 'null'.

Index

Constants

View Source
const (
	StateUnset state = 0
	StateNull  state = 1
	StateSet   state = 2
)

Variables

This section is empty.

Functions

func Equal

func Equal[T comparable](a, b Val[T]) bool

Equal compares two nullable values and returns true if they are equal.

Types

type Val

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

Val allows representing a value with a state of "unset", "null", or "set". Its zero value is useful and initially "unset".

func From

func From[T any](val T) Val[T]

From a value which is considered 'set'

func FromNull

func FromNull[T any](val null.Val[T]) Val[T]

FromNull constructs a value from a nullable value. This is a lossless conversion and cannot fail.

func FromOmit

func FromOmit[T any](val omit.Val[T]) Val[T]

FromOmit constructs a value from a omittable value. This is a lossless conversion and cannot fail.

func FromPtr

func FromPtr[T any](val *T) Val[T]

FromPtr creates a value from a pointer, if the pointer is null it will be 'null', if it has a value the deferenced value is stored.

func Map

func Map[A any, B any](v Val[A], fn func(A) B) Val[B]

Map transforms the value inside if it is set, else it returns a value of the same state.

func (Val[T]) Get

func (v Val[T]) Get() (T, bool)

Get the underlying value, if one exists.

func (Val[T]) GetNull

func (v Val[T]) GetNull() (null.Val[T], bool)

GetNull retrieves the value as a nullable value.

func (Val[T]) GetOmit

func (v Val[T]) GetOmit() (omit.Val[T], bool)

GetOmit retrieves the value as a omittable value.

func (Val[T]) GetOr

func (v Val[T]) GetOr(fallback T) T

GetOr gets the value or returns a fallback if the value does not exist.

func (Val[T]) GetOrZero

func (v Val[T]) GetOrZero() T

GetOrZero returns the zero value for T if the value was omitted or null.

func (Val[T]) IfNull

func (v Val[T]) IfNull(then func())

func (Val[T]) IfUnset

func (v Val[T]) IfUnset(then func())

func (Val[T]) IfValue