Documentation
¶
Overview ¶
Package jsonpatch provides a json patch library.
Index ¶
- Variables
- type Descriptor
- type Extension
- type JSONPointer
- type Operation
- type Option
- type Patch
- func (p *Patch) AddValue(o any, set Setter, key string, value any) (err error)
- func (p *Patch) Apply(b []byte, ops []Operation) ([]byte, error)
- func (p *Patch) ApplyAny(o *any, ops []Operation) error
- func (p *Patch) Check(ops []Operation) error
- func (p *Patch) MoveValue(o any, set Setter, from, to string) (err error)
- func (p *Patch) ParseArrayIndex(size int, s string) (i int, err error)
- func (p *Patch) RemoveValue(o any, set Setter, key string) (err error)
- func (p *Patch) ReplaceValue(o any, _ Setter, key string, value any) (err error)
- func (p *Patch) VisitPath(o *any, parts ...string) (any, Setter, error)
- type Setter
Constants ¶
This section is empty.
Variables ¶
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 ¶
UnmarshalJSON implements json.Unmarshaler.
type Option ¶
type Option func(o *Patch)
Option is a jsonpatch option.
func WithJSONEscapeHTML ¶
WithJSONEscapeHTML set the JSONEscapeHTML option.
func WithJSONIndent ¶
WithJSONIndent set the JSONIndent option.
func WithStrictPathExists ¶
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 ¶
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 (*Patch) ParseArrayIndex ¶
ParseArrayIndex parse the array index.
func (*Patch) RemoveValue ¶
RemoveValue remove a value from a node.
func (*Patch) ReplaceValue ¶
ReplaceValue replace a value to a node.