internal

package
v0.5.10 Latest Latest
Warning

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

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

Documentation

Overview

Package internal provides internal types and constants for JSON Patch operations.

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 added in v0.4.3

func IsJSONPatchExtendedOperation(op string) bool

IsJSONPatchExtendedOperation checks if operation is an extended operation.

func IsJSONPatchOperation added in v0.4.3

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 added in v0.4.3

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 CreateRegexMatcher

type CreateRegexMatcher func(pattern string, ignoreCase bool) RegexMatcher

CreateRegexMatcher is a function type that creates a RegexMatcher from a pattern. This aligns with json-joy's CreateRegexMatcher type. The function should return a matcher that always returns false if the pattern is invalid.

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 added in v0.4.3

type JSONPatchOptions struct {
	CreateMatcher CreateRegexMatcher
}

JSONPatchOptions contains options for JSON Patch decoding operations. This aligns with json-joy's JsonPatchOptions interface.

type JSONPatchTypes added in v0.4.3

type JSONPatchTypes string

JSONPatchTypes represents the valid JSON types for type operations.

const (
	// JSONPatchTypeString represents the string data type for JSON Patch type operations
	JSONPatchTypeString JSONPatchTypes = "string"
	// JSONPatchTypeNumber represents the number data type for JSON Patch type operations
	JSONPatchTypeNumber JSONPatchTypes = "number"
	// JSONPatchTypeBoolean represents the boolean data type for JSON Patch type operations
	JSONPatchTypeBoolean JSONPatchTypes = "boolean"
	// JSONPatchTypeObject represents the object data type for JSON Patch type operations
	JSONPatchTypeObject JSONPatchTypes = "object"
	// JSONPatchTypeInteger represents the integer data type for JSON Patch type operations
	JSONPatchTypeInteger JSONPatchTypes = "integer"
	// JSONPatchTypeArray represents the array data type for JSON Patch type operations
	JSONPatchTypeArray JSONPatchTypes = "array"
	// JSONPatchTypeNull represents the null data type for JSON Patch type operations
	JSONPatchTypeNull JSONPatchTypes = "null"
)

func GetJSONPatchType added in v0.4.3

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

OpType represents the string type for JSON Patch operation names, such as "add", "remove", "replace", etc. Used for type safety and constant references only.

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

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

	// OpAndType represents the "and" operation type for composite operations
	OpAndType OpType = "and"
	// OpOrType represents the "or" operation type for composite operations
	OpOrType OpType = "or"
	// OpNotType represents the "not" operation type for composite operations
	OpNotType OpType = "not"

	// OpFlipType represents the "flip" operation type for extended operations
	OpFlipType OpType = "flip"
	// OpIncType represents the "inc" operation type for extended operations
	OpIncType OpType = "inc"
	// OpStrInsType represents the "str_ins" operation type for extended operations
	OpStrInsType OpType = "str_ins"
	// OpStrDelType represents the "str_del" operation type for extended operations
	OpStrDelType OpType = "str_del"
	// OpSplitType represents the "split" operation type for extended operations
	OpSplitType OpType = "split"
	// OpMergeType represents the "merge" operation type for extended operations
	OpMergeType OpType = "merge"
	// OpExtendType represents the "extend" operation type for extended operations
	OpExtendType OpType = "extend"
)

type Operation

type Operation struct {
	Op    string `json:"op"`
	Path  string `json:"path"`
	Value any    `json:"value,omitempty"`
	From  string `json:"from,omitempty"`

	// Extended operation fields
	Inc float64 `json:"inc"` // No omitempty - 0 is a valid increment
	Pos int     `json:"pos"` // No omitempty - 0 is a valid position
	Str string  `json:"str"`
	Len int     `json:"len"` // No omitempty - 0 is a valid length

	// Predicate fields
	Not        bool        `json:"not,omitempty"`
	Type       any         `json:"type,omitempty"`
	IgnoreCase bool        `json:"ignore_case,omitempty"`
	Apply      []Operation `json:"apply,omitempty"`

	// Special fields
	Props      map[string]any `json:"props,omitempty"`
	DeleteNull bool           `json:"deleteNull,omitempty"`
	OldValue   any            `json:"oldValue,omitempty"`
}

Operation represents a JSON Patch operation object Matches json-joy Operation interface exactly

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 CreateRegexMatcher) 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 CreateRegexMatcher // 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. This aligns with json-joy's RegexMatcher type.

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