Documentation
¶
Index ¶
- Variables
- func Copy[T any](src T, opts ...CopyOption) (T, error)
- func CopySkipUnsupported[T any](src T) (T, error)deprecated
- func IgnorePath(path string) interface{ ... }
- func MustCopy[T any](src T, opts ...CopyOption) T
- func Register[T any]()
- type AndCondition
- 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 Equal[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 EqualFold[T any](path string, val any) 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 Not[T any](c Condition[T]) Condition[T]
- func NotEqual[T any](path string, val any) Condition[T]
- func NotEqualField[T any](path1, path2 string) Condition[T]
- func NotEqualFieldFold[T any](path1, path2 string) Condition[T]
- func NotEqualFold[T any](path string, val any) 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 ConflictResolver
- type Copier
- type CopyOption
- type DefinedCondition
- type DiffOption
- type Differ
- type InCondition
- 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) Put(value any) *Node
- 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 CopySkipUnsupported
deprecated
CopySkipUnsupported 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. Unsupported types (e.g. non-nil functions, channels or unsafe pointers) are skipped (the copy will have the zero value for the type) instead of returning an error.
Deprecated: Use Copy with the SkipUnsupported() option instead.
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 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.
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 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 Equal ¶
Equal returns a condition that checks if the value at the path is equal to the given value.
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 EqualFold ¶
EqualFold returns a condition that checks if the value at the path is equal to the given value (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 NotEqual ¶
NotEqual returns a condition that checks if the value at the path is not equal to the given value.
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 NotEqualFold ¶
NotEqualFold returns a condition that checks if the value at the path is not equal to the given value (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 DiffIgnorePath ¶
func DiffIgnorePath(path string) DiffOption
DiffIgnorePath returns an option that tells Diff to ignore changes at the specified path. The path should use Go-style notation (e.g., "Field.SubField", "Map.Key", "Slice[0]").
type Differ ¶
type Differ[T any] interface { // Diff returns a patch that transforms the receiver into other. Diff(other T) (Patch[T], error) }
Differ is an interface that types can implement to provide their own custom diff logic. The type T in Diff(other T) (Patch[T], error) must be the same concrete type as the receiver that implements this interface.
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 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) 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 Go-style 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) }
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 to a to make it equal to b.
It uses a combination of Myers' Diff algorithm for slices and recursive type-specific comparison for structs, maps, and pointers.
If a and b are deeply equal, it returns nil.
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
|
|
|
http_patch_api
command
|
|
|
json_interop
command
|
|
|
keyed_inventory
command
|
|
|
state_management
command
|
|
|
text_sync
command
|
|
|
websocket_sync
command
|
|
|
internal
|
|
|
resolvers
|
|