jsonpatch

package module
v0.0.0-...-3217193 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package jsonpatch provides a json patch library.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrStop is a stop error. Any extension can return this error to stop the patch.
	// For example, the "test" extension can return this error to stop the patch.
	ErrStop = errors.New("stop")
	// ErrNotExists is a not exists error.
	// If StrictPathExists is false, the patch will continue if extension return this error.
	ErrNotExists = errors.New("path member not exists")
)

Functions

This section is empty.

Types

type Descriptor

type Descriptor interface {
	// Description return a description of the operation
	Description(p *Patch, op Operation) string
}

Descriptor is a jsonpatch extension that return a description of the operation

type Extension

type Extension interface {
	// OP returns the operation name
	// e.g. "add", "remove", "replace", "move", "copy", "test"
	OP() string
	// Apply apply the operation
	// op is check before apply.
	Apply(p *Patch, o *any, op Operation) error
	// Check check the operation, and return error if the operation is invalid.
	Check(p *Patch, op Operation) error
}

Extension is a jsonpatch extension that apply an operation.

type JSONPointer

type JSONPointer struct {
	// contains filtered or unexported fields
}

JSONPointer is a json pointer introduce in RFC 6901.

func NewJSONPointer

func NewJSONPointer(p string) JSONPointer

NewJSONPointer create a new JSONPointer.

func (JSONPointer) Check

func (p JSONPointer) Check() error

Check check the JSONPointer, and return error if the JSONPointer is invalid.

func (JSONPointer) IsTheWholeDocument

func (p JSONPointer) IsTheWholeDocument() bool

IsTheWholeDocument return true if the JSONPointer points to the whole document.

func (JSONPointer) LastToken

func (p JSONPointer) LastToken() string

LastToken return the last token of the JSONPointer.

func (JSONPointer) ParentPath

func (p JSONPointer) ParentPath() []string

ParentPath return the parent path of the JSONPointer.

func (JSONPointer) Path

func (p JSONPointer) Path() []string

Path return the every parts of the JSONPointer.

func (JSONPointer) SameParent

func (p JSONPointer) SameParent(o JSONPointer) bool

SameParent return true if the parent path of the JSONPointer is the same as the parent path of the other JSONPointer.

type Operation

type Operation struct {
	OP   *string `json:"op"`
	Path *string `json:"path"`
	// Value is the value of the operation.
	// Unlike json Unmarshal, if the value is null, it will not be set to nil, but a pointer to nil.
	Value *any    `json:"value,omitempty"`
	From  *string `json:"from,omitempty"`
}

Operation is a jsonpatch operation introduced in RFC6902.

func (*Operation) UnmarshalJSON

func (o *Operation) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

type Option

type Option func(o *Patch)

Option is a jsonpatch option.

func WithExtension

func WithExtension(ext Extension) Option

WithExtension add a new extension.

func WithJSONEscapeHTML

func WithJSONEscapeHTML(on bool) Option

WithJSONEscapeHTML set the JSONEscapeHTML option.

func WithJSONIndent

func WithJSONIndent(prefix, indent string) Option

WithJSONIndent set the JSONIndent option.

func WithStrictPathExists

func WithStrictPathExists(on bool) Option

WithStrictPathExists set the StrictPathExists option. The default value is false. If StrictPathExists is true, an error will be thrown if the path does not exist.

func WithSupportNegativeArrayIndex

func WithSupportNegativeArrayIndex(on bool) Option

WithSupportNegativeArrayIndex set the SupportNegativeArrayIndex option. The default value is false. If SupportNegativeArrayIndex is true, negative array index is supported.

type Patch

type Patch struct {
	// StrictPathExists is a flag that indicates whether to throw an error if the path does not exist.
	StrictPathExists bool
	// SupportNegativeArrayIndex is a flag that indicates whether to support negative array index.
	SupportNegativeArrayIndex bool

	// Standard json marshaling options.
	JSONPrefix     string
	JSONIndent     string
	JSONEscapeHTML bool
	// contains filtered or unexported fields
}

Patch is a jsonpatch introduced in RFC6902.

func New

func New(options ...Option) *Patch

New create a new jsonpatch. It exactly matches the RFC6902 spec if no option is set.

func (*Patch) AddValue

func (p *Patch) AddValue(o any, set Setter, key string, value any) (err error)

AddValue add a value to a node.

func (*Patch) Apply

func (p *Patch) Apply(b []byte, ops []Operation) ([]byte, error)

Apply apply the operations.

func (*Patch) ApplyAny

func (p *Patch) ApplyAny(o *any, ops []Operation) error

ApplyAny apply the operations.

func (*Patch) Check

func (p *Patch) Check(ops []Operation) error

Check check the operations.

func (*Patch) MoveValue

func (p *Patch) MoveValue(o any, set Setter, from, to string) (err error)

MoveValue move a value from a node.

func (*Patch) ParseArrayIndex

func (p *Patch) ParseArrayIndex(size int, s string) (i int, err error)

ParseArrayIndex parse the array index.

func (*Patch) RemoveValue

func (p *Patch) RemoveValue(o any, set Setter, key string) (err error)

RemoveValue remove a value from a node.

func (*Patch) ReplaceValue

func (p *Patch) ReplaceValue(o any, _ Setter, key string, value any) (err error)

ReplaceValue replace a value to a node.

func (*Patch) VisitPath

func (p *Patch) VisitPath(o *any, parts ...string) (any, Setter, error)

VisitPath visit the path list.

type Setter

type Setter func(n any)

Setter is a function that sets the value of a node.

Jump to

Keyboard shortcuts

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