jsonpatch

package
v0.1.0-alpha.51 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2025 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyPatch

func ApplyPatch(original any, patches []Patch) (map[string]any, error)

ApplyPatch applies a series of JSON Patch operations to the original JSON-like object (struct or map). It returns the patched document as a map[string]any. The implementation applies operations sequentially and returns an error on the first failing operation.

func ApplyPatchAndHydrate

func ApplyPatchAndHydrate(original, updated any, patches []Patch) error

ApplyPatchAndHydrate applies patches to `original` and unmarshals the resulting document into `updated` (which should be a pointer). This is a convenience for applying patches and then hydrating a typed value.

Types

type Patch

type Patch struct {
	Op    string `json:"op"`
	Path  string `json:"path"`
	From  string `json:"from,omitempty"`
	Value any    `json:"value,omitempty"`
}

Patch represents a single JSON Patch operation as defined by RFC 6902. The Op field is the operation (add, remove, replace, move). Path is the JSON Pointer location. From is used by move operations and Value holds the operation payload when applicable.

func GeneratePatch

func GeneratePatch(before, after any, basePath string) ([]Patch, error)

GeneratePatch computes a list of JSON Patch operations that transform the `before` document into the `after` document. Both inputs may be structs or maps; basePath should be the JSON Pointer prefix (e.g. "" or "/root").

The function attempts to produce minimal patches for arrays using an LCS-based algorithm and treats string equality with trimmed whitespace.

Jump to

Keyboard shortcuts

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