internal

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 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 represents a compact-format operation as an array.

type CreateRegexMatcher

type CreateRegexMatcher func(pattern string, ignoreCase bool) RegexMatcher

CreateRegexMatcher creates a RegexMatcher from a pattern and case-sensitivity flag.

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.

type JSONPatchOptions added in v0.4.3

type JSONPatchOptions struct {
	CreateMatcher CreateRegexMatcher
}

JSONPatchOptions contains options for 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 T   `json:"doc"`
	Old any `json:"old,omitempty"`
}

OpResult holds the result of a single operation application.

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    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"` // No omitempty — empty string is a valid value.
	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.

type Option added in v0.2.0

type Option func(*Options)

Option is a functional option for configuring patch operations.

func WithMatcher added in v0.2.0

func WithMatcher(createMatcher CreateRegexMatcher) Option

WithMatcher sets a custom regex matcher factory for pattern operations.

func WithMutate added in v0.2.0

func WithMutate(mutate bool) Option

WithMutate sets whether the patch should modify the original document.

type Options added in v0.2.0

type Options struct {
	Mutate        bool               // Whether to modify the original document.
	CreateMatcher CreateRegexMatcher // Optional regex matcher factory.
}

Options holds configuration for patch operations.

type PatchResult

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

PatchResult holds the result of applying an entire JSON Patch.

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 tests whether a string value matches a 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