sliceund

package
v1.0.0-alpha8 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2025 License: Apache-2.0 Imports: 9 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal[T comparable](l, r Und[T]) bool

Equal tests equality of l and r then returns true if they are equal, false otherwise. For those types that are comparable but need special tests, e.g. time.Time, you should use Und.EqualFunc instead.

func UndValidate

func UndValidate[T validate.UndValidator](u Und[T]) error

Types

type SqlNull

type SqlNull[T any] struct {
	Und[T]
}

SqlNull[T] adapts Und[T] to sql.Scanner and driver.Valuer.

func (*SqlNull[T]) Scan

func (n *SqlNull[T]) Scan(src any) error

Scan implements sql.Scanner.

If T or *T implements sql.Scanner, the implementation is used. Otherwise, SqlNull[T] falls back to sql.Null[T] as sql.Scanner.

func (SqlNull[T]) Value

func (n SqlNull[T]) Value() (driver.Value, error)

Value implements driver.Valuer.

If T or *T implements driver.Valuer, the implementation is used. In this respect, T should not be a pointer type or Und[T] should not store nil value. Otherwise, SqlNull[T] falls back to sql.Null[T] as driver.Valuer.

type Und

type Und[T any] []option.Option[T]

Und[T] is a slice-based variant of und.Und.

Und[T] exposes same set of methods as und.Und and can be used in almost same way. Although it exposes its internal value, it is only intended to let some encoders, e.g. encoding/json, etc, see it as omittable value. You should manipulate the value only through methods.

*undefined* Und[T] struct fields are omitted by encoding/json if either or both of `json:",omitempty"` and `json:",omitzero"` (for Go 1.24 or later) options are attached to those fields.

func Clone

func Clone[T comparable](u Und[T]) Und[T]

Clone clones u.

func Defined

func Defined[T any](t T) Und[T]

Defined returns a defined Und[T] which contains t.

func FromOption

func FromOption[T any](opt option.Option[option.Option[T]]) Und[T]

FromOptions converts opt into an Und[T]. opt is retained by the returned value.

func FromPointer

func FromPointer[T any](v *T) Und[T]

FromPointer converts *T into Und[T]. If v is nil, it returns an undefined Und. Otherwise, it returns Defined[T] whose value is the dereferenced v.

If you need to keep t as pointer, use WrapPointer instead.

func FromSqlNull

func FromSqlNull[T any](v sql.Null[T]) Und[T]

FromSqlNull converts a valid sql.Null[T] to a defined Und[T] and invalid one into a null Und[].

func FromUnd

func FromUnd[T any](u und.Und[T]) Und[T]

FromUnd converts non-slice version of Und[T] into Und[T].

func Map

func Map[T, U any](u Und[T], f func(t T) U) Und[U]

Map returns a new Und value whose internal value is mapped by f.

func Null

func Null[T any]() Und[T]

Null returns a null Und[T].

func Undefined

func Undefined[T any]() Und[T]

Undefined returns an undefined Und[T].

func WrapPointer

func WrapPointer[T any](t *T) Und[*T]

WrapPointer converts *T into Und[*T]. The und value is defined if t is non nil, undefined otherwise.

If you want t to be dereferenced, use FromPointer instead.

func (Und[T]) CloneFunc

func (u Und[T]) CloneFunc(cloneT func(T) T) Und[T]

CloneFunc clones u using the cloneT functions.

func (Und[T]) DoublePointer

func (u Und[T]) DoublePointer() **T

DoublePointer returns nil if u is undefined, &(*T)(nil) if null, the internal value if defined.

func (Und[T]) EqualFunc

func (u Und[T]) EqualFunc(v Und[T], cmp func(i, j T) bool) bool

EqualFunc reports whether two Und values are equal. EqualFunc checks state of both. If both state does not match, it returns false. If both are "defined" state, then checks equality of their value by cmp, then returns true if they are equal.

func (Und[T]) IsDefined

func (u Und[T]) IsDefined() bool

IsDefined returns true if u contains a value. Through this method, you can check validity of the value returned from Value method.

func (Und[T]) IsNull

func (u Und[T]) IsNull() bool

IsNull returns true if u is a null value, otherwise false.

func (Und[T]) IsUndefined

func (u Und[T]) IsUndefined() bool

IsUndefined returns true if u is an undefined value, otherwise false.

func (Und[T]) IsZero

func (u Und[T]) IsZero() bool

IsZero is an alias for IsUndefined. Using `json:",omitzero"` option with "github.com/go-json-experiment/json" omits this field while encoding if IsZero returns true.

func (Und[T]) Iter

func (u Und[T]) Iter() iter.Seq[option.Option[T]]

Iter returns an iterator over the internal option. If u is undefined, the iterator yields nothing, otherwise the internal option.

func (Und[T]) LogValue

func (u Und[T]) LogValue() slog.Value

LogValue implements slog.LogValuer.

func (Und[T]) Map

func (u Und[T]) Map(f func(option.Option[option.Option[T]]) option.Option[option.Option[T]]) Und[T]

Map returns a new Und[T] whose internal value is u's mapped by f.

func (Und[T]) MarshalJSON

func (u Und[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Und[T]) MarshalXML

func (o Und[T]) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML implements xml.Marshaler.

func (Und[T]) Pointer

func (u Und[T]) Pointer() *T

Pointer returns u's internal value as a pointer.

func (Und[T]) SqlNull

func (u Und[T]) SqlNull() sql.Null[T]

SqlNull converts o into sql.Null[T].

func (Und[T]) State

func (u Und[T]) State() und.State

State returns u's value state.

func (Und[T]) Und

func (u Und[T]) Und() und.Und[T]

Und converts u into non-slice version Und[T].

func (Und[T]) UndCheck

func (u Und[T]) UndCheck() error

func (Und[T]) UndValidate

func (u Und[T]) UndValidate() error

func (*Und[T]) UnmarshalJSON

func (u *Und[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Und[T]) UnmarshalXML

func (o *Und[T]) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements xml.Unmarshaler.

func (Und[T]) Unwrap

func (u Und[T]) Unwrap() option.Option[option.Option[T]]

Unwrap converts u to a nested options.

func (Und[T]) Value

func (u Und[T]) Value() T

Value returns an internal value.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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