elastic

package
v1.0.0-alpha3 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2024 License: Apache-2.0 Imports: 8 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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] is comparable if T is comparable. And it can be copied by assign.

Elastic[T] implements IsZero and can be skippable struct fields when marshaled through appropriate marshalers, e.g. "github.com/go-json-experiment/json/jsontext" with omitzero option set to the field, or "github.com/json-iterator/go" with omitempty option to the field and an appropriate extension.

If you need to stick with encoding/json v1, you can use github.com/ngicks/und/sliceund/elastic, a slice based version of Elastic[T] whish is already skppable by v1.

func FromOptions

func FromOptions[Opts ~[]option.Option[T], T any](options Opts) Elastic[T]

FromOptions converts slice of option.Option[T] into Elastic[T]. options is retained by the returned value.

func FromPointer

func FromPointer[T any](t *T) Elastic[T]

FromPointer converts nil to undefined Elastic[T], or defined one whose internal value is dereferenced t.

func FromPointers

func FromPointers[T any](ps []*T) Elastic[T]

FromPointers converts []*T into an Elastic[T], treating nil as None[T], and non-nil as Some[T].

func FromUnd

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

FromUnd converts und.Und[option.Options[T]] into Elastic[T].

The internal value of u is retained by the returned value.

func FromValue

func FromValue[T any](t T) Elastic[T]

func FromValues

func FromValues[T any](ts []T) Elastic[T]

FromValues converts []T into an Elastic[T].

func Null

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

Null returns a null Elastic[T].

func Undefined

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

Undefined returns an undefined Elastic[T].

func (Elastic[T]) CheckUnd

func (e Elastic[T]) CheckUnd() error

func (Elastic[T]) Clone

func (e Elastic[T]) Clone() Elastic[T]

Clone implements option.Cloner[Elastic[T]].

Clone clones its internal option.Option slice by copy. Or if T implements Cloner[T], each element is cloned.

func (Elastic[T]) Equal

func (e Elastic[T]) Equal(other Elastic[T]) bool

Equal implements option.Equality[Elastic[T]].

Equal panics if T is uncomparable.

func (Elastic[T]) HasNull

func (e Elastic[T]) HasNull() bool

HasNull reports e is defined value has null in ins value.

func (Elastic[T]) IsDefined

func (e Elastic[T]) IsDefined() bool

IsDefined returns true if e is a defined Elastic[T], which includes a slice with no element.

func (Elastic[T]) IsNull

func (e Elastic[T]) IsNull() bool

IsNull returns true if e is a null Elastic[T].

func (Elastic[T]) IsUndefined

func (e Elastic[T]) IsUndefined() bool

IsUndefined returns true if e is an undefined Elastic[T].

func (Elastic[T]) IsZero

func (e Elastic[T]) IsZero() bool

IsZero is an alias for IsUndefined.

func (Elastic[T]) Len

func (e Elastic[T]) Len() int

Len returns length of values.

func (Elastic[T]) LogValue

func (e Elastic[T]) LogValue() slog.Value

LogValue implements slog.LogValuer.

func (Elastic[T]) Map

func (e Elastic[T]) Map(f func(und.Und[option.Options[T]]) und.Und[option.Options[T]]) Elastic[T]

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

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

MarshalJSON implements json.Marshaler.

func (Elastic[T]) MarshalJSONV2

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

MarshalJSONV2 implements jsonv2.MarshalerV2.

func (Elastic[T]) MarshalXML

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

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

func (e *Elastic[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Elastic[T]) UnmarshalXML

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

UnmarshalXML implements xml.Unmarshaler.

func (Elastic[T]) Unwrap

func (e Elastic[T]) Unwrap() und.Und[option.Options[T]]

Unwrap unwraps e.

func (Elastic[T]) ValidateUnd

func (e Elastic[T]) ValidateUnd() error

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.

func (Elastic[T]) Values

func (e Elastic[T]) Values() []T

Values returns internal option slice as plain []T.

If e is not defined, it returns nil. Any None value in its internal option slice will be converted to zero value of T.

Jump to

Keyboard shortcuts

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