Documentation
¶
Index ¶
- Variables
- func Copy[T any](src T, opts ...CopyOption) (T, error)
- func Equal[T any](a, b T, opts ...EqualOption) bool
- func IgnorePath(path string) interface{ ... }
- func Merge[T any](patches ...Patch[T]) (Patch[T], []Conflict, error)
- func MustCopy[T any](src T, opts ...CopyOption) T
- func Register[T any]()
- func RegisterCustomCopy[T any](fn func(T) (T, error))
- func RegisterCustomDiff[T any](fn func(a, b T) (Patch[T], error))
- func RegisterCustomEqual[T any](fn func(T, T) bool)
- func RegisterCustomPatch(p any)
- type ApplyError
- type Conflict
- type ConflictResolver
- type Copier
- type CopyOption
- type DiffOption
- type Differ
- type EqualOption
- type Keyer
- type OpInfo
- type OpKind
- type Patch
- type PatchBuilder
- func (b *PatchBuilder[T]) Add(keyOrIndex, val any) *PatchBuilder[T]
- func (b *PatchBuilder[T]) AddCondition(expr string) *PatchBuilder[T]
- func (b *PatchBuilder[T]) Build() (Patch[T], error)
- func (b *PatchBuilder[T]) Copy(from string) *PatchBuilder[T]
- func (b *PatchBuilder[T]) Delete(keyOrIndex any, oldVal any) *PatchBuilder[T]
- func (b *PatchBuilder[T]) Elem() *PatchBuilder[T]
- func (b *PatchBuilder[T]) Field(name string) *PatchBuilder[T]
- func (b *PatchBuilder[T]) FieldOrMapKey(key string) *PatchBuilder[T]
- func (b *PatchBuilder[T]) If(c any) *PatchBuilder[T]
- func (b *PatchBuilder[T]) Index(i int) *PatchBuilder[T]
- func (b *PatchBuilder[T]) Log(message string) *PatchBuilder[T]
- func (b *PatchBuilder[T]) MapKey(key any) *PatchBuilder[T]
- func (b *PatchBuilder[T]) Move(from string) *PatchBuilder[T]
- func (b *PatchBuilder[T]) Navigate(path string) *PatchBuilder[T]
- func (b *PatchBuilder[T]) Put(value any) *PatchBuilder[T]
- func (b *PatchBuilder[T]) Remove(oldVal any) *PatchBuilder[T]
- func (b *PatchBuilder[T]) Set(old, new any) *PatchBuilder[T]
- func (b *PatchBuilder[T]) Test(expected any) *PatchBuilder[T]
- func (b *PatchBuilder[T]) Unless(c any) *PatchBuilder[T]
- func (b *PatchBuilder[T]) WithCondition(c any) *PatchBuilder[T]
Constants ¶
This section is empty.
Variables ¶
var ErrConditionSkipped = fmt.Errorf("condition skipped")
Functions ¶
func Copy ¶
func Copy[T any](src T, opts ...CopyOption) (T, error)
Copy creates a deep copy of src. It returns the copy and a nil error in case of success and the zero value for the type and a non-nil error on failure.
It correctly handles cyclic references and unexported fields.
func Equal ¶
func Equal[T any](a, b T, opts ...EqualOption) bool
Equal performs a deep equality check between a and b. It supports cyclic references and unexported fields. You can customize behavior using EqualOption (e.g., IgnorePath).
func IgnorePath ¶
func IgnorePath(path string) interface { DiffOption CopyOption EqualOption }
IgnorePath returns an option that tells Diff, Copy, and Equal to ignore changes at the specified path. The path should use JSON Pointer notation (e.g., "/Field/SubField", "/Map/Key", "/Slice/0").
func Merge ¶
Merge combines multiple patches into a single patch. It detects conflicts and overlaps, including tree conflicts.
func MustCopy ¶
func MustCopy[T any](src T, opts ...CopyOption) T
MustCopy creates a deep copy of src. It returns the copy on success or panics in case of any failure.
It correctly handles cyclic references and unexported fields.
func Register ¶
func Register[T any]()
Register registers the Patch implementation for type T with the gob package. This is required if you want to use Gob serialization with Patch[T].
func RegisterCustomCopy ¶
RegisterCustomCopy registers a custom copy function for a specific type.
func RegisterCustomDiff ¶
RegisterCustomDiff registers a custom diff function for a specific type globally.
func RegisterCustomEqual ¶
RegisterCustomEqual registers a custom equality function for a specific type.
func RegisterCustomPatch ¶
func RegisterCustomPatch(p any)
RegisterCustomPatch registers a custom patch implementation for serialization. The provided patch instance must implement interface { PatchKind() string }.
Types ¶
type ApplyError ¶
type ApplyError struct {
// contains filtered or unexported fields
}
ApplyError represents one or more errors that occurred during patch application.
func (*ApplyError) Error ¶
func (e *ApplyError) Error() string
func (*ApplyError) Errors ¶
func (e *ApplyError) Errors() []error
func (*ApplyError) Unwrap ¶
func (e *ApplyError) Unwrap() []error
type Conflict ¶
Conflict represents a merge conflict where two patches modify the same path with different values or cause structural inconsistencies (tree conflicts).
type ConflictResolver ¶
type ConflictResolver interface {
// Resolve allows the resolver to intervene before an operation is applied.
// It returns the value to be applied and true if the operation should proceed,
// or the zero reflect.Value and false to skip it.
Resolve(path string, op OpKind, key, prevKey any, current, proposed reflect.Value) (reflect.Value, bool)
}
ConflictResolver allows custom logic to be injected during patch application. It is used to implement CRDTs, 3-way merges, and other conflict resolution strategies.
type Copier ¶
Copier is an interface that types can implement to provide their own custom deep copy logic.
type CopyOption ¶
type CopyOption interface {
// contains filtered or unexported methods
}
CopyOption allows configuring the behavior of the Copy function.
func SkipUnsupported ¶
func SkipUnsupported() CopyOption
SkipUnsupported returns an option that tells Copy to skip unsupported types.
type DiffOption ¶
type DiffOption interface {
// contains filtered or unexported methods
}
DiffOption allows configuring the behavior of the Diff function.
func DiffDetectMoves ¶
func DiffDetectMoves(enable bool) DiffOption
DiffDetectMoves returns an option that enables move and copy detection.
type Differ ¶
type Differ struct {
// contains filtered or unexported fields
}
Differ is a stateless engine for calculating patches between two values.
func NewDiffer ¶
func NewDiffer(opts ...DiffOption) *Differ
NewDiffer creates a new Differ with the given options.
type EqualOption ¶
type EqualOption interface {
// contains filtered or unexported methods
}
EqualOption allows configuring the behavior of the Equal function.
type Keyer ¶
type Keyer interface {
CanonicalKey() any
}
Keyer is an interface that types can implement to provide a canonical representation for map keys. This allows semantic equality checks for complex map keys.
type OpInfo ¶
type OpInfo struct {
Kind OpKind
Path string
From string // For Move/Copy
Val any
Conditions any // Placeholder
}
OpInfo represents a flattened operation from a patch.
type Patch ¶
type Patch[T any] interface { fmt.Stringer // Apply applies the patch to the value pointed to by v. // The value v must not be nil. Apply(v *T) // ApplyChecked applies the patch only if specific conditions are met. // 1. If the patch has a global Condition, it must evaluate to true. // 2. If Strict mode is enabled, every modification must match the 'oldVal' recorded in the patch. // 3. Any local per-field conditions must evaluate to true. ApplyChecked(v *T) error // ApplyResolved applies the patch using a custom ConflictResolver. // This is used for convergent synchronization (CRDTs). ApplyResolved(v *T, r ConflictResolver) error // Walk calls fn for every operation in the patch. // The path is a JSON Pointer dot-notation path (e.g. "/Field/SubField/0"). // If fn returns an error, walking stops and that error is returned. Walk(fn func(path string, op OpKind, old, new any) error) error // WithCondition returns a new Patch with the given global condition attached. WithCondition(c cond.Condition[T]) Patch[T] // WithStrict returns a new Patch with the strict consistency check enabled or disabled. WithStrict(strict bool) Patch[T] // Reverse returns a new Patch that undoes the changes in this patch. Reverse() Patch[T] // ToJSONPatch returns an RFC 6902 compliant JSON Patch representation of this patch. ToJSONPatch() ([]byte, error) // Summary returns a human-readable summary of the changes in the patch. Summary() string }
Patch represents a set of changes that can be applied to a value of type T.
func Diff ¶
func Diff[T any](a, b T, opts ...DiffOption) (Patch[T], error)
Diff compares two values a and b and returns a Patch that can be applied. It returns an error if the comparison fails (e.g., due to custom diff failure).
func DiffUsing ¶
DiffUsing compares two values a and b using the specified Differ and returns a Patch.
func MustDiff ¶
func MustDiff[T any](a, b T, opts ...DiffOption) Patch[T]
MustDiff compares two values a and b and returns a Patch that can be applied. It panics if the comparison fails.
func MustDiffUsing ¶
MustDiffUsing compares two values a and b using the specified Differ and returns a Patch. It panics if the comparison fails.
type PatchBuilder ¶
type PatchBuilder[T any] struct { // contains filtered or unexported fields }
PatchBuilder allows constructing a Patch[T] manually with on-the-fly type validation. It acts as a cursor within the value's structure, allowing for fluent navigation and modification.
func NewPatchBuilder ¶
func NewPatchBuilder[T any]() *PatchBuilder[T]
NewPatchBuilder returns a new PatchBuilder for type T, pointing at the root.
func (*PatchBuilder[T]) Add ¶
func (b *PatchBuilder[T]) Add(keyOrIndex, val any) *PatchBuilder[T]
Add appends an addition operation to a slice or map node.
func (*PatchBuilder[T]) AddCondition ¶
func (b *PatchBuilder[T]) AddCondition(expr string) *PatchBuilder[T]
AddCondition parses a string expression and attaches it to the appropriate node in the patch tree based on the paths used in the expression. It finds the longest common prefix of all paths in the expression and navigates to that node before attaching the condition. The expression is evaluated relative to the current node.
func (*PatchBuilder[T]) Build ¶
func (b *PatchBuilder[T]) Build() (Patch[T], error)
Build returns the constructed Patch or an error if any operation was invalid.
func (*PatchBuilder[T]) Copy ¶
func (b *PatchBuilder[T]) Copy(from string) *PatchBuilder[T]
Copy copies a value from another path to the current node.
func (*PatchBuilder[T]) Delete ¶
func (b *PatchBuilder[T]) Delete(keyOrIndex any, oldVal any) *PatchBuilder[T]
Delete appends a deletion operation to a slice or map node.
func (*PatchBuilder[T]) Elem ¶
func (b *PatchBuilder[T]) Elem() *PatchBuilder[T]
Elem returns a new PatchBuilder for the element type of a pointer or interface.
func (*PatchBuilder[T]) Field ¶
func (b *PatchBuilder[T]) Field(name string) *PatchBuilder[T]
Field returns a new PatchBuilder for the specified struct field. It automatically descends into pointers and interfaces if necessary.
func (*PatchBuilder[T]) FieldOrMapKey ¶
func (b *PatchBuilder[T]) FieldOrMapKey(key string) *PatchBuilder[T]
FieldOrMapKey returns a new PatchBuilder for the specified field or map key.
func (*PatchBuilder[T]) If ¶
func (b *PatchBuilder[T]) If(c any) *PatchBuilder[T]
If attaches an 'if' condition to the current node. If the condition evaluates to false, the operation at this node is skipped.
func (*PatchBuilder[T]) Index ¶
func (b *PatchBuilder[T]) Index(i int) *PatchBuilder[T]
Index returns a new PatchBuilder for the specified array or slice index.
func (*PatchBuilder[T]) Log ¶
func (b *PatchBuilder[T]) Log(message string) *PatchBuilder[T]
Log adds a log operation to the current node. It prints a message and the current value at the node during patch application.
func (*PatchBuilder[T]) MapKey ¶
func (b *PatchBuilder[T]) MapKey(key any) *PatchBuilder[T]
MapKey returns a new PatchBuilder for the specified map key.
func (*PatchBuilder[T]) Move ¶
func (b *PatchBuilder[T]) Move(from string) *PatchBuilder[T]
Move moves a value from another path to the current node.
func (*PatchBuilder[T]) Navigate ¶
func (b *PatchBuilder[T]) Navigate(path string) *PatchBuilder[T]
Navigate returns a new PatchBuilder for the specified path relative to the current node. It supports JSON Pointers ("/Field/Sub").
func (*PatchBuilder[T]) Put ¶
func (b *PatchBuilder[T]) Put(value any) *PatchBuilder[T]
Put replaces the value at the current node without requiring the 'old' value. Strict consistency checks for this specific value will be disabled.
func (*PatchBuilder[T]) Remove ¶
func (b *PatchBuilder[T]) Remove(oldVal any) *PatchBuilder[T]
Remove removes the current node from its parent.
func (*PatchBuilder[T]) Set ¶
func (b *PatchBuilder[T]) Set(old, new any) *PatchBuilder[T]
Set replaces the value at the current node. It requires the 'old' value to enable patch reversibility and strict application checking.
func (*PatchBuilder[T]) Test ¶
func (b *PatchBuilder[T]) Test(expected any) *PatchBuilder[T]
Test adds a test operation to the current node. The patch application will fail if the value at this node does not match the expected value.
func (*PatchBuilder[T]) Unless ¶
func (b *PatchBuilder[T]) Unless(c any) *PatchBuilder[T]
Unless attaches an 'unless' condition to the current node. If the condition evaluates to true, the operation at this node is skipped.
func (*PatchBuilder[T]) WithCondition ¶
func (b *PatchBuilder[T]) WithCondition(c any) *PatchBuilder[T]
WithCondition attaches a local condition to the current node. This condition is evaluated against the value at this node during ApplyChecked.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
audit_logging
command
|
|
|
business_rules
command
|
|
|
config_manager
command
|
|
|
crdt_sync
command
|
|
|
custom_types
command
|
|
|
http_patch_api
command
|
|
|
json_interop
command
|
|
|
key_normalization
command
|
|
|
keyed_inventory
command
|
|
|
move_detection
command
|
|
|
multi_error
command
|
|
|
state_management
command
|
|
|
text_sync
command
|
|
|
three_way_merge
command
|
|
|
websocket_sync
command
|
|
|
internal
|
|
|
resolvers
|
|