jsonpatch

package module
v5.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: BSD-3-Clause Imports: 10 Imported by: 229

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// SupportNegativeIndices decides whether to support non-standard practice of
	// allowing negative indices to mean indices starting at the end of an array.
	// Default to true.
	SupportNegativeIndices bool = true
	// AccumulatedCopySizeLimit limits the total size increase in bytes caused by
	// "copy" operations in a patch.
	AccumulatedCopySizeLimit int64 = 0
)
View Source
var (
	ErrTestFailed   = errors.New("test failed")
	ErrMissing      = errors.New("missing value")
	ErrUnknownType  = errors.New("unknown object type")
	ErrInvalid      = errors.New("invalid state detected")
	ErrInvalidIndex = errors.New("invalid index referenced")

	ErrExpectedObject = errors.New("invalid value, expected object")
)

Functions

func CreateMergePatch

func CreateMergePatch(originalJSON, modifiedJSON []byte) ([]byte, error)

CreateMergePatch will return a merge patch document capable of converting the original document(s) to the modified document(s). The parameters can be bytes of either two JSON Documents, or two arrays of JSON documents. The merge patch returned follows the specification defined at http://tools.ietf.org/html/draft-ietf-appsawg-json-merge-patch-07

func Equal

func Equal(a, b []byte) bool

Equal indicates if 2 JSON documents have the same structural equality.

func MergeMergePatches

func MergeMergePatches(patch1Data, patch2Data []byte) ([]byte, error)

MergeMergePatches merges two merge patches together, such that applying this resulting merged merge patch to a document yields the same as merging each merge patch to the document in succession.

func MergePatch

func MergePatch(docData, patchData []byte) ([]byte, error)

MergePatch merges the patchData into the docData.

Types

type AccumulatedCopySizeError

type AccumulatedCopySizeError struct {
	// contains filtered or unexported fields
}

AccumulatedCopySizeError is an error type returned when the accumulated size increase caused by copy operations in a patch operation has exceeded the limit.

func NewAccumulatedCopySizeError

func NewAccumulatedCopySizeError(l, a int64) *AccumulatedCopySizeError

NewAccumulatedCopySizeError returns an AccumulatedCopySizeError.

func (*AccumulatedCopySizeError) Error

func (a *AccumulatedCopySizeError) Error() string

Error implements the error interface.

type ApplyOptions added in v5.2.0

type ApplyOptions struct {
	// SupportNegativeIndices decides whether to support non-standard practice of
	// allowing negative indices to mean indices starting at the end of an array.
	// Default to true.
	SupportNegativeIndices bool
	// AccumulatedCopySizeLimit limits the total size increase in bytes caused by
	// "copy" operations in a patch.
	AccumulatedCopySizeLimit int64
	// AllowMissingPathOnRemove indicates whether to fail "remove" operations when the target path is missing.
	// Default to false.
	AllowMissingPathOnRemove bool
	// EnsurePathExistsOnAdd instructs json-patch to recursively create the missing parts of path on "add" operation.
	// Default to false.
	EnsurePathExistsOnAdd bool

	EscapeHTML bool
}

ApplyOptions specifies options for calls to ApplyWithOptions. Use NewApplyOptions to obtain default values for ApplyOptions.

func NewApplyOptions added in v5.2.0

func NewApplyOptions() *ApplyOptions

NewApplyOptions creates a default set of options for calls to ApplyWithOptions.

type ArraySizeError

type ArraySizeError struct {
	// contains filtered or unexported fields
}

ArraySizeError is an error type returned when the array size has exceeded the limit.

func NewArraySizeError

func NewArraySizeError(l, s int) *ArraySizeError

NewArraySizeError returns an ArraySizeError.

func (*ArraySizeError) Error

func (a *ArraySizeError) Error() string

Error implements the error interface.

type Operation

type Operation map[string]*json.RawMessage

Operation is a single JSON-Patch step, such as a single 'add' operation.

func (Operation) From

func (o Operation) From() (string, error)

From reads the "from" field of the Operation.

func (Operation) Kind

func (o Operation) Kind() string

Kind reads the "op" field of the Operation.

func (Operation) Path

func (o Operation) Path() (string, error)

Path reads the "path" field of the Operation.

func (Operation) ValueInterface

func (o Operation) ValueInterface() (interface{}, error)

ValueInterface decodes the operation value into an interface.

type Patch

type Patch []Operation

Patch is an ordered collection of Operations.

func DecodePatch

func DecodePatch(buf []byte) (Patch, error)

DecodePatch decodes the passed JSON document as an RFC 6902 patch.

func (Patch) Apply

func (p Patch) Apply(doc []byte) ([]byte, error)

Apply mutates a JSON document according to the patch, and returns the new document.

func (Patch) ApplyIndent

func (p Patch) ApplyIndent(doc []byte, indent string) ([]byte, error)

ApplyIndent mutates a JSON document according to the patch, and returns the new document indented.

func (Patch) ApplyIndentWithOptions added in v5.2.0

func (p Patch) ApplyIndentWithOptions(doc []byte, indent string, options *ApplyOptions) ([]byte, error)

ApplyIndentWithOptions mutates a JSON document according to the patch and the passed in ApplyOptions. It returns the new document indented.

func (Patch) ApplyWithOptions added in v5.2.0

func (p Patch) ApplyWithOptions(doc []byte, options *ApplyOptions) ([]byte, error)

ApplyWithOptions mutates a JSON document according to the patch and the passed in ApplyOptions. It returns the new document.

Directories

Path Synopsis
cmd
internal
json
Package json implements encoding and decoding of JSON as defined in RFC 7159.
Package json implements encoding and decoding of JSON as defined in RFC 7159.

Jump to

Keyboard shortcuts

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