sliceund

package
v1.0.0-alpha1 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2024 License: Apache-2.0 Imports: 8 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Und

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

Und[T] is a type that can express a value (`T`), empty (`null`), or absent (`undefined`).

Und[T] can be a skippable struct field with omitempty option of `encoding/json`.

Although it exposes its internal data structure, you should not mutate internal data. Using map[T]U, []T or json.RawMessage as base type is only allowed hacks to make it skippable by `json:",omitempty" option, without losing freedom of adding methods. Any method implemented on Und[T] assumes it has only either 0 or 1 element. So mutating an Und[T] object, e.g. appending it to have 2 or more elements, causes undefined behaviors and not promised to behave same between versions.

Und[T] is intended to behave much like a simple variable. There are only 2 way to change its internal state. Assigning a value of corresponding state to the variable you intend to change. Or calling UnmarshalJSON on an addressable Und[T].

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.

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 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 (Und[T]) Clone

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

Clone clones u. This is only a copy-by-assign unless T implements Cloner[T].

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]) Equal

func (u Und[T]) Equal(other Und[T]) bool

Equal implements Equality[Und[T]]. Equal panics if T is uncomparable and does not implement Equality[T].

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" skips this field while encoding if IsZero returns true.

func (Und[T]) LogValue

func (o 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]) MarshalJSONV2

func (u Und[T]) MarshalJSONV2(enc *jsontext.Encoder, opts jsonv2.Options) error

MarshalJSONV2 implements jsonv2.MarshalerV2.

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 (o Und[T]) SqlNull() sql.Null[T]

SqlNull converts o into sql.Null[T].

func (*Und[T]) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler.

func (*Und[T]) UnmarshalJSONV2

func (u *Und[T]) UnmarshalJSONV2(dec *jsontext.Decoder, opts jsonv2.Options) error

UnmarshalJSONV2 implements jsonv2.UnmarshalerV2.

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