Documentation
¶
Index ¶
- Variables
- func Copy[T any](src T, opts ...CopyOption) (T, error)
- func Equal[T any](a, b T) bool
- func IgnorePath(path string) interface{ ... }
- func JoinPath(parent, child string) string
- func Merge[T any](patchA, patchB Patch[T]) (Patch[T], []Conflict, error)
- func MustCopy[T any](src T, opts ...CopyOption) T
- func NormalizePath(path string) string
- func Register[T any]()
- func RegisterCustomDiff[T any](d *Differ, fn func(a, b T) (Patch[T], error))
- func RegisterCustomPatch(kind string, p any)
- func ValueEqual(a, b reflect.Value) bool
- type AndCondition
- type ApplyError
- type Builder
- type CompareCondition
- type CompareFieldCondition
- type Condition
- func And[T any](conds ...Condition[T]) Condition[T]
- func Contains[T any](path, val string) Condition[T]
- func ContainsFold[T any](path, val string) Condition[T]
- func Defined[T any](path string) Condition[T]
- func EndsWith[T any](path, val string) Condition[T]
- func EndsWithFold[T any](path, val string) Condition[T]
- func Eq[T any](path string, val any) Condition[T]
- func EqFold[T any](path string, val any) Condition[T]
- func EqualField[T any](path1, path2 string) Condition[T]
- func EqualFieldFold[T any](path1, path2 string) Condition[T]
- func Greater[T any](path string, val any) Condition[T]
- func GreaterEqual[T any](path string, val any) Condition[T]
- func GreaterEqualField[T any](path1, path2 string) Condition[T]
- func GreaterField[T any](path1, path2 string) Condition[T]
- func In[T any](path string, values ...any) Condition[T]
- func InFold[T any](path string, values ...any) Condition[T]
- func Less[T any](path string, val any) Condition[T]
- func LessEqual[T any](path string, val any) Condition[T]
- func LessEqualField[T any](path1, path2 string) Condition[T]
- func LessField[T any](path1, path2 string) Condition[T]
- func Log[T any](message string) Condition[T]
- func Matches[T any](path, pattern string) Condition[T]
- func MatchesFold[T any](path, pattern string) Condition[T]
- func Ne[T any](path string, val any) Condition[T]
- func NeFold[T any](path string, val any) Condition[T]
- func Not[T any](c Condition[T]) Condition[T]
- func NotEqualField[T any](path1, path2 string) Condition[T]
- func NotEqualFieldFold[T any](path1, path2 string) Condition[T]
- func Or[T any](conds ...Condition[T]) Condition[T]
- func ParseCondition[T any](expr string) (Condition[T], error)
- func StartsWith[T any](path, val string) Condition[T]
- func StartsWithFold[T any](path, val string) Condition[T]
- func Type[T any](path, typeName string) Condition[T]
- func Undefined[T any](path string) Condition[T]
- type Conflict
- type ConflictResolver
- type Copier
- type CopyOption
- type DefinedCondition
- type DiffOption
- type Differ
- type InCondition
- type Keyer
- type LogCondition
- type Node
- func (n *Node) Add(i int, val any) error
- func (n *Node) AddMapEntry(key, val any) error
- func (n *Node) Copy(from string) *Node
- func (n *Node) Delete(keyOrIndex any, oldVal any) error
- func (n *Node) Elem() *Node
- func (n *Node) Field(name string) (*Node, error)
- func (n *Node) FieldOrMapKey(key string) (*Node, error)
- func (n *Node) If(c any) *Node
- func (n *Node) Index(i int) (*Node, error)
- func (n *Node) Log(message string) *Node
- func (n *Node) MapKey(key any) (*Node, error)
- func (n *Node) Move(from string) *Node
- func (n *Node) Navigate(path string) (*Node, error)
- func (n *Node) NavigateParts(parts []pathPart) (*Node, error)
- func (n *Node) Put(value any) *Node
- func (n *Node) Remove(oldVal any) error
- func (n *Node) Set(old, new any) *Node
- func (n *Node) Test(expected any) *Node
- func (n *Node) Unless(c any) *Node
- func (n *Node) WithCondition(c any) *Node
- type NotCondition
- type OpKind
- type OrCondition
- type Patch
- type Path
- type StringCondition
- type TypeCondition
- type UndefinedCondition
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 ¶
Equal performs a deep equality check between a and b.
Unlike reflect.DeepEqual, this function: 1. Respects `deep:"-"` struct tags. 2. Uses the library's internal reflection cache for faster struct traversal. 3. Optimized for common Go types using Generics to reduce interface allocations at the entry point.
func IgnorePath ¶
func IgnorePath(path string) interface { DiffOption CopyOption }
IgnorePath returns an option that tells both Diff and Copy to ignore changes at the specified path. The path should use Go-style notation (e.g., "Field.SubField", "Map.Key", "Slice[0]").
func Merge ¶
Merge combines two patches into a single patch. If both patches modify the same field to different values, a conflict is recorded. patchA and patchB are assumed to be derived from the same base state.
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 NormalizePath ¶
NormalizePath converts a dot-notation or JSON Pointer path to a standard JSON Pointer.
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 RegisterCustomDiff ¶
RegisterCustomDiff registers a custom diff function for a specific type.
func RegisterCustomPatch ¶ added in v3.1.1
RegisterCustomPatch registers a custom patch implementation for serialization. The provided patch instance is used to determine the type.
func ValueEqual ¶
ValueEqual performs a deep equality check between two reflect.Values. It is the internal engine for Equal and respects the same rules.
Types ¶
type AndCondition ¶
AndCondition represents a logical AND of multiple conditions.
func (AndCondition[T]) Evaluate ¶
func (c AndCondition[T]) Evaluate(v *T) (bool, error)
func (AndCondition[T]) MarshalJSON ¶
func (c AndCondition[T]) MarshalJSON() ([]byte, error)
type ApplyError ¶
type ApplyError struct {
Errors []error
}
ApplyError represents one or more errors that occurred during patch application.
func (*ApplyError) Error ¶
func (e *ApplyError) Error() string
func (*ApplyError) Unwrap ¶
func (e *ApplyError) Unwrap() []error
type Builder ¶
type Builder[T any] struct { // contains filtered or unexported fields }
Builder allows constructing a Patch[T] manually with on-the-fly type validation.
func (*Builder[T]) AddCondition ¶
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.
type CompareCondition ¶
CompareCondition represents a comparison between a path and a literal value.
func (CompareCondition[T]) Evaluate ¶
func (c CompareCondition[T]) Evaluate(v *T) (bool, error)
func (CompareCondition[T]) MarshalJSON ¶
func (c CompareCondition[T]) MarshalJSON() ([]byte, error)
type CompareFieldCondition ¶
CompareFieldCondition represents a comparison between two paths.
func (CompareFieldCondition[T]) Evaluate ¶
func (c CompareFieldCondition[T]) Evaluate(v *T) (bool, error)
func (CompareFieldCondition[T]) MarshalJSON ¶
func (c CompareFieldCondition[T]) MarshalJSON() ([]byte, error)
type Condition ¶
type Condition[T any] interface { // Evaluate evaluates the condition against the given value. Evaluate(v *T) (bool, error) // MarshalJSON returns the JSON representation of the condition. MarshalJSON() ([]byte, error) // contains filtered or unexported methods }
Condition represents a logical check against a value of type T.
func Contains ¶
Contains returns a condition that checks if the string value at the path contains the given substring.
func ContainsFold ¶
ContainsFold returns a condition that checks if the string value at the path contains the given substring (case-insensitive).
func EndsWith ¶
EndsWith returns a condition that checks if the string value at the path ends with the given suffix.
func EndsWithFold ¶
EndsWithFold returns a condition that checks if the string value at the path ends with the given suffix (case-insensitive).
func EqFold ¶
EqFold returns a condition that checks if the value at the path is equal to the given value (case-insensitive).
func EqualField ¶
EqualField returns a condition that checks if the value at path1 is equal to the value at path2.
func EqualFieldFold ¶
EqualFieldFold returns a condition that checks if the value at path1 is equal to the value at path2 (case-insensitive).
func Greater ¶
Greater returns a condition that checks if the value at the path is greater than the given value.
func GreaterEqual ¶
GreaterEqual returns a condition that checks if the value at the path is greater than or equal to the given value.
func GreaterEqualField ¶
GreaterEqualField returns a condition that checks if the value at path1 is greater than or equal to the value at path2.
func GreaterField ¶
GreaterField returns a condition that checks if the value at path1 is greater than the value at path2.
func InFold ¶
InFold returns a condition that checks if the value at the path is one of the given values (case-insensitive).
func Less ¶
Less returns a condition that checks if the value at the path is less than the given value.
func LessEqual ¶
LessEqual returns a condition that checks if the value at the path is less than or equal to the given value.
func LessEqualField ¶
LessEqualField returns a condition that checks if the value at path1 is less than or equal to the value at path2.
func LessField ¶
LessField returns a condition that checks if the value at path1 is less than the value at path2.
func Matches ¶
Matches returns a condition that checks if the string value at the path matches the given regex pattern.
func MatchesFold ¶
MatchesFold returns a condition that checks if the string value at the path matches the given regex pattern (case-insensitive).
func Ne ¶
Ne returns a condition that checks if the value at the path is not equal to the given value.
func NeFold ¶
NeFold returns a condition that checks if the value at the path is not equal to the given value (case-insensitive).
func NotEqualField ¶
NotEqualField returns a condition that checks if the value at path1 is not equal to the value at path2.
func NotEqualFieldFold ¶
NotEqualFieldFold returns a condition that checks if the value at path1 is not equal to the value at path2 (case-insensitive).
func ParseCondition ¶
ParseCondition parses a string expression into a Condition[T] tree.
func StartsWith ¶
StartsWith returns a condition that checks if the string value at the path starts with the given prefix.
func StartsWithFold ¶
StartsWithFold returns a condition that checks if the string value at the path starts with the given prefix (case-insensitive).
type ConflictResolver ¶
type ConflictResolver interface {
// Resolve allows the resolver to intervene before an operation is applied.
// It returns true if the operation should be applied, false to skip it.
// The resolver can also modify the operation or target value directly.
Resolve(path string, op OpKind, key, prevKey any, val 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. The type T in Copy() (T, error) must be the same concrete type as the receiver that implements this interface.
type CopyOption ¶
type CopyOption interface {
// contains filtered or unexported methods
}
CopyOption allows configuring the behavior of the Copy function.
func CopyIgnorePath ¶
func CopyIgnorePath(path string) CopyOption
CopyIgnorePath returns an option that tells Copy to ignore the specified path. The ignored path will have the zero value for its type in the resulting copy.
func SkipUnsupported ¶
func SkipUnsupported() CopyOption
SkipUnsupported returns an option that tells Copy to skip unsupported types (like non-nil functions or channels) instead of returning an error.
type DefinedCondition ¶
DefinedCondition checks if a path is defined (non-zero).
func (DefinedCondition[T]) Evaluate ¶
func (c DefinedCondition[T]) Evaluate(v *T) (bool, error)
func (DefinedCondition[T]) MarshalJSON ¶
func (c DefinedCondition[T]) MarshalJSON() ([]byte, error)
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.
func DiffIgnorePath ¶
func DiffIgnorePath(path string) DiffOption
DiffIgnorePath returns an option that tells Diff to ignore changes at the specified path.
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 InCondition ¶
InCondition checks if the value at a path is one of the given values.
func (InCondition[T]) Evaluate ¶
func (c InCondition[T]) Evaluate(v *T) (bool, error)
func (InCondition[T]) MarshalJSON ¶
func (c InCondition[T]) MarshalJSON() ([]byte, error)
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 LogCondition ¶
LogCondition logs a message during evaluation.
func (LogCondition[T]) Evaluate ¶
func (c LogCondition[T]) Evaluate(v *T) (bool, error)
func (LogCondition[T]) MarshalJSON ¶
func (c LogCondition[T]) MarshalJSON() ([]byte, error)
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node represents a specific location within a value's structure.
func (*Node) AddMapEntry ¶
AddMapEntry adds a new entry to a map node.
func (*Node) Field ¶
Field returns a Node for the specified struct field. It automatically descends into pointers and interfaces if necessary.
func (*Node) FieldOrMapKey ¶
FieldOrMapKey returns a Node for the specified field or map key.
func (*Node) If ¶
If attaches an 'if' condition to the current node. If the condition evaluates to false, the operation at this node is skipped.
func (*Node) Log ¶
Log adds a log operation to the current node. It prints a message and the current value at the node during patch application.
func (*Node) Navigate ¶
navigate returns a Node for the specified path relative to the current node. Navigate returns a Node for the specified path relative to the current node. It supports both Go-style paths ("Field.Sub") and JSON Pointers ("/Field/Sub").
func (*Node) NavigateParts ¶
func (*Node) Put ¶
Put replaces the value at the current node without requiring the 'old' value. Strict consistency checks for this specific value will be disabled.
func (*Node) Set ¶
Set replaces the value at the current node. It requires the 'old' value to enable patch reversibility and strict application checking.
func (*Node) Test ¶
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 (*Node) Unless ¶
Unless attaches an 'unless' condition to the current node. If the condition evaluates to true, the operation at this node is skipped.
func (*Node) WithCondition ¶
WithCondition attaches a local condition to the current node. This condition is evaluated against the value at this node during ApplyChecked.
type NotCondition ¶
NotCondition represents a logical NOT of a condition.
func (NotCondition[T]) Evaluate ¶
func (c NotCondition[T]) Evaluate(v *T) (bool, error)
func (NotCondition[T]) MarshalJSON ¶
func (c NotCondition[T]) MarshalJSON() ([]byte, error)
type OrCondition ¶
OrCondition represents a logical OR of multiple conditions.
func (OrCondition[T]) Evaluate ¶
func (c OrCondition[T]) Evaluate(v *T) (bool, error)
func (OrCondition[T]) MarshalJSON ¶
func (c OrCondition[T]) MarshalJSON() ([]byte, error)
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 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 // Release returns the patch and its internal structures to the pool. // The patch must not be used after calling this. Release() }
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]
Diff compares two values a and b and returns a Patch that can be applied.
type Path ¶
type Path string
Path represents a path to a field or element within a structure. Syntax: "Field", "Field.SubField", "Slice[0]", "Map.Key", "Ptr.Field". It also supports JSON Pointers (RFC 6901) like "/Field/SubField".
type StringCondition ¶
StringCondition checks a string value at a path against a pattern.
func (StringCondition[T]) Evaluate ¶
func (c StringCondition[T]) Evaluate(v *T) (bool, error)
func (StringCondition[T]) MarshalJSON ¶
func (c StringCondition[T]) MarshalJSON() ([]byte, error)
type TypeCondition ¶
TypeCondition checks if the value at a path has a specific type.
func (TypeCondition[T]) Evaluate ¶
func (c TypeCondition[T]) Evaluate(v *T) (bool, error)
func (TypeCondition[T]) MarshalJSON ¶
func (c TypeCondition[T]) MarshalJSON() ([]byte, error)
type UndefinedCondition ¶
UndefinedCondition checks if a path is undefined (zero value).
func (UndefinedCondition[T]) Evaluate ¶
func (c UndefinedCondition[T]) Evaluate(v *T) (bool, error)
func (UndefinedCondition[T]) MarshalJSON ¶
func (c UndefinedCondition[T]) MarshalJSON() ([]byte, error)
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
|
|