Documentation
¶
Overview ¶
Package jsonpatch compiles and applies JSON Patch, predicate, and extended operations to Go values while preserving the caller's document type.
Example ¶
package main
import (
"fmt"
jsoncodec "github.com/kaptinlin/jsonpatch/codec/json"
"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
"github.com/kaptinlin/jsonpatch"
)
func main() {
// Original document
doc := map[string]any{
"user": map[string]any{
"name": "Alice",
"email": "alice@example.com",
"age": 25,
},
"settings": map[string]any{
"theme": "dark",
},
}
// Create patch operations
patch := []jsoncodec.Operation{
// Add a new field
{
Op: "add",
Path: "/user/active",
Value: true,
},
// Update existing field
{
Op: "replace",
Path: "/user/age",
Value: 26,
},
// Add to settings
{
Op: "add",
Path: "/settings/notifications",
Value: true,
},
}
compiled, err := jsonpatch.CompileOperations(patch)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
result, err := jsonpatch.Apply(compiled, doc)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
// Print result
resultJSON, _ := json.Marshal(result.Doc, jsontext.Multiline(true), json.Deterministic(true))
fmt.Println(string(resultJSON))
}
Output: { "settings": { "notifications": true, "theme": "dark" }, "user": { "active": true, "age": 26, "email": "alice@example.com", "name": "Alice" } }
Index ¶
Examples ¶
Constants ¶
const ( // JSON Patch (RFC 6902) operations OpAddType = internal.OpAddType OpRemoveType = internal.OpRemoveType OpReplaceType = internal.OpReplaceType OpMoveType = internal.OpMoveType OpCopyType = internal.OpCopyType OpTestType = internal.OpTestType // JSON Predicate operations OpContainsType = internal.OpContainsType OpDefinedType = internal.OpDefinedType OpUndefinedType = internal.OpUndefinedType OpTypeType = internal.OpTypeType OpTestTypeType = internal.OpTestTypeType OpTestStringType = internal.OpTestStringType OpTestStringLenType = internal.OpTestStringLenType OpEndsType = internal.OpEndsType OpStartsType = internal.OpStartsType OpInType = internal.OpInType OpLessType = internal.OpLessType OpMoreType = internal.OpMoreType OpMatchesType = internal.OpMatchesType // Composite operations OpAndType = internal.OpAndType OpOrType = internal.OpOrType OpNotType = internal.OpNotType // Extended operations OpFlipType = internal.OpFlipType OpIncType = internal.OpIncType OpStrInsType = internal.OpStrInsType OpStrDelType = internal.OpStrDelType OpSplitType = internal.OpSplitType OpMergeType = internal.OpMergeType OpExtendType = internal.OpExtendType )
These constants name the supported operations.
const AllCapabilities = RFC6902 | Predicate | RegexPredicate | Extended
AllCapabilities enables every operation vocabulary implemented by the package.
Variables ¶
var ( // ErrPayloadInvalid reports an invalid patch payload or compiled operation. ErrPayloadInvalid = errors.New("payload invalid") // ErrUnsupportedCapability reports an operation outside the enabled vocabulary. ErrUnsupportedCapability = errors.New("unsupported capability") // ErrRuntimeConflict reports a valid operation that cannot apply to the document state. ErrRuntimeConflict = errors.New("runtime conflict") // ErrTestFailed reports a failed predicate or test operation. ErrTestFailed = errors.New("test failed") // ErrTypeMismatch reports a runtime type mismatch. ErrTypeMismatch = errors.New("type mismatch") // ErrConversionFailed reports that a patched result could not be converted back. ErrConversionFailed = errors.New("failed to convert result back to original type") )
Functions ¶
Types ¶
type Capability ¶ added in v0.7.8
type Capability uint64
Capability identifies an operation vocabulary that may be compiled.
const ( // RFC6902 enables the core JSON Patch operations. RFC6902 Capability = 1 << iota // Predicate enables non-regex predicate operations. Predicate // RegexPredicate enables the matches predicate operation. RegexPredicate // Extended enables JSON Patch Extended operations. Extended )
type CompileOption ¶ added in v0.7.8
type CompileOption func(*compileOptions)
CompileOption configures patch compilation.
func WithCapabilities ¶ added in v0.7.8
func WithCapabilities(capabilities ...Capability) CompileOption
WithCapabilities sets the operation vocabularies accepted during compilation.
func WithCompileMatcher ¶ added in v0.7.8
func WithCompileMatcher(createMatcher CreateRegexMatcher) CompileOption
WithCompileMatcher sets the regex matcher factory used while decoding matches operations.
type CreateRegexMatcher ¶
type CreateRegexMatcher = internal.CreateRegexMatcher
CreateRegexMatcher creates a RegexMatcher from a pattern.
type Error ¶ added in v0.7.8
type Error struct {
// contains filtered or unexported fields
}
Error carries stable patch failure context for programmatic inspection.
func (*Error) Index ¶ added in v0.7.8
Index returns the failing operation index, or -1 when no operation was decoded.
type JSONText ¶ added in v0.7.8
type JSONText string
JSONText marks a string as JSON text instead of a scalar string document.
type Patch ¶ added in v0.7.8
type Patch struct {
// contains filtered or unexported fields
}
Patch is a compiled, reusable operation sequence.
func Compile ¶ added in v0.7.8
Compile compiles Go-built operations with the default RFC 6902 capability.
func CompileJSON ¶ added in v0.7.8
func CompileJSON(data []byte, opts ...CompileOption) (*Patch, error)
CompileJSON compiles a JSON patch document.
func CompileOperations ¶ added in v0.7.8
func CompileOperations(operations []jsoncodec.Operation, opts ...CompileOption) (*Patch, error)
CompileOperations compiles JSON-shaped Operation values.
func CompileOps ¶ added in v0.7.8
func CompileOps(ops []Op, opts ...CompileOption) (*Patch, error)
CompileOps compiles Go-built operations.
type RegexMatcher ¶
type RegexMatcher = internal.RegexMatcher
RegexMatcher tests if a value matches a pattern.
func CreateMatcherDefault ¶
func CreateMatcherDefault(pattern string, ignoreCase bool) (RegexMatcher, error)
CreateMatcherDefault creates a regex matcher from a pattern and case sensitivity flag. It returns a regexp syntax error when pattern is invalid.
type Step ¶ added in v0.7.8
type Step struct {
// contains filtered or unexported fields
}
Step describes one applied operation.
Directories
¶
| Path | Synopsis |
|---|---|
|
codec
|
|
|
binary
Package binary implements a MessagePack-based binary codec for JSON Patch operations.
|
Package binary implements a MessagePack-based binary codec for JSON Patch operations. |
|
compact
Package compact implements an array-based codec for JSON Patch operations.
|
Package compact implements an array-based codec for JSON Patch operations. |
|
json
Package json implements a codec for JSON Patch operations with full RFC 6902, JSON Predicate, and extended operation support.
|
Package json implements a codec for JSON Patch operations with full RFC 6902, JSON Predicate, and extended operation support. |
|
json/tests
Package tests provides test data and automated codec tests for the JSON codec.
|
Package tests provides test data and automated codec tests for the JSON codec. |
|
examples
|
|
|
apply-in-place
command
Package main demonstrates explicit in-place JSON Patch application.
|
Package main demonstrates explicit in-place JSON Patch application. |
|
array-operations
command
Package main demonstrates array operations using JSON Patch.
|
Package main demonstrates array operations using JSON Patch. |
|
basic
command
Package main demonstrates basic JSON Patch operations.
|
Package main demonstrates basic JSON Patch operations. |
|
basic-operations
command
Package main demonstrates basic JSON Patch operations.
|
Package main demonstrates basic JSON Patch operations. |
|
batch-update
command
Package main demonstrates batch update operations using JSON Patch.
|
Package main demonstrates batch update operations using JSON Patch. |
|
binary-codec
command
Package main demonstrates binary codec operations using JSON Patch.
|
Package main demonstrates binary codec operations using JSON Patch. |
|
compact-codec
command
Package main demonstrates the compact codec functionality.
|
Package main demonstrates the compact codec functionality. |
|
conditional-operations
command
Package main demonstrates conditional operations using JSON Patch.
|
Package main demonstrates conditional operations using JSON Patch. |
|
copy-move-operations
command
Package main demonstrates copy and move operations using JSON Patch.
|
Package main demonstrates copy and move operations using JSON Patch. |
|
error-handling
command
Package main demonstrates error handling with JSON Patch operations.
|
Package main demonstrates error handling with JSON Patch operations. |
|
errors
command
Package main demonstrates error handling in JSON Patch operations.
|
Package main demonstrates error handling in JSON Patch operations. |
|
extended
command
Package main demonstrates extended JSON Patch operations.
|
Package main demonstrates extended JSON Patch operations. |
|
json-bytes-patch
command
Package main demonstrates JSON bytes patching operations.
|
Package main demonstrates JSON bytes patching operations. |
|
json-string-patch
command
Package main demonstrates JSON string patching operations.
|
Package main demonstrates JSON string patching operations. |
|
map-patch
command
Package main demonstrates map patching operations using JSON Patch.
|
Package main demonstrates map patching operations using JSON Patch. |
|
string-operations
command
Package main demonstrates string operations using JSON Patch.
|
Package main demonstrates string operations using JSON Patch. |
|
struct-patch
command
Package main demonstrates struct patching operations using JSON Patch.
|
Package main demonstrates struct patching operations using JSON Patch. |
|
types
command
Package main demonstrates type operations with JSON Patch.
|
Package main demonstrates type operations with JSON Patch. |
|
Package internal provides shared types, interfaces, and constants for JSON Patch operations.
|
Package internal provides shared types, interfaces, and constants for JSON Patch operations. |
|
Package op provides implementations for JSON Patch operations.
|
Package op provides implementations for JSON Patch operations. |
|
tests
|
|
|
data
Package data contains test case data and specifications for JSON Patch operations.
|
Package data contains test case data and specifications for JSON Patch operations. |
|
testutils
Package testutils provides shared utilities for JSON Patch testing.
|
Package testutils provides shared utilities for JSON Patch testing. |