patch

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: MPL-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package patch implements RFC 6902 (JSON Patch) and RFC 7396 (JSON Merge Patch) for use in HTTP PATCH handlers per RFC 5789.

Index

Constants

View Source
const (
	ContentTypeMergePatch = "application/merge-patch+json" // RFC 7396
	ContentTypeJSONPatch  = "application/json-patch+json"  // RFC 6902
)

AcceptPatch is the value for the Accept-Patch response header (RFC 5789 Section 3.1).

Variables

View Source
var (
	// ErrUnsupportedContentType should map to 415 with an Accept-Patch header.
	ErrUnsupportedContentType = errors.New("unsupported patch content type: use application/merge-patch+json (RFC 7396) or application/json-patch+json (RFC 6902)")

	// ErrInvalidPatch should map to 422 Unprocessable Entity.
	ErrInvalidPatch = errors.New("invalid patch document")
)

Functions

func Apply

func Apply(contentType string, original, patchData []byte) ([]byte, error)

Apply applies a patch to a raw JSON document. Both formats are applied atomically (RFC 5789 Section 2).

func ApplyTo

func ApplyTo[T any](contentType string, target *T, patchData []byte) error

ApplyTo applies a patch directly to a Go struct. On error the target is left unchanged.

func Equal

func Equal(a, b []byte) bool

Equal reports whether two JSON documents are semantically equal.

func MakeOptionalSchema

func MakeOptionalSchema(s *core.Schema) *core.Schema

MakeOptionalSchema deep-copies the schema with all Required fields removed. Useful for merge-patch request body schemas where every field is optional.

Types

type OpType

type OpType string
const (
	OpAdd     OpType = "add"
	OpRemove  OpType = "remove"
	OpReplace OpType = "replace"
	OpMove    OpType = "move" // RFC 6902 only
	OpCopy    OpType = "copy" // RFC 6902 only
	OpTest    OpType = "test" // RFC 6902 only
)

type Operation

type Operation struct {
	Op    OpType
	Path  []string // Target path segments, e.g. ["address", "zip"]
	From  []string // Source path for move/copy (RFC 6902 only)
	Value any      // Value to set/add/test. Nil for remove.
}

Operation is a single parsed patch instruction.

func Parse

func Parse(contentType string, patchData []byte) ([]Operation, error)

Parse parses a patch document into operations without applying them. For RFC 6902: operations map directly from the patch array. For RFC 7396: the patch object is walked recursively, producing OpReplace for non-null leaves and OpRemove for null values.

Jump to

Keyboard shortcuts

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