delta

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 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 additions and deletions

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