internal

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// JSON Patch (RFC 6902) operations
	OpAddCode     = 0
	OpRemoveCode  = 1
	OpReplaceCode = 2
	OpCopyCode    = 3
	OpMoveCode    = 4
	OpTestCode    = 5

	// String editing
	OpStrInsCode = 6
	OpStrDelCode = 7

	// Extra
	OpFlipCode = 8
	OpIncCode  = 9

	// Slate.js
	OpSplitCode  = 10
	OpMergeCode  = 11
	OpExtendCode = 12

	// JSON Predicate
	OpContainsCode      = 30
	OpDefinedCode       = 31
	OpEndsCode          = 32
	OpInCode            = 33
	OpLessCode          = 34
	OpMatchesCode       = 35
	OpMoreCode          = 36
	OpStartsCode        = 37
	OpUndefinedCode     = 38
	OpTestTypeCode      = 39
	OpTestStringCode    = 40
	OpTestStringLenCode = 41
	OpTypeCode          = 42
	OpAndCode           = 43
	OpNotCode           = 44
	OpOrCode            = 45
)

Operation code constants, numeric codes

Variables

This section is empty.

Functions

func IsFirstOrderPredicateOperation

func IsFirstOrderPredicateOperation(op string) bool

IsFirstOrderPredicateOperation checks if operation is a first-order predicate

func IsJsonPatchExtendedOperation

func IsJsonPatchExtendedOperation(op string) bool

IsJsonPatchExtendedOperation checks if operation is an extended operation.

func IsJsonPatchOperation

func IsJsonPatchOperation(op string) bool

IsJsonPatchOperation checks if operation is a core JSON Patch operation

func IsPredicateOperation

func IsPredicateOperation(op string) bool

IsPredicateOperation checks if operation is a predicate operation

func IsSecondOrderPredicateOperation

func IsSecondOrderPredicateOperation(op string) bool

IsSecondOrderPredicateOperation checks if operation is a second-order predicate.

func IsValidJsonPatchType

func IsValidJsonPatchType(typeStr string) bool

IsValidJsonPatchType checks if a type string is a valid JSON Patch type

Types

type Codec

type Codec interface {
	// Decode decodes a JSON operation object into an Op instance
	Decode(operation Operation) (Op, error)
	// DecodeSlice decodes an array of operations into an Op slice
	DecodeSlice(operations []Operation) ([]Op, error)
	// Encode encodes an Op instance into a JSON operation object
	Encode(op Op) (Operation, error)
	// EncodeSlice encodes an Op slice into an array of operations
	EncodeSlice(ops []Op) ([]Operation, error)
}

Codec is the interface for codecs that provide unified encoding and decoding functionality.

type CompactOperation

type CompactOperation = []interface{}

CompactOperation represents a compact format operation actually []interface{}, but with clearer semantics

type Document added in v0.2.0

type Document interface {
	~[]byte | ~string | map[string]any | any
}

Document defines the supported document types for JSON Patch operations. Supports: structs, map[string]any, []byte (JSON), and string (JSON).

type JsonPatchOptions

type JsonPatchOptions struct {
	CreateMatcher func(pattern string, ignoreCase bool) RegexMatcher
}

JsonPatchOptions contains options for JSON Patch operations. This is kept for decoder compatibility.

type JsonPatchTypes

type JsonPatchTypes string

JsonPatchTypes represents the valid JSON types for type operations.

const (
	JsonPatchTypeString  JsonPatchTypes = "string"
	JsonPatchTypeNumber  JsonPatchTypes = "number"
	JsonPatchTypeBoolean JsonPatchTypes = "boolean"
	JsonPatchTypeObject  JsonPatchTypes = "object"
	JsonPatchTypeInteger JsonPatchTypes = "integer"
	JsonPatchTypeArray   JsonPatchTypes = "array"
	JsonPatchTypeNull    JsonPatchTypes = "null"
)

func GetJsonPatchType

func GetJsonPatchType(value interface{}) JsonPatchTypes

GetJsonPatchType returns the JSON Patch type for a given value

type Op

type Op interface {
	// Op returns the operation type string (e.g. "add", "remove", etc.)
	Op() OpType
	// Code returns the numeric code for the operation (consistent with constants)
	Code() int
	// Path returns the JSON Pointer path for the operation (slice form)
	Path() []string
	// Apply applies the operation to the document, returning the new document and old value
	Apply(doc any) (OpResult[any], error)
	// ToJSON returns the standard JSON Patch format
	ToJSON() (Operation, error)
	// ToCompact returns the compact array format
	ToCompact() (CompactOperation, error)
	// Validate validates the operation parameters
	Validate() error
}

Op is the unified interface for all JSON Patch operations. Any operation must implement these methods.

type OpResult

type OpResult[T Document] struct {
	Doc T           `json:"doc"`
	Old interface{} `json:"old,omitempty"`
}

OpResult represents the result of a single operation with generic type support

type OpType

type OpType string
const (
	// JSON Patch (RFC 6902) operations
	OpAddType     OpType = "add"
	OpRemoveType  OpType = "remove"
	OpReplaceType OpType = "replace"
	OpMoveType    OpType = "move"
	OpCopyType    OpType = "copy"
	OpTestType    OpType = "test"

	// JSON Predicate operations
	OpContainsType      OpType = "contains"
	OpDefinedType       OpType = "defined"
	OpUndefinedType     OpType = "undefined"
	OpTypeType          OpType = "type"
	OpTestTypeType      OpType = "test_type"
	OpTestStringType    OpType = "test_string"
	OpTestStringLenType OpType = "test_string_len"
	OpEndsType          OpType = "ends"
	OpStartsType        OpType = "starts"
	OpInType            OpType = "in"
	OpLessType          OpType = "less"
	OpMoreType          OpType = "more"
	OpMatchesType       OpType = "matches"

	// Composite operations
	OpAndType OpType = "and"
	OpOrType  OpType = "or"
	OpNotType OpType = "not"

	// Extended operations
	OpFlipType   OpType = "flip"
	OpIncType    OpType = "inc"
	OpStrInsType OpType = "str_ins"
	OpStrDelType OpType = "str_del"
	OpSplitType  OpType = "split"
	OpMergeType  OpType = "merge"
	OpExtendType OpType = "extend"
)

type Operation

type Operation = map[string]interface{}

Operation represents a JSON Patch operation object compatible with map format

type Option added in v0.2.0

type Option func(*Options)

Option represents functional options for configuring patch operations. This follows the standard Go functional options pattern.

func WithMatcher added in v0.2.0

func WithMatcher(createMatcher func(pattern string, ignoreCase bool) RegexMatcher) Option

WithMatcher configures a custom regex matcher for pattern operations. The createMatcher function should create a RegexMatcher from a pattern and ignoreCase flag.

func WithMutate added in v0.2.0

func WithMutate(mutate bool) Option

WithMutate configures whether the patch operation should modify the original document. When false (default), returns a new copy. When true, modifies the original.

type Options added in v0.2.0

type Options struct {
	Mutate        bool                                               // Whether to modify the original document
	CreateMatcher func(pattern string, ignoreCase bool) RegexMatcher // Optional regex matcher creator
}

Options holds configuration parameters for patch operations. This is the unified configuration struct following Go best practices.

type PatchResult

type PatchResult[T Document] struct {
	Doc T             // The patched document of the original type
	Res []OpResult[T] // Results of individual patch operations
}

PatchResult represents the result of applying a JSON Patch with generic type support. It contains the patched document and the results of individual operations.

type PredicateOp

type PredicateOp interface {
	Op
	// Test tests the operation on the document, returning whether it passed
	Test(doc any) (bool, error)
	// Not returns whether the operation is a negation predicate
	Not() bool
}

PredicateOp is the interface for all predicate (test-type) operations.

type RegexMatcher

type RegexMatcher func(value string) bool

RegexMatcher is a function type that tests if a value matches a pattern.

type SecondOrderPredicateOp

type SecondOrderPredicateOp interface {
	PredicateOp
	// Ops returns all sub-predicate operations
	Ops() []PredicateOp
}

SecondOrderPredicateOp is the interface for second-order predicate operations that combine multiple predicates.

Jump to

Keyboard shortcuts

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