delta

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: Apache-2.0 Imports: 4 Imported by: 1

README

delta

Change-tracking containers. delta.Slice[T] wraps a []T and records which elements were Added and Deleted relative to its starting state, so a caller can compute and apply a minimal diff. Part of rosetta.

What matters here

  • Only Values is serialized; Added and Deleted are transient. Both carry json:"-" bson:"-" tags — the diff bookkeeping exists only for the lifetime of the in-memory object and is intentionally not persisted. A round-trip through JSON/BSON yields a Slice with the current values but an empty change set, as if freshly constructed.
  • SetValue(any) is the schema-integration seam (it satisfies the ValueSetter interface used by schema). It accepts a generic value and coerces it into []T; that is why the element type is constrained to comparable (membership checks drive the Added/Deleted tracking).
  • UnmarshalJSON parses untrusted input and is fuzzed (FuzzSliceUnmarshalJSON). When changing the unmarshal path, keep that fuzzer green — it guards against panics on malformed JSON.

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

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

func (s Slice[T]) GetIndex(index int) (any, bool)

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

func (s Slice[T]) GetValue() any

GetValue implements schema.ValueGetter and returns the current value

func (Slice[T]) IsChanged

func (s Slice[T]) IsChanged() bool

IsChanged returns TRUE if the slice has been modified

func (Slice[T]) Length

func (s Slice[T]) Length() int

Length returns the number of keys in the map

func (Slice[T]) MarshalJSON

func (s Slice[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface and serializes the Slice into a JSON array

func (*Slice[T]) Reset

func (s *Slice[T]) Reset()

Reset resets all of the added/deleted values

func (*Slice[T]) SetIndex added in v0.29.0

func (s *Slice[T]) SetIndex(index int, value any) bool

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

func (s *Slice[T]) SetValue(value any) error

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

func (s *Slice[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface and deserializes the Slice from a JSON array

Jump to

Keyboard shortcuts

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