opt

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2025 License: MIT Imports: 3 Imported by: 0

README

opt - handling optional data

Go Reference

This package provides types and functions for dealing with optional data.

All types implement json.Marshaler, json.Unmarshaler, driver.Valuer and sql.Scanner interfaces.

Types in textopt package additionally implement encoding.TextMarshaler and encoding.TextUnmarshaler interfaces.

Null[T]

Null represents an optional value that becomes valid when it is assigned a value other than nil.

var v opt.Null[string]

json.Unmarshal(&v, []byte(`null`))
fmt.Printf("%q %v\n", v.V, v.Valid) // output: "" false

json.Unmarshal(&v, []byte(`""`))
fmt.Printf("%q %v\n", v.V, v.Valid) // output: "" true

json.Unmarshal(&v, []byte(`"123"`))
fmt.Printf("%q %v\n", v.V, v.Valid) // output: "123" true

Zero[T]

Zero represents an optional value that becomes valid when it is assigned a value other than nil or zero.

Value is considered zero according to rules:

  1. If type T has an IsZero() bool method, that returns true for the value.
  2. Otherwise, the value is zero if it is the zero value for type T.
var v opt.Zero[string]

json.Unmarshal(&v, []byte(`null`))
fmt.Printf("%q %v\n", v.V, v.Valid) // output: "" false

json.Unmarshal(&v, []byte(`""`))
fmt.Printf("%q %v\n", v.V, v.Valid) // output: "" false

json.Unmarshal(&v, []byte(`"123"`))
fmt.Printf("%q %v\n", v.V, v.Valid) // output: "123" true

Undefined[T]

Undefined represents an optional value that becomes valid when it is assigned any value.

var v opt.Undefined[string]

json.Unmarshal(&v, []byte(`null`))
fmt.Printf("%q %v\n", v.V, v.Valid) // output: "" true

json.Unmarshal(&v, []byte(`""`))
fmt.Printf("%q %v\n", v.V, v.Valid) // output: "" true

json.Unmarshal(&v, []byte(`"123"`))
fmt.Printf("%q %v\n", v.V, v.Valid) // output: "123" true

Documentation

Overview

Package opt provides types and functions for dealing with optional data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Empty added in v0.4.0

type Empty[T comparable] struct {
	V     T
	Valid bool
}

Empty represents an optional value that becomes valid when it is assigned a value other than nil or zero. Value is considered zero if it is the zero value for type T.

func EmptyFrom added in v0.4.0

func EmptyFrom[T comparable](value T) Empty[T]

func EmptyFromFunc added in v0.4.0

func EmptyFromFunc[T comparable, U any](value *U, f func(U) T) Empty[T]

func EmptyFromFuncPtr added in v0.4.0

func EmptyFromFuncPtr[T comparable, U any](value *U, f func(*U) T) Empty[T]

func EmptyFromPtr added in v0.4.0

func EmptyFromPtr[T comparable](value *T) Empty[T]

func NewEmpty added in v0.4.0

func NewEmpty[T comparable](value T, valid bool) Empty[T]

func (Empty[T]) Get added in v0.4.0

func (v Empty[T]) Get() (value T, ok bool)

func (Empty[T]) IsZero added in v0.4.0

func (v Empty[T]) IsZero() bool

func (Empty[T]) MarshalJSON added in v0.4.0

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

func (Empty[T]) Or added in v0.4.0

func (v Empty[T]) Or(value T) T

func (*Empty[T]) Ptr added in v0.4.0

func (v *Empty[T]) Ptr() *T

func (*Empty[T]) Reset added in v0.4.0

func (v *Empty[T]) Reset()

func (*Empty[T]) Scan added in v0.4.0

func (v *Empty[T]) Scan(value any) error

func (*Empty[T]) Set added in v0.4.0

func (v *Empty[T]) Set(value T)

func (Empty[T]) ToSQL added in v0.4.0

func (v Empty[T]) ToSQL() sql.Null[T]

func (*Empty[T]) UnmarshalJSON added in v0.4.0

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

func (Empty[T]) Value added in v0.4.0

func (v Empty[T]) Value() (driver.Value, error)

type IsZeroer added in v0.4.0

type IsZeroer interface {
	IsZero() bool
}

IsZeroer represents types that can report whether their value is zero.

type Null

type Null[T any] struct {
	V     T
	Valid bool
}

Null represents an optional value that becomes valid when it is assigned a value other than nil.

func NewNull

func NewNull[T any](value T, valid bool) Null[T]

func NullFrom

func NullFrom[T any](value T) Null[T]

func NullFromFunc

func NullFromFunc[T, U any](value *U, f func(U) T) Null[T]

func NullFromFuncPtr

func NullFromFuncPtr[T, U any](value *U, f func(*U) T) Null[T]

func NullFromPtr

func NullFromPtr[T any](value *T) Null[T]

func (Null[T]) Get

func (v Null[T]) Get() (value T, ok bool)

func (Null[T]) IsZero

func (v Null[T]) IsZero() bool

func (Null[T]) MarshalJSON

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

func (Null[T]) Or

func (v Null[T]) Or(value T) T

func (*Null[T]) Ptr

func (v *Null[T]) Ptr() *T

func (*Null[T]) Reset

func (v *Null[T]) Reset()

func (*Null[T]) Scan

func (v *Null[T]) Scan(value any) error

func (*Null[T]) Set

func (v *Null[T]) Set(value T)

func (Null[T]) ToSQL

func (v Null[T]) ToSQL() sql.Null[T]

func (*Null[T]) UnmarshalJSON

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

func (Null[T]) Value

func (v Null[T]) Value() (driver.Value, error)

type Undefined

type Undefined[T any] struct {
	V     T
	Valid bool
}

Undefined represents an optional value that becomes valid when it is assigned any value.

func NewUndefined

func NewUndefined[T any](value T, valid bool) Undefined[T]

func UndefinedFrom

func UndefinedFrom[T any](value T) Undefined[T]

func UndefinedFromFunc

func UndefinedFromFunc[T, U any](value *U, f func(U) T) Undefined[T]

func UndefinedFromFuncPtr

func UndefinedFromFuncPtr[T, U any](value *U, f func(*U) T) Undefined[T]

func UndefinedFromPtr

func UndefinedFromPtr[T any](value *T) Undefined[T]

func (Undefined[T]) Get

func (v Undefined[T]) Get() (value T, ok bool)

func (Undefined[T]) IsZero

func (v Undefined[T]) IsZero() bool

func (Undefined[T]) MarshalJSON

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

func (Undefined[T]) Or

func (v Undefined[T]) Or(value T) T

func (*Undefined[T]) Ptr

func (v *Undefined[T]) Ptr() *T

func (*Undefined[T]) Reset

func (v *Undefined[T]) Reset()

func (*Undefined[T]) Scan

func (v *Undefined[T]) Scan(value any) error

func (*Undefined[T]) Set

func (v *Undefined[T]) Set(value T)

func (Undefined[T]) ToSQL

func (v Undefined[T]) ToSQL() sql.Null[T]

func (*Undefined[T]) UnmarshalJSON

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

func (Undefined[T]) Value

func (v Undefined[T]) Value() (driver.Value, error)

type Zero

type Zero[T IsZeroer] struct {
	V     T
	Valid bool
}

Zero represents an optional value that becomes valid when it is assigned a value other than nil or zero. Value is considered zero if IsZero method of type T returns true for the value.

func NewZero

func NewZero[T IsZeroer](value T, valid bool) Zero[T]

func ZeroFrom

func ZeroFrom[T IsZeroer](value T) Zero[T]

func ZeroFromFunc

func ZeroFromFunc[T IsZeroer, U any](value *U, f func(U) T) Zero[T]

func ZeroFromFuncPtr

func ZeroFromFuncPtr[T IsZeroer, U any](value *U, f func(*U) T) Zero[T]

func ZeroFromPtr

func ZeroFromPtr[T IsZeroer](value *T) Zero[T]

func (Zero[T]) Get

func (v Zero[T]) Get() (value T, ok bool)

func (Zero[T]) IsZero

func (v Zero[T]) IsZero() bool

func (Zero[T]) MarshalJSON

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

func (Zero[T]) Or

func (v Zero[T]) Or(value T) T

func (*Zero[T]) Ptr

func (v *Zero[T]) Ptr() *T

func (*Zero[T]) Reset

func (v *Zero[T]) Reset()

func (*Zero[T]) Scan

func (v *Zero[T]) Scan(value any) error

func (*Zero[T]) Set

func (v *Zero[T]) Set(value T)

func (Zero[T]) ToSQL

func (v Zero[T]) ToSQL() sql.Null[T]

func (*Zero[T]) UnmarshalJSON

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

func (Zero[T]) Value

func (v Zero[T]) Value() (driver.Value, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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