op

package
v0.6.6 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package op provides implementations for JSON Patch operations.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Core path errors
	ErrPathNotFound   = errors.New("path not found")
	ErrInvalidPath    = errors.New("invalid path")
	ErrPathEmpty      = errors.New("path cannot be empty")
	ErrFromPathEmpty  = errors.New("from path cannot be empty")
	ErrPathsIdentical = errors.New("cannot move into own children")

	// Array operation errors
	ErrIndexOutOfRange     = errors.New("index out of range")
	ErrNotAnArray          = errors.New("not an array")
	ErrArrayTooSmall       = errors.New("array must have at least 2 elements")
	ErrPositionOutOfBounds = errors.New("position out of bounds")
	ErrPositionNegative    = errors.New("position cannot be negative")
	ErrInvalidTarget       = errors.New("invalid target")

	// Type validation errors
	ErrNotString                 = errors.New("value is not a string")
	ErrNotNumber                 = errors.New("value must be a number")
	ErrNotObject                 = errors.New("value is not an object")
	ErrInvalidType               = errors.New("invalid type")
	ErrEmptyTypeList             = errors.New("empty type list")
	ErrContainsValueMustBeString = errors.New("contains operation value must be a string")

	// Operation execution errors
	ErrTestFailed          = errors.New("test failed")
	ErrDefinedTestFailed   = errors.New("defined test failed")
	ErrUndefinedTestFailed = errors.New("undefined test failed")
	ErrAndTestFailed       = errors.New("and test failed")
	ErrOrTestFailed        = errors.New("or test failed")
	ErrNotTestFailed       = errors.New("not test failed")

	// Value operation errors
	ErrCannotReplace          = errors.New("path not found for replace")
	ErrCannotAddToValue       = errors.New("cannot add to non-object/non-array value")
	ErrCannotRemoveFromValue  = errors.New("cannot remove from non-object/non-array document")
	ErrCannotMoveIntoChildren = errors.New("cannot move into own children")
	ErrPropertiesNil          = errors.New("properties cannot be nil")
	ErrValuesArrayEmpty       = errors.New("'in' operation 'value' must be an array")

	// Key type errors
	ErrInvalidKeyTypeMap     = errors.New("invalid key type for map")
	ErrInvalidKeyTypeSlice   = errors.New("invalid key type for slice")
	ErrUnsupportedParentType = errors.New("unsupported parent type")

	// String operation errors
	ErrPositionOutOfStringRange = errors.New("position out of string range")
	ErrSubstringTooLong         = errors.New("value too long")
	ErrSubstringMismatch        = errors.New("substring does not match")
	ErrStringLengthMismatch     = errors.New("string length mismatch")
	ErrPatternEmpty             = errors.New("pattern cannot be empty")
	ErrLengthNegative           = errors.New("length cannot be negative")

	// Type comparison errors
	ErrTypeMismatch     = errors.New("type mismatch")
	ErrContainsMismatch = errors.New("contains check failed")

	// Predicate operation errors
	ErrInvalidPredicateInAnd = errors.New("invalid predicate in and operation")
	ErrInvalidPredicateInNot = errors.New("invalid predicate in not operation")
	ErrInvalidPredicateInOr  = errors.New("invalid predicate in or operation")
	ErrNotNoOperands         = errors.New("not operation requires operands")

	// Operation modification errors
	ErrCannotModifyRootArray   = errors.New("cannot modify root array directly")
	ErrCannotUpdateParent      = errors.New("cannot update parent")
	ErrCannotUpdateGrandparent = errors.New("cannot update grandparent")

	// Value conversion errors
	ErrCannotConvertNilToString = errors.New("cannot convert nil to string")
	ErrCannotConvertToString    = errors.New("cannot convert value to string")

	// Test operation errors
	ErrTestOperationNumberStringMismatch = errors.New("number is not equal to string")
	ErrTestOperationStringNotEquivalent  = errors.New("string not equivalent")

	// Base errors for dynamic wrapping with fmt.Errorf
	ErrComparisonFailed    = errors.New("comparison failed")
	ErrStringMismatch      = errors.New("string mismatch")
	ErrTestOperationFailed = errors.New("test operation failed")
	ErrInvalidIndex        = errors.New("invalid index")
	ErrRegexPattern        = errors.New("regex pattern error")
	ErrOperationFailed     = errors.New("operation failed")
)

Sentinel errors for path and validation related operations

Functions

func DeepClone

func DeepClone(value any) (any, error)

DeepClone performs a deep clone of a value.

func ToFloat64 added in v0.4.5

func ToFloat64(val any) (float64, bool)

ToFloat64 converts a value to float64, handling various numeric types, booleans, and strings. This matches JavaScript's Number() behavior for type coercion. Optimized for common numeric types with fast paths.

Types

type AddOperation added in v0.4.3

type AddOperation struct {
	BaseOp
	Value any `json:"value"` // Value to add
}

AddOperation represents an add operation that adds a value at a specified path.

func NewAdd added in v0.4.3

func NewAdd(path []string, value any) *AddOperation

NewAdd creates a new add operation.

func (*AddOperation) Apply added in v0.4.3

func (a *AddOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the add operation.

func (*AddOperation) Code added in v0.4.3

func (a *AddOperation) Code() int

Code returns the operation code.

func (*AddOperation) Op added in v0.4.3

func (a *AddOperation) Op() internal.OpType

Op returns the operation type.

func (*AddOperation) ToCompact added in v0.4.3

func (a *AddOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*AddOperation) ToJSON added in v0.4.3

func (a *AddOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*AddOperation) Validate added in v0.4.3

func (a *AddOperation) Validate() error

Validate validates the add operation.

type AndOperation added in v0.4.3

type AndOperation struct {
	BaseOp
	Operations []any `json:"apply"` // Array of operations to apply
}

AndOperation represents an and operation that combines multiple predicate operations.

func NewAnd added in v0.4.3

func NewAnd(path []string, ops []any) *AndOperation

NewAnd creates a new AND operation.

func (*AndOperation) Apply added in v0.4.3

func (ao *AndOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the AND operation.

func (*AndOperation) Code added in v0.4.3

func (ao *AndOperation) Code() int

Code returns the operation code.

func (*AndOperation) Op added in v0.4.3

func (ao *AndOperation) Op() internal.OpType

Op returns the operation type.

func (*AndOperation) Ops added in v0.4.3

func (ao *AndOperation) Ops() []internal.PredicateOp

Ops returns the predicate operations.

func (*AndOperation) Test added in v0.4.3

func (ao *AndOperation) Test(doc any) (bool, error)

Test performs the AND operation.

func (*AndOperation) ToCompact added in v0.4.3

func (ao *AndOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*AndOperation) ToJSON added in v0.4.3

func (ao *AndOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*AndOperation) Validate added in v0.4.3

func (ao *AndOperation) Validate() error

Validate validates the AND operation.

type BaseOp

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

BaseOp provides common functionality for all operations. It stores the target path and optional source path (for move/copy operations). All operation types embed BaseOp to inherit path management functionality.

func NewBaseOp

func NewBaseOp(path []string) BaseOp

NewBaseOp creates a new BaseOp with the given path. The path is a JSON Pointer path represented as a slice of string segments.

func NewBaseOpWithFrom

func NewBaseOpWithFrom(path, from []string) BaseOp

NewBaseOpWithFrom creates a new BaseOp with both target path and source path. This is used for move and copy operations that require both paths.

func (*BaseOp) From

func (b *BaseOp) From() []string

From returns the from path for move/copy operations.

func (*BaseOp) HasFrom

func (b *BaseOp) HasFrom() bool

HasFrom returns true if the operation has a from path.

func (*BaseOp) Not added in v0.5.11

func (b *BaseOp) Not() bool

Not returns false by default. Operations that support negation override this.

func (*BaseOp) Path

func (b *BaseOp) Path() []string

Path returns the operation path.

type ContainsOperation added in v0.4.3

type ContainsOperation struct {
	BaseOp
	Value      string `json:"value"`       // Substring to search for
	IgnoreCase bool   `json:"ignore_case"` // Whether to ignore case when comparing
}

ContainsOperation represents a contains operation that tests if a string contains a substring.

func NewContains added in v0.4.3

func NewContains(path []string, substring string) *ContainsOperation

NewContains creates a new contains operation.

func NewContainsWithIgnoreCase added in v0.4.3

func NewContainsWithIgnoreCase(path []string, substring string, ignoreCase bool) *ContainsOperation

NewContainsWithIgnoreCase creates a new contains operation with ignore case option.

func (*ContainsOperation) Apply added in v0.4.3

func (co *ContainsOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the contains test operation to the document.

func (*ContainsOperation) Code added in v0.4.3

func (co *ContainsOperation) Code() int

Code returns the operation code.

func (*ContainsOperation) Op added in v0.4.3

Op returns the operation type.

func (*ContainsOperation) Test added in v0.4.3

func (co *ContainsOperation) Test(doc any) (bool, error)

Test performs the contains test operation.

func (*ContainsOperation) ToCompact added in v0.4.3

func (co *ContainsOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*ContainsOperation) ToJSON added in v0.4.3

func (co *ContainsOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*ContainsOperation) Validate added in v0.4.3

func (co *ContainsOperation) Validate() error

Validate validates the contains operation.

type CopyOperation added in v0.4.3

type CopyOperation struct {
	BaseOp
}

CopyOperation represents a copy operation that copies a value from one path to another.

func NewCopy added in v0.4.3

func NewCopy(path, from []string) *CopyOperation

NewCopy creates a new copy operation.

func (*CopyOperation) Apply added in v0.4.3

func (c *CopyOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the copy operation.

func (*CopyOperation) Code added in v0.4.3

func (c *CopyOperation) Code() int

Code returns the operation code.

func (*CopyOperation) Op added in v0.4.3

func (c *CopyOperation) Op() internal.OpType

Op returns the operation type.

func (*CopyOperation) ToCompact added in v0.4.3

func (c *CopyOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*CopyOperation) ToJSON added in v0.4.3

func (c *CopyOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*CopyOperation) Validate added in v0.4.3

func (c *CopyOperation) Validate() error

Validate validates the copy operation.

type DefinedOperation added in v0.4.3

type DefinedOperation struct {
	BaseOp
}

DefinedOperation represents a test operation that checks if a path is defined.

func NewDefined added in v0.4.3

func NewDefined(path []string) *DefinedOperation

NewDefined creates a new defined operation.

func (*DefinedOperation) Apply added in v0.4.3

func (d *DefinedOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the defined operation.

func (*DefinedOperation) Code added in v0.4.3

func (d *DefinedOperation) Code() int

Code returns the operation code.

func (*DefinedOperation) Op added in v0.4.3

Op returns the operation type.

func (*DefinedOperation) Test added in v0.4.3

func (d *DefinedOperation) Test(doc any) (bool, error)

Test performs the defined operation.

func (*DefinedOperation) ToCompact added in v0.4.3

func (d *DefinedOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*DefinedOperation) ToJSON added in v0.4.3

func (d *DefinedOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*DefinedOperation) Validate added in v0.4.3

func (d *DefinedOperation) Validate() error

Validate validates the defined operation.

type EndsOperation added in v0.4.3

type EndsOperation struct {
	BaseOp
	Value      string `json:"value"`       // Expected suffix
	IgnoreCase bool   `json:"ignore_case"` // Whether to ignore case
}

EndsOperation represents a test operation that checks if a string value ends with a specific suffix.

func NewEnds added in v0.4.3

func NewEnds(path []string, suffix string) *EndsOperation

NewEnds creates a new ends operation.

func NewEndsWithIgnoreCase added in v0.4.3

func NewEndsWithIgnoreCase(path []string, suffix string, ignoreCase bool) *EndsOperation

NewEndsWithIgnoreCase creates a new ends operation with ignore case option.

func (*EndsOperation) Apply added in v0.4.3

func (e *EndsOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the ends test operation to the document.

func (*EndsOperation) Code added in v0.4.3

func (e *EndsOperation) Code() int

Code returns the operation code.

func (*EndsOperation) Op added in v0.4.3

func (e *EndsOperation) Op() internal.OpType

Op returns the operation type.

func (*EndsOperation) Test added in v0.4.3

func (e *EndsOperation) Test(doc any) (bool, error)

Test evaluates the ends predicate condition.

func (*EndsOperation) ToCompact added in v0.4.3

func (e *EndsOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*EndsOperation) ToJSON added in v0.4.3

func (e *EndsOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*EndsOperation) Validate added in v0.4.3

func (e *EndsOperation) Validate() error

Validate validates the ends operation.

type ExtendOperation added in v0.4.3

type ExtendOperation struct {
	BaseOp
	Properties map[string]any `json:"props"`      // Properties to add
	DeleteNull bool           `json:"deleteNull"` // Whether to delete null properties
}

ExtendOperation represents an object extend operation. path: target path props: properties to add/update deleteNull: whether to delete properties with null values Only supports object type fields.

func NewExtend added in v0.4.3

func NewExtend(path []string, properties map[string]any, deleteNull bool) *ExtendOperation

NewExtend creates a new object extend operation.

func (*ExtendOperation) Apply added in v0.4.3

func (ex *ExtendOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the object extend operation.

func (*ExtendOperation) Code added in v0.4.3

func (ex *ExtendOperation) Code() int

Code returns the operation code.

func (*ExtendOperation) Op added in v0.4.3

func (ex *ExtendOperation) Op() internal.OpType

Op returns the operation type.

func (*ExtendOperation) ToCompact added in v0.4.3

func (ex *ExtendOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*ExtendOperation) ToJSON added in v0.4.3

func (ex *ExtendOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*ExtendOperation) Validate added in v0.4.3

func (ex *ExtendOperation) Validate() error

Validate validates the extend operation.

type FlipOperation added in v0.4.3

type FlipOperation struct {
	BaseOp
}

FlipOperation represents a flip operation that inverts boolean values or converts other types to boolean and then inverts them.

func NewFlip added in v0.4.3

func NewFlip(path []string) *FlipOperation

NewFlip creates a new flip operation.

func (*FlipOperation) Apply added in v0.4.3

func (f *FlipOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the flip operation to the document.

func (*FlipOperation) Code added in v0.4.3

func (f *FlipOperation) Code() int

Code returns the operation code.

func (*FlipOperation) Op added in v0.4.3

func (f *FlipOperation) Op() internal.OpType

Op returns the operation type.

func (*FlipOperation) ToCompact added in v0.4.3

func (f *FlipOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*FlipOperation) ToJSON added in v0.4.3

func (f *FlipOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*FlipOperation) Validate added in v0.4.3

func (f *FlipOperation) Validate() error

Validate validates the flip operation.

type InOperation added in v0.4.3

type InOperation struct {
	BaseOp
	Value []any `json:"value"` // Array of values to check against
}

InOperation represents a test operation that checks if a value is present in a specified array.

func NewIn added in v0.4.3

func NewIn(path []string, values []any) *InOperation

NewIn creates a new in operation.

func (*InOperation) Apply added in v0.4.3

func (in *InOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the in test operation to the document.

func (*InOperation) Code added in v0.4.3

func (in *InOperation) Code() int

Code returns the operation code.

func (*InOperation) Op added in v0.4.3

func (in *InOperation) Op() internal.OpType

Op returns the operation type.

func (*InOperation) Test added in v0.4.3

func (in *InOperation) Test(doc any) (bool, error)

Test evaluates the in predicate condition.

func (*InOperation) ToCompact added in v0.4.3

func (in *InOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*InOperation) ToJSON added in v0.4.3

func (in *InOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*InOperation) Validate added in v0.4.3

func (in *InOperation) Validate() error

Validate validates the in operation.

type IncOperation added in v0.4.3

type IncOperation struct {
	BaseOp
	Inc float64 `json:"inc"` // Increment value
}

IncOperation represents an increment operation that increments a numeric value.

func NewInc added in v0.4.3

func NewInc(path []string, inc float64) *IncOperation

NewInc creates a new increment operation.

func (*IncOperation) Apply added in v0.4.3

func (ic *IncOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the increment operation to the document.

func (*IncOperation) Code added in v0.4.3

func (ic *IncOperation) Code() int

Code returns the operation code.

func (*IncOperation) Op added in v0.4.3

func (ic *IncOperation) Op() internal.OpType

Op returns the operation type.

func (*IncOperation) ToCompact added in v0.4.3

func (ic *IncOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*IncOperation) ToJSON added in v0.4.3

func (ic *IncOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*IncOperation) Validate added in v0.4.3

func (ic *IncOperation) Validate() error

Validate validates the increment operation.

type LessOperation added in v0.4.3

type LessOperation struct {
	BaseOp
	Value float64 `json:"value"` // Value to compare against
}

LessOperation represents a test operation that checks if a numeric value is less than a specified value.

func NewLess added in v0.5.10

func NewLess(path []string, value float64) *LessOperation

NewLess creates a new less operation.

func (*LessOperation) Apply added in v0.4.3

func (l *LessOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the less test operation to the document.

func (*LessOperation) Code added in v0.4.3

func (l *LessOperation) Code() int

Code returns the operation code.

func (*LessOperation) Op added in v0.4.3

func (l *LessOperation) Op() internal.OpType

Op returns the operation type.

func (*LessOperation) Test added in v0.4.3

func (l *LessOperation) Test(doc any) (bool, error)

Test evaluates the less predicate condition.

func (*LessOperation) ToCompact added in v0.4.3

func (l *LessOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*LessOperation) ToJSON added in v0.4.3

func (l *LessOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*LessOperation) Validate added in v0.4.3

func (l *LessOperation) Validate() error

Validate validates the less operation.

type MatchesOperation added in v0.4.3

type MatchesOperation struct {
	BaseOp
	Pattern    string `json:"value"`       // The regex pattern string
	IgnoreCase bool   `json:"ignore_case"` // Case insensitive flag
	// contains filtered or unexported fields
}

MatchesOperation represents a "matches" predicate operation that checks if a string matches a regex pattern.

func NewMatches added in v0.4.3

func NewMatches(path []string, pattern string, ignoreCase bool, createMatcher internal.CreateRegexMatcher) *MatchesOperation

NewMatches creates a new matches operation. If createMatcher is nil, uses the default Go regexp implementation. This aligns with json-joy's OpMatches constructor pattern.

func (*MatchesOperation) Apply added in v0.4.3

func (ma *MatchesOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the matches operation.

func (*MatchesOperation) Code added in v0.4.3

func (ma *MatchesOperation) Code() int

Code returns the operation code.

func (*MatchesOperation) Op added in v0.4.3

func (ma *MatchesOperation) Op() internal.OpType

Op returns the operation type.

func (*MatchesOperation) Test added in v0.4.3

func (ma *MatchesOperation) Test(doc any) (bool, error)

Test evaluates the matches predicate condition.

func (*MatchesOperation) ToCompact added in v0.4.3

func (ma *MatchesOperation) ToCompact() (internal.CompactOperation, error)

ToCompact converts the operation to compact array representation.

func (*MatchesOperation) ToJSON added in v0.4.3

func (ma *MatchesOperation) ToJSON() (internal.Operation, error)

ToJSON converts the operation to JSON representation.

func (*MatchesOperation) Validate added in v0.4.3

func (ma *MatchesOperation) Validate() error

Validate validates the matches operation.

type MergeOperation added in v0.4.3

type MergeOperation struct {
	BaseOp
	Pos   float64        `json:"pos"`   // Merge position
	Props map[string]any `json:"props"` // Properties to apply after merge
}

MergeOperation represents an array merge operation. path: target path pos: merge position (array index) props: properties to apply after merge (can be nil) Only supports array type fields.

func NewMerge added in v0.4.3

func NewMerge(path []string, pos float64, props map[string]any) *MergeOperation

NewMerge creates a new merge operation.

func (*MergeOperation) Apply added in v0.4.3

func (mg *MergeOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the merge operation following TypeScript reference.

func (*MergeOperation) Code added in v0.4.3

func (mg *MergeOperation) Code() int

Code returns the operation code.

func (*MergeOperation) Op added in v0.4.3

func (mg *MergeOperation) Op() internal.OpType

Op returns the operation type.

func (*MergeOperation) ToCompact added in v0.4.3

func (mg *MergeOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*MergeOperation) ToJSON added in v0.4.3

func (mg *MergeOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*MergeOperation) Validate added in v0.4.3

func (mg *MergeOperation) Validate() error

Validate validates the merge operation.

type MoreOperation added in v0.4.3

type MoreOperation struct {
	BaseOp
	Value float64 `json:"value"` // The number to compare against
}

MoreOperation represents a "more" predicate operation that checks if a value is greater than a specified number.

func NewMore added in v0.4.3

func NewMore(path []string, value float64) *MoreOperation

NewMore creates a new more operation.

func (*MoreOperation) Apply added in v0.4.3

func (mo *MoreOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the more operation.

func (*MoreOperation) Code added in v0.4.3

func (mo *MoreOperation) Code() int

Code returns the operation code.

func (*MoreOperation) Op added in v0.4.3

func (mo *MoreOperation) Op() internal.OpType

Op returns the operation type.

func (*MoreOperation) Test added in v0.4.3

func (mo *MoreOperation) Test(doc any) (bool, error)

Test evaluates the more predicate condition.

func (*MoreOperation) ToCompact added in v0.4.3

func (mo *MoreOperation) ToCompact() (internal.CompactOperation, error)

ToCompact converts the operation to compact array representation.

func (*MoreOperation) ToJSON added in v0.4.3

func (mo *MoreOperation) ToJSON() (internal.Operation, error)

ToJSON converts the operation to JSON representation.

func (*MoreOperation) Validate added in v0.4.3

func (mo *MoreOperation) Validate() error

Validate validates the more operation.

type MoveOperation added in v0.4.3

type MoveOperation struct {
	BaseOp
}

MoveOperation represents a move operation that moves a value from one path to another.

func NewMove added in v0.4.3

func NewMove(path, from []string) *MoveOperation

NewMove creates a new move operation.

func (*MoveOperation) Apply added in v0.4.3

func (m *MoveOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the move operation following RFC 6902: remove then add.

func (*MoveOperation) Code added in v0.4.3

func (m *MoveOperation) Code() int

Code returns the operation code.

func (*MoveOperation) Op added in v0.4.3

func (m *MoveOperation) Op() internal.OpType

Op returns the operation type.

func (*MoveOperation) ToCompact added in v0.4.3

func (m *MoveOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*MoveOperation) ToJSON added in v0.4.3

func (m *MoveOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*MoveOperation) Validate added in v0.4.3

func (m *MoveOperation) Validate() error

Validate validates the move operation.

type NotOperation added in v0.4.3

type NotOperation struct {
	BaseOp
	Operations []any `json:"apply"` // Array of operations to apply (then negate)
}

NotOperation represents a logical NOT operation that negates predicates.

func NewNot added in v0.4.3

func NewNot(operand internal.PredicateOp) *NotOperation

NewNot creates a new NOT operation.

func NewNotMultiple added in v0.4.3

func NewNotMultiple(path []string, ops []any) *NotOperation

NewNotMultiple creates a new NOT operation with multiple operands.

func (*NotOperation) Apply added in v0.4.3

func (n *NotOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the NOT operation.

func (*NotOperation) Code added in v0.4.3

func (n *NotOperation) Code() int

Code returns the operation code.

func (*NotOperation) Not added in v0.4.3

func (n *NotOperation) Not() bool

Not returns true since this is a NOT operation.

func (*NotOperation) Op added in v0.4.3

func (n *NotOperation) Op() internal.OpType

Op returns the operation type.

func (*NotOperation) Ops added in v0.4.3

func (n *NotOperation) Ops() []internal.PredicateOp

Ops returns the operand operations.

func (*NotOperation) Test added in v0.4.3

func (n *NotOperation) Test(doc any) (bool, error)

Test evaluates the NOT predicate condition.

func (*NotOperation) ToCompact added in v0.4.3

func (n *NotOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*NotOperation) ToJSON added in v0.4.3

func (n *NotOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*NotOperation) Validate added in v0.4.3

func (n *NotOperation) Validate() error

Validate validates the NOT operation.

type Op

type Op = internal.Op

Op interface defines the core operation behavior.

type OrOperation added in v0.4.3

type OrOperation struct {
	BaseOp
	Operations []any `json:"apply"` // Array of operations to apply
}

OrOperation represents an OR operation that combines multiple predicate operations.

func NewOr added in v0.4.3

func NewOr(path []string, ops []any) *OrOperation

NewOr creates a new OR operation.

func (*OrOperation) Apply added in v0.4.3

func (oo *OrOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the OR operation to the document.

func (*OrOperation) Code added in v0.4.3

func (oo *OrOperation) Code() int

Code returns the operation code.

func (*OrOperation) Op added in v0.4.3

func (oo *OrOperation) Op() internal.OpType

Op returns the operation type.

func (*OrOperation) Ops added in v0.4.3

func (oo *OrOperation) Ops() []internal.PredicateOp

Ops returns the predicate operations.

func (*OrOperation) Test added in v0.4.3

func (oo *OrOperation) Test(doc any) (bool, error)

Test performs the OR operation.

func (*OrOperation) ToCompact added in v0.4.3

func (oo *OrOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*OrOperation) ToJSON added in v0.4.3

func (oo *OrOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*OrOperation) Validate added in v0.4.3

func (oo *OrOperation) Validate() error

Validate validates the OR operation.

type PredicateOp

type PredicateOp = internal.PredicateOp

PredicateOp represents predicate operations used for testing conditions.

type RemoveOperation added in v0.4.3

type RemoveOperation struct {
	BaseOp
	OldValue    any  `json:"oldValue,omitempty"` // The value that was removed (optional)
	HasOldValue bool // Whether oldValue is explicitly set
}

RemoveOperation represents a remove operation that removes a value at a specified path.

func NewRemove added in v0.4.3

func NewRemove(path []string) *RemoveOperation

NewRemove creates a new remove operation.

func NewRemoveWithOldValue added in v0.4.3

func NewRemoveWithOldValue(path []string, oldValue any) *RemoveOperation

NewRemoveWithOldValue creates a new remove operation with oldValue.

func (*RemoveOperation) Apply added in v0.4.3

func (r *RemoveOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the remove operation to the document.

func (*RemoveOperation) Code added in v0.4.3

func (r *RemoveOperation) Code() int

Code returns the operation code.

func (*RemoveOperation) Op added in v0.4.3

Op returns the operation type.

func (*RemoveOperation) ToCompact added in v0.4.3

func (r *RemoveOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*RemoveOperation) ToJSON added in v0.4.3

func (r *RemoveOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*RemoveOperation) Validate added in v0.4.3

func (r *RemoveOperation) Validate() error

Validate validates the remove operation.

type ReplaceOperation added in v0.4.3

type ReplaceOperation struct {
	BaseOp
	Value    any `json:"value"`              // New value
	OldValue any `json:"oldValue,omitempty"` // The value that was replaced (optional)
}

ReplaceOperation represents a replace operation that replaces a value at a specified path.

func NewReplace added in v0.4.3

func NewReplace(path []string, value any) *ReplaceOperation

NewReplace creates a new replace operation.

func NewReplaceWithOldValue added in v0.4.3

func NewReplaceWithOldValue(path []string, value any, oldValue any) *ReplaceOperation

NewReplaceWithOldValue creates a new replace operation with oldValue.

func (*ReplaceOperation) Apply added in v0.4.3

func (rp *ReplaceOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the replace operation to the document.

func (*ReplaceOperation) Code added in v0.4.3

func (rp *ReplaceOperation) Code() int

Code returns the operation code.

func (*ReplaceOperation) Op added in v0.4.3

func (rp *ReplaceOperation) Op() internal.OpType

Op returns the operation type.

func (*ReplaceOperation) ToCompact added in v0.4.3

func (rp *ReplaceOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*ReplaceOperation) ToJSON added in v0.4.3

func (rp *ReplaceOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*ReplaceOperation) Validate added in v0.4.3

func (rp *ReplaceOperation) Validate() error

Validate validates the replace operation.

type Result added in v0.4.3

type Result[T internal.Document] = internal.OpResult[T]

Result represents the result of applying an operation.

type SecondOrderPredicateOp

type SecondOrderPredicateOp = internal.SecondOrderPredicateOp

SecondOrderPredicateOp represents operations that combine multiple predicate operations.

type SplitOperation added in v0.4.3

type SplitOperation struct {
	BaseOp
	Pos   float64 `json:"pos"`   // Split position
	Props any     `json:"props"` // Properties to apply after split
}

SplitOperation represents a string split operation. path: target path pos: split position (rune index) props: properties to apply after split (can be nil) Only supports string type fields.

func NewSplit added in v0.4.3

func NewSplit(path []string, pos float64, props any) *SplitOperation

NewSplit creates a new split operation.

func (*SplitOperation) Apply added in v0.4.3

func (sp *SplitOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the split operation following TypeScript reference.

func (*SplitOperation) Code added in v0.4.3

func (sp *SplitOperation) Code() int

Code returns the operation code.

func (*SplitOperation) Op added in v0.4.3

func (sp *SplitOperation) Op() internal.OpType

Op returns the operation type.

func (*SplitOperation) ToCompact added in v0.4.3

func (sp *SplitOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*SplitOperation) ToJSON added in v0.4.3

func (sp *SplitOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*SplitOperation) Validate added in v0.4.3

func (sp *SplitOperation) Validate() error

Validate validates the split operation.

type StartsOperation added in v0.4.3

type StartsOperation struct {
	BaseOp
	Value      string `json:"value"`       // Expected prefix
	IgnoreCase bool   `json:"ignore_case"` // Whether to ignore case
}

StartsOperation represents a test operation that checks if a string value starts with a specific prefix.

func NewStarts added in v0.4.3

func NewStarts(path []string, prefix string) *StartsOperation

NewStarts creates a new starts operation.

func NewStartsWithIgnoreCase added in v0.4.3

func NewStartsWithIgnoreCase(path []string, prefix string, ignoreCase bool) *StartsOperation

NewStartsWithIgnoreCase creates a new starts operation with ignore case option.

func (*StartsOperation) Apply added in v0.4.3

func (s *StartsOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the starts test operation to the document.

func (*StartsOperation) Code added in v0.4.3

func (s *StartsOperation) Code() int

Code returns the operation code.

func (*StartsOperation) Op added in v0.4.3

Op returns the operation type.

func (*StartsOperation) Test added in v0.4.3

func (s *StartsOperation) Test(doc any) (bool, error)

Test evaluates the starts predicate condition.

func (*StartsOperation) ToCompact added in v0.4.3

func (s *StartsOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*StartsOperation) ToJSON added in v0.4.3

func (s *StartsOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*StartsOperation) Validate added in v0.4.3

func (s *StartsOperation) Validate() error

Validate validates the starts operation.

type StrDelOperation added in v0.4.3

type StrDelOperation struct {
	BaseOp
	Pos float64 `json:"pos"` // Delete position
	Len float64 `json:"len"` // Number of characters to delete
	Str string  `json:"str"` // Specific string to delete (optional)
}

StrDelOperation represents a string delete operation. path: target path pos: start position (rune index) len: number of runes to delete (when Str is empty) str: specific string to delete (when not empty, takes precedence) Only supports string type fields.

func NewStrDel added in v0.4.3

func NewStrDel(path []string, pos, length float64) *StrDelOperation

NewStrDel creates a new string delete operation with length.

func NewStrDelWithStr added in v0.4.3

func NewStrDelWithStr(path []string, pos float64, str string) *StrDelOperation

NewStrDelWithStr creates a new string delete operation with specific string.

func (*StrDelOperation) Apply added in v0.4.3

func (sd *StrDelOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the string delete operation.

func (*StrDelOperation) Code added in v0.4.3

func (sd *StrDelOperation) Code() int

Code returns the operation code.

func (*StrDelOperation) Op added in v0.4.3

func (sd *StrDelOperation) Op() internal.OpType

Op returns the operation type.

func (*StrDelOperation) ToCompact added in v0.4.3

func (sd *StrDelOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*StrDelOperation) ToJSON added in v0.4.3

func (sd *StrDelOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*StrDelOperation) Validate added in v0.4.3

func (sd *StrDelOperation) Validate() error

Validate validates the string delete operation.

type StrInsOperation added in v0.4.3

type StrInsOperation struct {
	BaseOp
	Pos float64 `json:"pos"` // Insert position
	Str string  `json:"str"` // String to insert
}

StrInsOperation represents a string insert operation. path: target path pos: insert position (rune index) str: string to insert Only supports string type fields.

func NewStrIns added in v0.4.3

func NewStrIns(path []string, pos float64, str string) *StrInsOperation

NewStrIns creates a new string insert operation.

func (*StrInsOperation) Apply added in v0.4.3

func (si *StrInsOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the string insert operation.

func (*StrInsOperation) Code added in v0.4.3

func (si *StrInsOperation) Code() int

Code returns the operation code.

func (*StrInsOperation) Op added in v0.4.3

func (si *StrInsOperation) Op() internal.OpType

Op returns the operation type.

func (*StrInsOperation) ToCompact added in v0.4.3

func (si *StrInsOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*StrInsOperation) ToJSON added in v0.4.3

func (si *StrInsOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*StrInsOperation) Validate added in v0.4.3

func (si *StrInsOperation) Validate() error

Validate validates the string insert operation.

type TestOperation added in v0.4.3

type TestOperation struct {
	BaseOp
	Value   any  `json:"value"`         // Expected value
	NotFlag bool `json:"not,omitempty"` // Whether to negate the test
}

TestOperation represents a test operation that checks if a value equals a specified value.

func NewTest added in v0.4.3

func NewTest(path []string, value any) *TestOperation

NewTest creates a new test operation.

func NewTestWithNot added in v0.5.14

func NewTestWithNot(path []string, value any, not bool) *TestOperation

NewTestWithNot creates a new test operation with not flag.

func (*TestOperation) Apply added in v0.4.3

func (t *TestOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the test operation.

func (*TestOperation) Code added in v0.4.3

func (t *TestOperation) Code() int

Code returns the operation code.

func (*TestOperation) Not added in v0.4.3

func (t *TestOperation) Not() bool

Not returns whether this operation is negated.

func (*TestOperation) Op added in v0.4.3

func (t *TestOperation) Op() internal.OpType

Op returns the operation type.

func (*TestOperation) Test added in v0.4.3

func (t *TestOperation) Test(doc any) (bool, error)

Test performs the test operation.

func (*TestOperation) ToCompact added in v0.4.3

func (t *TestOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*TestOperation) ToJSON added in v0.4.3

func (t *TestOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*TestOperation) Validate added in v0.4.3

func (t *TestOperation) Validate() error

Validate validates the test operation.

type TestStringLenOperation added in v0.4.3

type TestStringLenOperation struct {
	BaseOp
	Length  float64 `json:"len"` // Expected string length
	NotFlag bool    `json:"not"` // Whether to negate the result
}

TestStringLenOperation represents a test operation that checks if a string value has a specific length.

func NewTestStringLen added in v0.4.3

func NewTestStringLen(path []string, expectedLength float64) *TestStringLenOperation

NewTestStringLen creates a new test string length operation.

func NewTestStringLenWithNot added in v0.4.3

func NewTestStringLenWithNot(path []string, expectedLength float64, not bool) *TestStringLenOperation

NewTestStringLenWithNot creates a new test string length operation with not flag.

func (*TestStringLenOperation) Apply added in v0.4.3

func (tl *TestStringLenOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the test string length operation to the document.

func (*TestStringLenOperation) Code added in v0.4.3

func (tl *TestStringLenOperation) Code() int

Code returns the operation code.

func (*TestStringLenOperation) Not added in v0.4.3

func (tl *TestStringLenOperation) Not() bool

Not returns whether this is a negation predicate.

func (*TestStringLenOperation) Op added in v0.4.3

Op returns the operation type.

func (*TestStringLenOperation) Test added in v0.5.1

func (tl *TestStringLenOperation) Test(doc any) (bool, error)

Test tests the string length condition on the document.

func (*TestStringLenOperation) ToCompact added in v0.4.3

ToCompact serializes the operation to compact format.

func (*TestStringLenOperation) ToJSON added in v0.4.3

ToJSON serializes the operation to JSON format.

func (*TestStringLenOperation) Validate added in v0.4.3

func (tl *TestStringLenOperation) Validate() error

Validate validates the test string length operation.

type TestStringOperation added in v0.4.3

type TestStringOperation struct {
	BaseOp
	Str        string `json:"str"`                   // Expected string value
	Pos        int    `json:"pos"`                   // Position within string
	NotFlag    bool   `json:"not,omitempty"`         // Whether to negate the result
	IgnoreCase bool   `json:"ignore_case,omitempty"` // Whether to ignore case
}

TestStringOperation represents a test operation that checks if a value is a string and matches a pattern.

func NewTestString added in v0.4.3

func NewTestString(path []string, str string, pos float64, not bool, ignoreCase bool) *TestStringOperation

NewTestString creates a new test string operation.

func (*TestStringOperation) Apply added in v0.4.3

func (ts *TestStringOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the test string operation to the document.

func (*TestStringOperation) Code added in v0.4.3

func (ts *TestStringOperation) Code() int

Code returns the operation code.

func (*TestStringOperation) Not added in v0.5.1

func (ts *TestStringOperation) Not() bool

Not returns whether this operation is a negation predicate.

func (*TestStringOperation) Op added in v0.4.3

Op returns the operation type.

func (*TestStringOperation) Test added in v0.4.3

func (ts *TestStringOperation) Test(doc any) (bool, error)

Test evaluates the test string predicate condition.

func (*TestStringOperation) ToCompact added in v0.4.3

ToCompact serializes the operation to compact format.

func (*TestStringOperation) ToJSON added in v0.4.3

func (ts *TestStringOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*TestStringOperation) Validate added in v0.4.3

func (ts *TestStringOperation) Validate() error

Validate validates the test string operation.

type TestTypeOperation added in v0.4.3

type TestTypeOperation struct {
	BaseOp
	Types []string `json:"type"` // Expected type names
}

TestTypeOperation represents a test operation that checks if a value is of a specific type.

func NewTestType added in v0.4.3

func NewTestType(path []string, expectedType string) *TestTypeOperation

NewTestType creates a new test type operation.

func NewTestTypeMultiple added in v0.4.3

func NewTestTypeMultiple(path []string, expectedTypes []string) *TestTypeOperation

NewTestTypeMultiple creates a new test type operation with multiple types.

func (*TestTypeOperation) Apply added in v0.4.3

func (tt *TestTypeOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the test type operation to the document.

func (*TestTypeOperation) Code added in v0.4.3

func (tt *TestTypeOperation) Code() int

Code returns the operation code.

func (*TestTypeOperation) Op added in v0.4.3

Op returns the operation type.

func (*TestTypeOperation) Test added in v0.4.3

func (tt *TestTypeOperation) Test(doc any) (bool, error)

Test evaluates the test type predicate condition.

func (*TestTypeOperation) ToCompact added in v0.4.3

func (tt *TestTypeOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*TestTypeOperation) ToJSON added in v0.4.3

func (tt *TestTypeOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*TestTypeOperation) Validate added in v0.4.3

func (tt *TestTypeOperation) Validate() error

Validate validates the test type operation.

type TypeOperation added in v0.4.3

type TypeOperation struct {
	BaseOp
	TypeValue string `json:"value"` // Expected type name
}

TypeOperation represents a type operation that checks if a value is of a specific type.

func NewType added in v0.4.3

func NewType(path []string, expectedType string) *TypeOperation

NewType creates a new type operation.

func (*TypeOperation) Apply added in v0.4.3

func (tp *TypeOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the type operation to the document.

func (*TypeOperation) Code added in v0.4.3

func (tp *TypeOperation) Code() int

Code returns the operation code.

func (*TypeOperation) Op added in v0.4.3

func (tp *TypeOperation) Op() internal.OpType

Op returns the operation type.

func (*TypeOperation) Test added in v0.4.3

func (tp *TypeOperation) Test(doc any) (bool, error)

Test evaluates the type predicate condition.

func (*TypeOperation) ToCompact added in v0.4.3

func (tp *TypeOperation) ToCompact() (internal.CompactOperation, error)

ToCompact serializes the operation to compact format.

func (*TypeOperation) ToJSON added in v0.4.3

func (tp *TypeOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*TypeOperation) Validate added in v0.4.3

func (tp *TypeOperation) Validate() error

Validate validates the type operation.

type UndefinedOperation added in v0.4.3

type UndefinedOperation struct {
	BaseOp
}

UndefinedOperation represents an undefined operation that checks if a path doesn't exist.

func NewUndefined added in v0.4.3

func NewUndefined(path []string) *UndefinedOperation

NewUndefined creates a new undefined operation.

func (*UndefinedOperation) Apply added in v0.4.3

func (u *UndefinedOperation) Apply(doc any) (internal.OpResult[any], error)

Apply applies the undefined operation.

func (*UndefinedOperation) Code added in v0.4.3

func (u *UndefinedOperation) Code() int

Code returns the operation code.

func (*UndefinedOperation) Op added in v0.4.3

Op returns the operation type.

func (*UndefinedOperation) Test added in v0.4.3

func (u *UndefinedOperation) Test(doc any) (bool, error)

Test performs the undefined operation.

func (*UndefinedOperation) ToCompact added in v0.4.3

ToCompact serializes the operation to compact format.

func (*UndefinedOperation) ToJSON added in v0.4.3

func (u *UndefinedOperation) ToJSON() (internal.Operation, error)

ToJSON serializes the operation to JSON format.

func (*UndefinedOperation) Validate added in v0.4.3

func (u *UndefinedOperation) Validate() error

Validate validates the undefined operation.

Jump to

Keyboard shortcuts

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