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 ¶
- type Bool
- func (v Bool) Bool() bool
- func (v *Bool) Clear()
- func (v Bool) GetValue() any
- func (v Bool) IsChanged() bool
- func (v Bool) IsFalse() bool
- func (v Bool) IsTrue() bool
- func (v Bool) IsZero() bool
- func (v Bool) MarshalBSONValue() (bsontype.Type, []byte, error)
- func (v Bool) MarshalJSON() ([]byte, error)
- func (v Bool) NotChanged() bool
- func (v Bool) Original() bool
- func (v *Bool) Pointer() *bool
- func (v *Bool) Reset()
- func (v *Bool) Set(value bool)
- func (v *Bool) SetValue(value any) error
- func (v *Bool) UnmarshalBSONValue(t bsontype.Type, data []byte) error
- func (v *Bool) UnmarshalJSON(data []byte) error
- func (v Bool) Value() bool
- type ObjectID
- func (v *ObjectID) Clear()
- func (v ObjectID) GetValue() any
- func (v ObjectID) Hex() string
- func (v ObjectID) IsChanged() bool
- func (v ObjectID) IsZero() bool
- func (v ObjectID) MarshalBSONValue() (bsontype.Type, []byte, error)
- func (v ObjectID) MarshalJSON() ([]byte, error)
- func (v ObjectID) NotChanged() bool
- func (v ObjectID) Original() primitive.ObjectID
- func (v *ObjectID) Pointer() *primitive.ObjectID
- func (v *ObjectID) Reset()
- func (v *ObjectID) Set(value primitive.ObjectID)
- func (v *ObjectID) SetValue(value any) error
- func (v ObjectID) String() string
- func (v *ObjectID) UnmarshalBSONValue(t bsontype.Type, data []byte) error
- func (v *ObjectID) UnmarshalJSON(data []byte) error
- func (v ObjectID) Value() primitive.ObjectID
- type Slice
- func (v Slice[T]) Added() []T
- func (v *Slice[T]) Clear()
- func (v Slice[T]) Deleted() []T
- func (v Slice[T]) GetIndex(index int) (any, bool)
- func (v Slice[T]) GetValue() any
- func (v Slice[T]) IsChanged() bool
- func (v Slice[T]) IsZero() bool
- func (v Slice[T]) Length() int
- func (v Slice[T]) MarshalBSONValue() (bsontype.Type, []byte, error)
- func (v Slice[T]) MarshalJSON() ([]byte, error)
- func (v Slice[T]) NotChanged() bool
- func (v Slice[T]) Original() []T
- func (v *Slice[T]) Pointer() *[]T
- func (v *Slice[T]) Reset()
- func (v *Slice[T]) Set(values []T)
- func (v *Slice[T]) SetIndex(index int, value any) bool
- func (v *Slice[T]) SetValue(value any) error
- func (v Slice[T]) Unchanged() []T
- func (v *Slice[T]) UnmarshalBSONValue(t bsontype.Type, data []byte) error
- func (v *Slice[T]) UnmarshalJSON(data []byte) error
- func (v Slice[T]) Value() []T
- type String
- func (v *String) Clear()
- func (v String) GetValue() any
- func (v String) IsChanged() bool
- func (v String) IsZero() bool
- func (v String) MarshalBSONValue() (bsontype.Type, []byte, error)
- func (v String) MarshalJSON() ([]byte, error)
- func (v String) NotChanged() bool
- func (v String) Original() string
- func (v *String) Pointer() *string
- func (v *String) Reset()
- func (v *String) Set(value string)
- func (v *String) SetValue(value any) error
- func (v String) String() string
- func (v *String) UnmarshalBSONValue(t bsontype.Type, data []byte) error
- func (v *String) UnmarshalJSON(data []byte) error
- func (v String) Value() string
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
NewBool returns a Bool whose original and current values are both the given value
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
GetValue implements schema.ValueGetter and returns the current value
func (Bool) IsChanged ¶ added in v0.1.0
IsChanged returns TRUE if the value has changed since it was loaded
func (Bool) IsZero ¶ added in v0.1.0
IsZero returns TRUE if the current value is the zero value for this type
func (Bool) MarshalBSONValue ¶ added in v0.1.0
MarshalBSONValue implements the bson.ValueMarshaler interface
func (Bool) MarshalJSON ¶ added in v0.1.0
MarshalJSON implements the json.Marshaler interface and serializes the current value as a JSON boolean
func (Bool) NotChanged ¶ added in v0.1.0
NotChanged returns TRUE if the value has not changed since it was loaded
func (Bool) Original ¶ added in v0.2.0
Original returns the value as it was when this Bool was loaded
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) SetValue ¶ added in v0.1.0
SetValue implements schema.ValueSetter and updates the current value, leaving the original value unchanged.
func (*Bool) UnmarshalBSONValue ¶ added in v0.1.0
UnmarshalBSONValue implements the bson.ValueUnmarshaler interface
func (*Bool) UnmarshalJSON ¶ added in v0.1.0
UnmarshalJSON implements the json.Unmarshaler interface and sets both the original and current values from a JSON boolean
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 ¶
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) MarshalBSONValue ¶
MarshalBSONValue implements the bson.ValueMarshaler interface
func (ObjectID) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface and serializes the current value as a JSON hex string
func (ObjectID) NotChanged ¶
NotChanged returns TRUE if the value has not changed since it was loaded
func (ObjectID) Original ¶ added in v0.2.0
Original returns the value as it was when this ObjectID was loaded
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) SetValue ¶
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) UnmarshalBSONValue ¶
UnmarshalBSONValue implements the bson.ValueUnmarshaler interface
func (*ObjectID) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface and sets both the original and current values from a JSON hex string or null
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
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
GetValue implements schema.ValueGetter and returns a copy of the current list
func (Slice[T]) IsChanged ¶ added in v0.4.0
IsChanged returns TRUE if the list has changed since it was loaded. A change of order alone is a change.
func (Slice[T]) MarshalBSONValue ¶ added in v0.4.0
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
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
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
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
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
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
UnmarshalJSON implements the json.Unmarshaler interface and deserializes the Slice from a JSON array
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 ¶
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) MarshalBSONValue ¶
MarshalBSONValue implements the bson.ValueMarshaler interface
func (String) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface and serializes the current value as a JSON string
func (String) NotChanged ¶
NotChanged returns TRUE if the value has not changed since it was loaded
func (String) Original ¶ added in v0.2.0
Original returns the value as it was when this String was loaded
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) SetValue ¶
SetValue implements schema.ValueSetter and updates the current value, leaving the original value unchanged.
func (*String) UnmarshalBSONValue ¶
UnmarshalBSONValue implements the bson.ValueUnmarshaler interface
func (*String) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface and sets both the original and current values from a JSON string