jsonpatch

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InvalidPathError

type InvalidPathError struct {
	Path   string
	Index  int
	Char   string
	Reason string
}

func ConvertPath

func ConvertPath(path string) (string, *InvalidPathError)

ConvertPath takes a JSONPath-like path expression (.foo.bar[0].baz, .foo["bar"][0][baz]) and converts it into the format specified by the JSONPatch RFC (/foo/bar/0/baz). Rules: - The path expression may start with a dot (.). - Dots (.), square brackets ([, ]), and single (') or double (") quotes in field names are escaped with a preceding backslash (\). - Backslashes (\) in field names are escaped with a preceding backslash (\). - Field names are separated by either dots (.) or by wrapping them in square brackets ([]). - Dots (.) that appear within square brackets are treated as part of the field name, not as separators (even if not escaped). - Values in square brackets may be wrapped in double (") or single (') quotes, or may be unquoted. - Nesting brackets in brackets is not supported, unless the whole value in the outer brackets is in quotes, then the inner brackets are treated as part of the value. - The JSONPatch path expression does not differentiate between field names and array indices, so neither does this format.

Noop if the path starts with a slash (/), because then it is expected to be in the JSONPatch format already. Returns just a slash (/) if the path is empty. Returns an error in case of an invalid path expression (non-matching brackets or quotes, wrong escaping, etc.).

Note that the JSONPatch's Apply method calls this function automatically, it is usually not necessary to call this function directly.

func NewInvalidPathError

func NewInvalidPathError(path string, index int, char string, reason string, args ...any) *InvalidPathError

func (*InvalidPathError) Error

func (e *InvalidPathError) Error() string

type Option

type Option func(*Options)

func AccumulatedCopySizeLimit

func AccumulatedCopySizeLimit(val int64) Option

AccumulatedCopySizeLimit limits the total size increase in bytes caused by "copy" operations in a patch.

func AllowMissingPathOnRemove

func AllowMissingPathOnRemove(val bool) Option

AllowMissingPathOnRemove indicates whether to fail "remove" operations when the target path is missing. Default to false.

func EnsurePathExistsOnAdd

func EnsurePathExistsOnAdd(val bool) Option

EnsurePathExistsOnAdd instructs json-patch to recursively create the missing parts of path on "add" operation. Defaults to false.

func EscapeHTML

func EscapeHTML(val bool) Option

EscapeHTML sets the EscapeHTML flag for json marshalling. Defaults to true.

func Indent

func Indent(val string) Option

Indent sets the indentation string for the output JSON. If empty, no indentation is applied.

func SupportNegativeIndices

func SupportNegativeIndices(val bool) Option

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.

type Options

type Options struct {
	*jplib.ApplyOptions

	// Indent is the string used for indentation in the output JSON.
	// Empty string means no indentation.
	Indent string
}

type Patch

type Patch = TypedPatch[Untyped]

func New

func New(patches ...jpapi.JSONPatch) *Patch

New creates a new JSONPatch with the given patches. This JSONPatch's Apply method works on plain JSON bytes. To apply the patches to an arbitrary type (which is marshalled to JSON before and unmarshalled back afterwards), use NewTyped instead.

type PatchValueData

type PatchValueData = apiextensionsv1.JSON

type TypedPatch

type TypedPatch[T any] struct {
	jpapi.JSONPatches
}

func NewTyped

func NewTyped[T any](patches ...jpapi.JSONPatch) *TypedPatch[T]

NewTyped creates a new TypedJSONPatch with the given patches.

func (*TypedPatch[T]) Apply

func (p *TypedPatch[T]) Apply(doc T, options ...Option) (T, error)

Apply applies the patch to the given document. If the generic type is Untyped (which is an alias for []byte), it will treat the document as raw JSON bytes. Otherwise, doc is marshalled to JSON before applying the patch and then again unmarshalled back to the original type afterwards.

func (*TypedPatch[T]) MarshalJSON

func (p *TypedPatch[T]) MarshalJSON() ([]byte, error)

MarshalJSON marshals the TypedJSONPatch to JSON. Note that this uses the ConvertPath function to ensure that the paths are in the correct format.

type Untyped

type Untyped = []byte

Jump to

Keyboard shortcuts

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