delta

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

🔺 delta

Go Reference Version Build Status Go Report Card Codecov

Change-Tracking Value Wrappers for Go

delta provides typed value wrappers that remember the value they were loaded with, report whether it has changed since, and hand the original back. This lets you tell which fields of a record were modified, and what they used to be. That is useful for writing minimal database updates, skipping no-op saves, or cleaning up whatever the previous value pointed at.

Type Wraps
Bool bool
String string
ObjectID a MongoDB primitive.ObjectID
Slice[T] a list of any comparable type

Each wrapper marshals to and from JSON and BSON as its plain value, so it drops into an existing data model without changing the stored format.

Using delta
// Wrap a value as it loads from the database.
name := delta.NewString("Sarah Connor")

name.IsChanged() // false, because nothing has changed yet

name.Set("Sarah Reese")
name.IsChanged() // true, because the current value differs from the original
name.Value()     // "Sarah Reese"
name.Original()  // "Sarah Connor", which the database still holds

// After you save the record, make the saved value the new baseline.
name.Reset()
name.IsChanged() // false
name.Original()  // "Sarah Reese"

The baseline is captured when a value is constructed and when it is unmarshaled, so a record freshly loaded from the database reads as unchanged until your code writes to it. Nothing moves the baseline at a save, so call Reset() on each value once the save has succeeded.

Clear() sets a value to the zero value for its type, and counts as a change like any other Set.

Track a list

Slice[T] reports what was added and deleted since the list was loaded.

targets := delta.NewSlice("mastodon", "bluesky")

targets.Set([]string{"bluesky", "threads"})

targets.IsChanged() // true
targets.Added()     // ["threads"]
targets.Deleted()   // ["mastodon"]
targets.Unchanged() // ["bluesky"]
targets.Original()  // ["mastodon", "bluesky"]

A change of order alone counts as a change, with nothing added or deleted.

Use a wrapper in a record

A wrapper is an ordinary struct field. It is stored as its plain value, and it loads with its baseline already set.

type Person struct {
	Name delta.String        `json:"name" bson:"name"`
	Tags delta.Slice[string] `json:"tags" bson:"tags"`
}

// Stored as {"name": "Sarah Connor", "tags": ["resistance"]}

A Slice also reads the {values: [...]} document that the older rosetta/delta package stored, and writes it back as a plain array.

Work with rosetta schemas

Every wrapper implements the GetValue and SetValue methods that rosetta's schema package uses, so a schema can read and write a wrapped field as a single value. SetValue converts loosely: an ObjectID accepts a hex string, and a Slice accepts any list that rosetta can convert. A Slice can also be addressed by index.

No Warranty

This software is provided as-is, without any warranty of any kind. Use it at your own risk.

Pull Requests Welcome

I'm trying to make delta the best it can be, and your help is greatly appreciated. If you find a bug or have an idea for a new feature, please open an issue or submit a pull request. We're all in this together! 🔺

Documentation

Overview

Package delta wraps a few value types so that each one remembers the value it was loaded with, and can report whether it has changed since.

Bool, String, ObjectID, and the generic Slice each hold a current value and an original value. The original is captured when the wrapper is constructed and again whenever it is unmarshaled from JSON or BSON, so a record freshly read from the database reads as unchanged until application code calls Set, Clear, or SetValue. Original returns that baseline, which is the value the database still holds, and Reset moves the baseline to the current value once a caller has saved it.

Slice tracks a list of any comparable type. Its Added, Deleted, and Unchanged methods compare the current list with the original, and a change of order alone is a change.

Each wrapper marshals as its plain value rather than as a sub-document, so it drops into an existing model without changing the stored format. A failed unmarshal changes neither value. GetValue and SetValue let rosetta's schema package read and write a wrapper as a single value, and Slice can also be addressed by index.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool added in v0.1.0

type Bool struct {
	// contains filtered or unexported fields
}

Bool tracks changes to a value since it has been loaded into memory

Example

ExampleBool shows a change being tracked against the value that was loaded

isPublic := NewBool(false)
fmt.Println(isPublic.IsChanged())

isPublic.Set(true)
fmt.Println(isPublic.IsChanged())
fmt.Println(isPublic.Value())
fmt.Println(isPublic.Original())
Output:
false
true
true
false

func NewBool added in v0.1.0

func NewBool(value bool) Bool

NewBool returns a Bool whose original and current values are both the given value

func (Bool) Bool added in v0.1.0

func (v Bool) Bool() bool

Bool returns the value as a bool

func (*Bool) Clear added in v0.4.0

func (v *Bool) Clear()

Clear sets the current value to false, leaving the baseline as it was

func (Bool) GetValue added in v0.1.0

func (v Bool) GetValue() any

GetValue implements schema.ValueGetter and returns the current value

func (Bool) IsChanged added in v0.1.0

func (v Bool) IsChanged() bool

IsChanged returns TRUE if the value has changed since it was loaded

func (Bool) IsFalse added in v0.1.0

func (v Bool) IsFalse() bool

IsFalse returns TRUE if the current value is false

func (Bool) IsTrue added in v0.1.0

func (v Bool) IsTrue() bool

IsTrue returns TRUE if the current value is true

func (Bool) IsZero added in v0.1.0

func (v Bool) IsZero() bool

IsZero returns TRUE if the current value is the zero value for this type

func (Bool) MarshalBSONValue added in v0.1.0

func (v Bool) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue implements the bson.ValueMarshaler interface

func (Bool) MarshalJSON added in v0.1.0

func (v Bool) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface and serializes the current value as a JSON boolean

func (Bool) NotChanged added in v0.1.0

func (v Bool) NotChanged() bool

NotChanged returns TRUE if the value has not changed since it was loaded

func (Bool) Original added in v0.2.0

func (v Bool) Original() bool

Original returns the value as it was when this Bool was loaded

func (*Bool) Pointer added in v0.1.0

func (v *Bool) Pointer() *bool

Pointer returns a pointer to the current value

func (*Bool) Reset added in v0.4.0

func (v *Bool) Reset()

Reset makes the current value the baseline, for a caller that has just saved it

Example

ExampleBool_Reset shows the baseline moving once the caller has saved the value

isPublic := NewBool(false)
isPublic.Set(true)

isPublic.Reset()
fmt.Println(isPublic.IsChanged())
fmt.Println(isPublic.Original())
Output:
false
true

func (*Bool) Set added in v0.1.0

func (v *Bool) Set(value bool)

Set updates the current value

func (*Bool) SetValue added in v0.1.0

func (v *Bool) SetValue(value any) error

SetValue implements schema.ValueSetter and updates the current value, leaving the original value unchanged.

func (*Bool) UnmarshalBSONValue added in v0.1.0

func (v *Bool) UnmarshalBSONValue(t bsontype.Type, data []byte) error

UnmarshalBSONValue implements the bson.ValueUnmarshaler interface

func (*Bool) UnmarshalJSON added in v0.1.0

func (v *Bool) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface and sets both the original and current values from a JSON boolean

func (Bool) Value added in v0.1.0

func (v Bool) Value() bool

Value returns the value as a bool

type ObjectID

type ObjectID struct {
	// contains filtered or unexported fields
}

ObjectID tracks changes to a value since it has been loaded into memory

Example

ExampleObjectID shows a change being tracked against the value that was loaded

stored, _ := primitive.ObjectIDFromHex("6b04f64736058a543006f9a6")
next, _ := primitive.ObjectIDFromHex("6b04f64736058a543006f9a7")

parentID := NewObjectID(stored)
fmt.Println(parentID.IsChanged())

parentID.Set(next)
fmt.Println(parentID.IsChanged())
fmt.Println(parentID.Hex())
fmt.Println(parentID.Original().Hex())
Output:
false
true
6b04f64736058a543006f9a7
6b04f64736058a543006f9a6

func NewObjectID

func NewObjectID(value primitive.ObjectID) ObjectID

NewObjectID returns an ObjectID whose original and current values are both the given value

func (*ObjectID) Clear added in v0.4.0

func (v *ObjectID) Clear()

Clear sets the current value to the nil ObjectID, leaving the baseline as it was

func (ObjectID) GetValue

func (v ObjectID) GetValue() any

GetValue implements schema.ValueGetter and returns the current value

func (ObjectID) Hex

func (v ObjectID) Hex() string

Hex returns the value as a hex string

func (ObjectID) IsChanged

func (v ObjectID) IsChanged() bool

IsChanged returns TRUE if the value has changed since it was loaded

func (ObjectID) IsZero

func (v ObjectID) IsZero() bool

IsZero returns TRUE if the current value is the zero value for this type

func (ObjectID) MarshalBSONValue

func (v ObjectID) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue implements the bson.ValueMarshaler interface

func (ObjectID) MarshalJSON

func (v ObjectID) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface and serializes the current value as a JSON hex string

func (ObjectID) NotChanged

func (v ObjectID) NotChanged() bool

NotChanged returns TRUE if the value has not changed since it was loaded

func (ObjectID) Original added in v0.2.0

func (v ObjectID) Original() primitive.ObjectID

Original returns the value as it was when this ObjectID was loaded

func (*ObjectID) Pointer

func (v *ObjectID) Pointer() *primitive.ObjectID

Pointer returns a pointer to the current value

func (*ObjectID) Reset added in v0.4.0

func (v *ObjectID) Reset()

Reset makes the current value the baseline, for a caller that has just saved it

Example

ExampleObjectID_Reset shows the baseline moving once the caller has saved the value

stored, _ := primitive.ObjectIDFromHex("6b04f64736058a543006f9a6")
next, _ := primitive.ObjectIDFromHex("6b04f64736058a543006f9a7")

parentID := NewObjectID(stored)
parentID.Set(next)

parentID.Reset()
fmt.Println(parentID.IsChanged())
fmt.Println(parentID.Original().Hex())
Output:
false
6b04f64736058a543006f9a7

func (*ObjectID) Set

func (v *ObjectID) Set(value primitive.ObjectID)

Set updates the current value

func (*ObjectID) SetValue

func (v *ObjectID) SetValue(value any) error

SetValue implements schema.ValueSetter and updates the current value from an ObjectID or a hex string. An empty value clears it, and the original value is unchanged.

func (ObjectID) String

func (v ObjectID) String() string

String returns the value as a hex string

func (*ObjectID) UnmarshalBSONValue

func (v *ObjectID) UnmarshalBSONValue(t bsontype.Type, data []byte) error

UnmarshalBSONValue implements the bson.ValueUnmarshaler interface

func (*ObjectID) UnmarshalJSON

func (v *ObjectID) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface and sets both the original and current values from a JSON hex string or null

func (ObjectID) Value

func (v ObjectID) Value() primitive.ObjectID

Value returns the value as a primitive.ObjectID

type Slice added in v0.4.0

type Slice[T comparable] struct {
	// contains filtered or unexported fields
}

Slice tracks changes to a list of values since it has been loaded into memory

Example

ExampleSlice shows a change being tracked against the list that was loaded

targets := NewSlice("mastodon", "bluesky")
fmt.Println(targets.IsChanged())

targets.Set([]string{"bluesky", "threads"})
fmt.Println(targets.IsChanged())
fmt.Println(targets.Added())
fmt.Println(targets.Deleted())
fmt.Println(targets.Unchanged())
fmt.Println(targets.Original())
Output:
false
true
[threads]
[mastodon]
[bluesky]
[mastodon bluesky]

func NewSlice added in v0.4.0

func NewSlice[T comparable](values ...T) Slice[T]

NewSlice returns a fully initialized Slice object, holding a copy of the values

func (Slice[T]) Added added in v0.4.0

func (v Slice[T]) Added() []T

Added returns the current values that the baseline does not hold, in their current order

func (*Slice[T]) Clear added in v0.4.0

func (v *Slice[T]) Clear()

Clear sets the current value to the empty list, leaving the baseline as it was

func (Slice[T]) Deleted added in v0.4.0

func (v Slice[T]) Deleted() []T

Deleted returns the baseline values that the current list does not hold, in their original order

func (Slice[T]) GetIndex added in v0.4.0

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

GetIndex implements schema.ArrayGetter and returns the value at the index, or (nil, false) if the index is out of range.

func (Slice[T]) GetValue added in v0.4.0

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

GetValue implements schema.ValueGetter and returns a copy of the current list

func (Slice[T]) IsChanged added in v0.4.0

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

IsChanged returns TRUE if the list has changed since it was loaded. A change of order alone is a change.

func (Slice[T]) IsZero added in v0.4.0

func (v Slice[T]) IsZero() bool

IsZero returns TRUE if the current list is empty

func (Slice[T]) Length added in v0.4.0

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

Length returns the number of values in the current list

func (Slice[T]) MarshalBSONValue added in v0.4.0

func (v Slice[T]) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue implements the bson.ValueMarshaler interface and serializes the Slice into a BSON array, which is never null

func (Slice[T]) MarshalJSON added in v0.4.0

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

MarshalJSON implements the json.Marshaler interface and serializes the Slice into a JSON array, which is never null

func (Slice[T]) NotChanged added in v0.4.0

func (v Slice[T]) NotChanged() bool

NotChanged returns TRUE if the list has not changed since it was loaded

func (Slice[T]) Original added in v0.4.0

func (v Slice[T]) Original() []T

Original returns a copy of the list as it was when this Slice was loaded

func (*Slice[T]) Pointer added in v0.4.0

func (v *Slice[T]) Pointer() *[]T

Pointer returns a pointer to the current list, for a caller that edits it in place

func (*Slice[T]) Reset added in v0.4.0

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

Reset makes the current value the baseline, for a caller that has just saved it

Example

ExampleSlice_Reset shows the baseline moving once the caller has saved the list

targets := NewSlice("mastodon")
targets.Set([]string{"mastodon", "bluesky"})

targets.Reset()
fmt.Println(targets.IsChanged())
fmt.Println(targets.Added())
fmt.Println(targets.Original())
Output:
false
[]
[mastodon bluesky]

func (*Slice[T]) Set added in v0.4.0

func (v *Slice[T]) Set(values []T)

Set updates the current value to a copy of the provided list

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

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

SetIndex implements schema.ArraySetter and stores a value at the index, growing the list to fit. It returns FALSE for a negative index, or a value that is not a T.

func (*Slice[T]) SetValue added in v0.4.0

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

SetValue implements schema.ValueSetter and updates the current list. It converts loosely, and returns an error for a value that rosetta cannot read as a list.

func (Slice[T]) Unchanged added in v0.4.0

func (v Slice[T]) Unchanged() []T

Unchanged returns the current values that the baseline also holds, in their current order

func (*Slice[T]) UnmarshalBSONValue added in v0.4.0

func (v *Slice[T]) UnmarshalBSONValue(t bsontype.Type, data []byte) error

UnmarshalBSONValue implements the bson.ValueUnmarshaler interface. It reads a BSON array, a null, or the {values: [...]} document that rosetta's delta.Slice stored.

Example

ExampleSlice_UnmarshalBSONValue shows a list that an older version stored inside a document being read, and written back as a plain array

stored, _ := bson.Marshal(bson.M{"targets": bson.M{"values": bson.A{"mastodon", "bluesky"}}})

record := struct {
	Targets Slice[string] `bson:"targets"`
}{}

if err := bson.Unmarshal(stored, &record); err != nil {
	fmt.Println(err)
	return
}

saved, _ := bson.Marshal(record)

fmt.Println(record.Targets.Value())
fmt.Println(record.Targets.IsChanged())
fmt.Println(bson.Raw(saved))
Output:
[mastodon bluesky]
false
{"targets": ["mastodon","bluesky"]}

func (*Slice[T]) UnmarshalJSON added in v0.4.0

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

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

func (Slice[T]) Value added in v0.4.0

func (v Slice[T]) Value() []T

Value returns a copy of the current list, which is never nil

type String

type String struct {
	// contains filtered or unexported fields
}

String tracks changes to a value since it has been loaded into memory

Example

ExampleString shows a change being tracked against the value that was loaded

name := NewString("Sarah Connor")
fmt.Println(name.IsChanged())

name.Set("Sarah Reese")
fmt.Println(name.IsChanged())
fmt.Println(name.Value())
fmt.Println(name.Original())
Output:
false
true
Sarah Reese
Sarah Connor

func NewString

func NewString(value string) String

NewString returns a String whose original and current values are both the given value

func (*String) Clear added in v0.4.0

func (v *String) Clear()

Clear sets the current value to the empty string, leaving the baseline as it was

Example

ExampleString_Clear shows that emptying a value counts as a change

name := NewString("Sarah Connor")
name.Clear()

fmt.Println(name.IsZero())
fmt.Println(name.IsChanged())
fmt.Println(name.Original())
Output:
true
true
Sarah Connor

func (String) GetValue

func (v String) GetValue() any

GetValue implements schema.ValueGetter and returns the current value

func (String) IsChanged

func (v String) IsChanged() bool

IsChanged returns TRUE if the value has changed since it was loaded

func (String) IsZero

func (v String) IsZero() bool

IsZero returns TRUE if the current value is the zero value for this type

func (String) MarshalBSONValue

func (v String) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue implements the bson.ValueMarshaler interface

func (String) MarshalJSON

func (v String) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface and serializes the current value as a JSON string

func (String) NotChanged

func (v String) NotChanged() bool

NotChanged returns TRUE if the value has not changed since it was loaded

func (String) Original added in v0.2.0

func (v String) Original() string

Original returns the value as it was when this String was loaded

func (*String) Pointer

func (v *String) Pointer() *string

Pointer returns a pointer to the current value

func (*String) Reset added in v0.4.0

func (v *String) Reset()

Reset makes the current value the baseline, for a caller that has just saved it

Example

ExampleString_Reset shows the baseline moving once the caller has saved the value

name := NewString("Sarah Connor")
name.Set("Sarah Reese")

name.Reset()
fmt.Println(name.IsChanged())
fmt.Println(name.Original())
Output:
false
Sarah Reese

func (*String) Set

func (v *String) Set(value string)

Set updates the current value

func (*String) SetValue

func (v *String) SetValue(value any) error

SetValue implements schema.ValueSetter and updates the current value, leaving the original value unchanged.

func (String) String

func (v String) String() string

String returns the value as a string

func (*String) UnmarshalBSONValue

func (v *String) UnmarshalBSONValue(t bsontype.Type, data []byte) error

UnmarshalBSONValue implements the bson.ValueUnmarshaler interface

func (*String) UnmarshalJSON

func (v *String) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface and sets both the original and current values from a JSON string

func (String) Value

func (v String) Value() string

Value returns the value as a string

Jump to

Keyboard shortcuts

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