Documentation
¶
Overview ¶
Package delta provides collection types that track their own changes.
A delta.Slice[T] wraps an ordinary []T and records which elements have been added and which have been deleted relative to the values it started with, so a caller can compute a minimal diff and apply only what actually moved. The element type is constrained to comparable because membership checks are what drive that bookkeeping.
Only the values are serialized. The Added and Deleted sets are marked json:"-" and bson:"-" on purpose: they describe one in-memory editing session, not durable state, so a value that round-trips through storage comes back with its current contents and an empty change set.
Index ¶
- type Slice
- func (s Slice[T]) GetIndex(index int) (any, bool)
- func (s Slice[T]) GetValue() any
- func (s Slice[T]) IsChanged() bool
- func (s Slice[T]) Length() int
- func (s Slice[T]) MarshalJSON() ([]byte, error)
- func (s *Slice[T]) Reset()
- func (s *Slice[T]) SetIndex(index int, value any) bool
- func (s *Slice[T]) SetValue(value any) error
- func (s Slice[T]) Unchanged() []T
- func (s *Slice[T]) UnmarshalJSON(data []byte) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Slice ¶
type Slice[T comparable] struct { Values []T `json:"values" bson:"values"` Added []T `json:"-" bson:"-"` Deleted []T `json:"-" bson:"-"` }
Slice tracks changes to a slice of generic values. As items are added or removed from the slice, they are tracked in the "Added" and "Deleted" lists.
func NewSlice ¶
func NewSlice[T comparable](values ...T) Slice[T]
NewSlice returns a fully initialized Slice object
func (Slice[T]) GetIndex ¶ added in v0.29.0
GetIndex implements schema.ArrayGetter, returning the value at the provided index and TRUE, or (nil, false) if the index is out of range.
func (Slice[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface and serializes the Slice into a JSON array
func (*Slice[T]) SetIndex ¶ added in v0.29.0
SetIndex implements schema.ArraySetter, storing a value at the provided index (growing the slice to fit if necessary) and tracking any newly-added value. Returns FALSE if the value is not assignable to the slice's element type.
func (*Slice[T]) SetValue ¶
SetValue implements schema.ValueSetter and updates the current value, tracking changes to the added and deleted lists.
func (Slice[T]) Unchanged ¶ added in v0.25.4
func (s Slice[T]) Unchanged() []T
Unchanged returns the values that have not been added or removed
func (*Slice[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface and deserializes the Slice from a JSON array