patch

package
v3.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(original, patch []byte) ([]byte, error)

func CreateJSONPatchFromMergePatch added in v3.8.0

func CreateJSONPatchFromMergePatch(mergePatch, modified, current []byte) ([]byte, bool, error)

CreateJSONPatchFromMergePatch translates an RFC 7386 JSON merge patch into an equivalent RFC 6902 JSON Patch.

A null in a merge patch is ambiguous. RFC 7386 defines it as an instruction to remove a key, so a caller cannot use one to set a value to null -- which makes an explicit null inexpressible against any resource patched with types.MergePatchType, and that is every CRD. RFC 6902 draws the distinction the merge format cannot: "remove" and "add" with a null value are different operations.

The ambiguity is resolved against modified, the desired object the merge patch was generated from. A null at a path that is explicitly null in modified is an assignment; a null at a path absent from modified is a removal.

current, the live object, is used to drop removals of paths that do not exist, because RFC 6902 requires "remove" to target an existing path and fails the entire patch otherwise.

Everything the merge patch would have done is preserved. Arrays are treated as opaque leaves and replaced wholesale, matching merge patch semantics and keeping positional index operations -- and the races that come with them -- out of the result.

The second return value reports whether the patch assigns a null anywhere. It is false for the overwhelmingly common case of a patch that only adds, changes and removes keys, where the translation buys nothing and the caller should prefer the merge patch it already has: RFC 6902 "remove" fails the whole request if the path has since been deleted by another writer, whereas a merge patch treats that as a no-op. Converting only when a null actually has to be expressed keeps that stricter failure mode off the common path.

func EscapePointerToken added in v3.8.0

func EscapePointerToken(token string) string

EscapePointerToken escapes a single JSON Pointer reference token per RFC 6901.

func GetPatchStyle

func GetPatchStyle(original, patch []byte) (types.PatchType, strategicpatch.LookupPatchMeta, error)

func IsJSONPatch added in v3.8.0

func IsJSONPatch(patch []byte) bool

IsJSONPatch reports whether the given bytes are an RFC 6902 JSON Patch, which is a list, as opposed to a merge patch, which is an object.

Types

type Operation added in v3.8.0

type Operation struct {
	Op    string          `json:"op"`
	Path  string          `json:"path"`
	Value json.RawMessage `json:"value,omitempty"`
}

Operation is a single RFC 6902 JSON Patch operation.

Value is a json.RawMessage rather than a *json.RawMessage so that the type survives a decode/encode round trip. A literal null must stay distinguishable from an absent value -- "remove" carries no value member, while assigning null requires one -- and a pointer cannot do that: encoding/json sets a pointer field to nil for a JSON null, which omitempty then drops, silently turning a null assignment into an "add" with no value, an operation RFC 6902 rejects. A RawMessage instead holds the four bytes "null", which omitempty keeps.

Jump to

Keyboard shortcuts

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