Documentation
¶
Index ¶
- func Equal[T comparable](l, r Elastic[T]) bool
- func UndValidate[T validate.UndValidator](e Elastic[T], skip ...int) error
- type Elastic
- func Clone[T comparable](e Elastic[T]) Elastic[T]
- func FromOptionSeq[T any](seq iter.Seq[option.Option[T]]) Elastic[T]
- func FromOptions[T any](options ...option.Option[T]) Elastic[T]
- func FromPointer[T any](t *T) Elastic[T]
- func FromPointers[T any](ps ...*T) Elastic[T]
- func FromUnd[T any, Opts ~[]option.Option[T]](u und.Und[Opts]) Elastic[T]
- func FromValue[T any](t T) Elastic[T]
- func FromValues[T any](ts ...T) Elastic[T]
- func Map[T, U any](e Elastic[T], f func(t T) U) Elastic[U]
- func Null[T any]() Elastic[T]
- func Undefined[T any]() Elastic[T]
- func WrapPointer[T any](t *T) Elastic[*T]
- func WrapPointers[T any](ps ...*T) Elastic[*T]
- func (e Elastic[T]) CloneFunc(cloneT func(T) T) Elastic[T]
- func (e Elastic[T]) EqualFunc(other Elastic[T], cmp func(i, j T) bool) bool
- func (e Elastic[T]) HasNull() bool
- func (e Elastic[T]) IsDefined() bool
- func (e Elastic[T]) IsNull() bool
- func (e Elastic[T]) IsUndefined() bool
- func (e Elastic[T]) IsZero() bool
- func (e Elastic[T]) Iter() iter.Seq[option.Option[option.Options[T]]]
- func (e Elastic[T]) Len() int
- func (e Elastic[T]) LogValue() slog.Value
- func (e Elastic[T]) Map(f func(und.Und[option.Options[T]]) und.Und[option.Options[T]]) Elastic[T]
- func (u Elastic[T]) MarshalJSON() ([]byte, error)
- func (e Elastic[T]) MarshalXML(enc *xml.Encoder, start xml.StartElement) error
- func (e Elastic[T]) PlainMultiple() []*Tdeprecated
- func (e Elastic[T]) PlainSingle() *Tdeprecated
- func (e Elastic[T]) Pointer() *T
- func (e Elastic[T]) Pointers() []*T
- func (e Elastic[T]) State() und.State
- func (e Elastic[T]) UndCheck() error
- func (e Elastic[T]) UndValidate() error
- func (e *Elastic[T]) UnmarshalJSON(data []byte) error
- func (e *Elastic[T]) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
- func (e Elastic[T]) Unwrap() und.Und[option.Options[T]]
- func (e Elastic[T]) Value() T
- func (e Elastic[T]) ValueMultiple() []Tdeprecated
- func (e Elastic[T]) ValueSingle() Tdeprecated
- func (e Elastic[T]) Values() []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Equal ¶
func Equal[T comparable](l, r Elastic[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 Elastic.EqualFunc instead.
Equal is a specialized slices.Equal where it also considers value state of l and r.
func UndValidate ¶
func UndValidate[T validate.UndValidator](e Elastic[T], skip ...int) error
Types ¶
type Elastic ¶
type Elastic[T any] struct { // contains filtered or unexported fields }
Elastic[T] is a type that can express *undefined* | *null* | T | [](null | T). Elastic[T] implements json.Unmarshaler so that it can be unmarshaled from all of those type. However it always marshaled into an array of JSON value that corresponds to T.
Elastic[T] is defined mainly to create/consume JSON documents stored in Elasticsearch. It may also be useful for parsing hand-written configuration files.
Elastic[T] implements IsZero. For Go 1.24 or later version, *undefined* Elastic[T] struct fields are omitted by encoding/json if `json:",omitzero"` option is attached to those fields. For Go 1.23 or older version, instead you can use the sliceund/elastic variant with `json:",omitempty"` option.
func FromOptions ¶
FromOptions converts variadic option.Option[T] values into Elastic[T]. options is retained by the returned value.
func FromPointer ¶
FromPointer converts nil to undefined Elastic[T], or defined one whose internal value is dereferenced t.
If you need to keep t as pointer, use WrapPointer instead.
func FromPointers ¶
FromPointers converts variadic *T values into an Elastic[T], treating nil as None[T], and non-nil as Some[T].
If you need to keep t-s as pointer, use WrapPointers instead.
func FromUnd ¶
FromUnd converts und.Und[option.Options[T]] into Elastic[T].
The internal value of u is retained by the returned value.
func FromValues ¶
FromValues converts variadic T values into an Elastic[T].
func Map ¶
Map returns a new Elastic value whose internal value is mapped by f.
Be cautious that f will only be applied to some value; null values remain as null.
func WrapPointer ¶
WrapPointer converts *T into Elastic[*T]. The elastic value is defined if t is non nil, undefined otherwise.
If you want t to be dereferenced, use FromPointer instead.
func WrapPointers ¶
FromPointers converts variadic *T values into an Elastic[*T], treating nil as None[*T], and non-nil as Some[*T].
If you need t-s to be dereferenced, use FromPointers instead.
func (Elastic[T]) EqualFunc ¶
EqualFunc reports whether two Elastic values are equal. EqualFunc checks state of both. If both state does not match, it returns false. If both are "defined" and lengths of their internal value match, it then checks equality of their value by cmp. It returns true if they are equal.
func (Elastic[T]) IsDefined ¶
IsDefined returns true if e is a defined Elastic[T], which includes a slice with no element.
func (Elastic[T]) IsUndefined ¶
IsUndefined returns true if e is an undefined Elastic[T].
func (Elastic[T]) Iter ¶
Iter returns an iterator over the internal option. If e is undefined, the iterator yields nothing, otherwise the internal option.
func (Elastic[T]) Map ¶
Map returns a new Elastic[T] whose internal value is e's mapped by f.
The internal slice of e is capped to its length before passed to f.
func (Elastic[T]) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (Elastic[T]) MarshalXML ¶
MarshalXML implements xml.Marshaler.
func (Elastic[T]) PlainMultiple
deprecated
func (e Elastic[T]) PlainMultiple() []*T
Deprecated: use Pointers
func (Elastic[T]) PlainSingle
deprecated
func (e Elastic[T]) PlainSingle() *T
Deprecated: use Pointer.
func (Elastic[T]) Pointer ¶
func (e Elastic[T]) Pointer() *T
Pointer returns a first value of its internal option slice as *T if e is defined.
Pointer returns nil if
- e is not defined
- e has no element
- e's first element is None.
func (Elastic[T]) Pointers ¶
func (e Elastic[T]) Pointers() []*T
Pointer returns its internal option slice as []*T if e is defined.
func (Elastic[T]) UndValidate ¶
func (*Elastic[T]) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Elastic[T]) UnmarshalXML ¶
UnmarshalXML implements xml.Unmarshaler.
func (Elastic[T]) Value ¶
func (e Elastic[T]) Value() T
Value returns a first value of its internal option slice if e is defined. Otherwise it returns zero value for T.
func (Elastic[T]) ValueMultiple
deprecated
func (e Elastic[T]) ValueMultiple() []T
Deprecated: use Values.
func (Elastic[T]) ValueSingle
deprecated
func (e Elastic[T]) ValueSingle() T
Deprecated: use Value.