Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type InvalidPathError ¶
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 (*InvalidPathError) Error ¶
func (e *InvalidPathError) Error() string
type Option ¶
type Option func(*Options)
func AccumulatedCopySizeLimit ¶
AccumulatedCopySizeLimit limits the total size increase in bytes caused by "copy" operations in a patch.
func AllowMissingPathOnRemove ¶
AllowMissingPathOnRemove indicates whether to fail "remove" operations when the target path is missing. Default to false.
func EnsurePathExistsOnAdd ¶
EnsurePathExistsOnAdd instructs json-patch to recursively create the missing parts of path on "add" operation. Defaults to false.
func EscapeHTML ¶
EscapeHTML sets the EscapeHTML flag for json marshalling. Defaults to true.
func Indent ¶
Indent sets the indentation string for the output JSON. If empty, no indentation is applied.
func SupportNegativeIndices ¶
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]
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.