internal

package
v0.7.7 Latest Latest
Warning

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

Go to latest
Published: May 9, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package internal provides shared types, interfaces, and constants for JSON Patch operations.

Index

Constants

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

	// String editing.
	OpStrInsCode = 6
	OpStrDelCode = 7

	// Extra operations.
	OpFlipCode = 8
	OpIncCode  = 9

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

	// JSON Predicate operations.
	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 codes for binary serialization.

Variables

This section is empty.

Functions

func IsFirstOrderPredicateOperation

func IsFirstOrderPredicateOperation(op string) bool

IsFirstOrderPredicateOperation reports whether op is a first-order predicate.

func IsJSONPatchExtendedOperation added in v0.4.3

func IsJSONPatchExtendedOperation(op string) bool

IsJSONPatchExtendedOperation reports whether op is an extended operation.

func IsJSONPatchOperation added in v0.4.3

func IsJSONPatchOperation(op string) bool

IsJSONPatchOperation reports whether op is a core JSON Patch (RFC 6902) operation.

func IsPredicateOperation

func IsPredicateOperation(op string) bool

IsPredicateOperation reports whether op is any predicate operation.

func IsSecondOrderPredicateOperation

func IsSecondOrderPredicateOperation(op string) bool

IsSecondOrderPredicateOperation reports whether op is a second-order (composite) predicate.

func IsValidJSONPatchType added in v0.4.3

func IsValidJSONPatchType(typeStr string) bool

IsValidJSONPatchType reports whether typeStr is a valid JSON Patch type name.

Types

type Codec

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

Codec is the interface for encoding and decoding JSON Patch operations.

type CompactOperation

type CompactOperation = []any

CompactOperation is the array representation used by the compact codec.

type CreateRegexMatcher

type CreateRegexMatcher func(pattern string, ignoreCase bool) RegexMatcher

CreateRegexMatcher builds a RegexMatcher from pattern and ignoreCase.

type Document added in v0.2.0

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

Document describes the input and output shapes supported by the generic API.

type JSONPatchOptions added in v0.4.3

type JSONPatchOptions struct {
	// CreateMatcher overrides regex compilation for pattern operations.
	CreateMatcher CreateRegexMatcher
}

JSONPatchOptions configures JSON Patch decoding.

type JSONPatchType added in v0.5.14

type JSONPatchType string

JSONPatchType represents valid JSON types for type-checking operations.

const (
	JSONPatchTypeString  JSONPatchType = "string"
	JSONPatchTypeNumber  JSONPatchType = "number"
	JSONPatchTypeBoolean JSONPatchType = "boolean"
	JSONPatchTypeObject  JSONPatchType = "object"
	JSONPatchTypeInteger JSONPatchType = "integer"
	JSONPatchTypeArray   JSONPatchType = "array"
	JSONPatchTypeNull    JSONPatchType = "null"
)

Valid JSON Patch types.

func GetJSONPatchType added in v0.4.3

func GetJSONPatchType(value any) JSONPatchType

GetJSONPatchType returns the JSON Patch type for a Go value. Whole-number floats return "integer"; unknown types return "null".

type Op

type Op interface {
	// Op returns the operation type string (e.g. "add", "remove").
	Op() OpType
	// Code returns the numeric code for the operation.
	Code() int
	// Path returns the JSON Pointer path as a string slice.
	Path() []string
	// Apply applies the operation to the document, returning the result and any error.
	Apply(doc any) (OpResult[any], error)
	// ToJSON serializes the operation to standard JSON Patch format.
	ToJSON() (Operation, error)
	// ToCompact serializes the operation to compact array format.
	ToCompact() (CompactOperation, error)
	// Validate checks that the operation parameters are valid.
	Validate() error
}

Op is the unified interface for all JSON Patch operations.

type OpResult

type OpResult[T Document] struct {
	// Doc is the document after the operation completes.
	Doc T `json:"doc"`
	// Old is the previous value returned by operations that expose one.
	Old any `json:"old,omitempty"`
}

OpResult holds the result of applying one operation.

type OpType

type OpType string

OpType represents a JSON Patch operation name such as "add" or "remove".

const (
	OpAddType     OpType = "add"
	OpRemoveType  OpType = "remove"
	OpReplaceType OpType = "replace"
	OpMoveType    OpType = "move"
	OpCopyType    OpType = "copy"
	OpTestType    OpType = "test"
)

JSON Patch (RFC 6902) operation types.

const (
	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"
)

JSON Predicate operation types.

const (
	OpAndType OpType = "and"
	OpOrType  OpType = "or"
	OpNotType OpType = "not"
)

Composite (second-order) predicate operation types.

const (
	OpFlipType   OpType = "flip"
	OpIncType    OpType = "inc"
	OpStrInsType OpType = "str_ins"
	OpStrDelType OpType = "str_del"
	OpSplitType  OpType = "split"
	OpMergeType  OpType = "merge"
	OpExtendType OpType = "extend"
)

Extended operation types.

type Operation

type Operation struct {
	// Op names the operation to apply.
	Op string `json:"op"`
	// Path is the target JSON Pointer.
	Path string `json:"path"`
	// Value holds the operation payload when the operation uses one.
	Value any `json:"value,omitempty"`
	// From is the source JSON Pointer for move and copy.
	From string `json:"from,omitempty"`

	// Inc is the numeric delta for inc. It is never omitted because 0 is valid.
	Inc float64 `json:"inc"`
	// Pos is the string or array position for operations that use one. It is never omitted because 0 is valid.
	Pos int `json:"pos"`
	// Str is the string operand for string-editing operations. It is never omitted because the empty string is valid.
	Str string `json:"str"`
	// Len is the requested length for operations that use one. It is never omitted because 0 is valid.
	Len int `json:"len"`

	// Not inverts supported predicate operations.
	Not bool `json:"not,omitempty"`
	// Type holds one JSON type name or a list of JSON type names.
	Type any `json:"type,omitempty"`
	// IgnoreCase makes string matching case-insensitive when supported.
	IgnoreCase bool `json:"ignore_case,omitempty"`
	// Apply contains nested predicate operations for and, or, and not.
	Apply []Operation `json:"apply,omitempty"`

	// Props supplies object properties for extend.
	Props map[string]any `json:"props,omitempty"`
	// DeleteNull removes properties whose incoming value is null during extend.
	DeleteNull bool `json:"deleteNull,omitempty"`
	// OldValue carries the expected previous value for operations that use it.
	OldValue any `json:"oldValue,omitempty"`
}

Operation describes one JSON Patch, predicate, or extended operation.

type Option added in v0.2.0

type Option func(*Options)

Option applies one patch option.

func WithMatcher added in v0.2.0

func WithMatcher(createMatcher CreateRegexMatcher) Option

WithMatcher sets the regex matcher factory used by pattern operations.

func WithMutate added in v0.2.0

func WithMutate(mutate bool) Option

WithMutate enables or disables in-place mutation.

type Options added in v0.2.0

type Options struct {
	// Mutate enables in-place updates instead of cloning the input first.
	Mutate bool
	// CreateMatcher overrides regex compilation for pattern operations.
	CreateMatcher CreateRegexMatcher
}

Options configures patch application.

type PatchResult

type PatchResult[T Document] struct {
	// Doc is the final patched document.
	Doc T
	// Res contains the per-operation results in application order.
	Res []OpResult[T]
}

PatchResult holds the result of applying a sequence of operations.

type PredicateOp

type PredicateOp interface {
	Op
	// Test evaluates the predicate against the document.
	Test(doc any) (bool, error)
	// Not reports whether the predicate is negated.
	Not() bool
}

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

type RegexMatcher

type RegexMatcher func(value string) bool

RegexMatcher reports whether value matches a compiled pattern.

type SecondOrderPredicateOp

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

SecondOrderPredicateOp is the interface for composite predicates that combine multiple sub-predicates (and, or, not).

Jump to

Keyboard shortcuts

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