jsonpatch2

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2020 License: Apache-2.0 Imports: 6 Imported by: 27

README

jsonpatch is a simple little Go library for creating and applying RFC 6902 JSON patches.

Semi-unique among its features is the ability to generate paranoid patches that include tests that validate that the segments of JSON being patched have not changed in the time the patch was generated to the time it was applied.

Documentation

Index

Constants

View Source
const ContentType = "application/json-patch+json"

Variables

This section is empty.

Functions

This section is empty.

Types

type Operation

type Operation struct {
	// Op can be one of:
	//    * "add"
	//    * "remove"
	//    * "replace"
	//    * "move"
	//    * "copy"
	//    * "test"
	// All Operations must have an Op.
	Op string `json:"op"`
	// Path is a JSON Pointer as defined in RFC 6901
	// All Operations must have a Path
	Path string `json:"path"`
	// From is a JSON pointer indicating where a value should be
	// copied/moved from.  From is only used by copy and move operations.
	From string `json:"from"`
	// Value is the Value to be used for add, replace, and test operations.
	Value interface{} `json:"value"`
	// contains filtered or unexported fields
}

operation represents a valid JSON Patch operation as defined by RFC 6902

func (*Operation) UnmarshalJSON

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

type Patch

type Patch []Operation

Patch is an array of individual JSON Patch operations.

func Generate

func Generate(base, target []byte, paranoid bool) (Patch, error)

Generate generates a JSON Patch that will modify base into target. If paranoid is true, then the generated patch with have test checks for changed item.

base and target must be byte arrays containing valid JSON

func GenerateFull

func GenerateFull(base, target []byte, paranoid, pretest bool) (Patch, error)

Generate generates a JSON Patch that will modify base into target. If paranoid is true, then the generated patch with have test checks for changed item. If pretest is true, then the generated patch with have test ALL parts of the base.

base and target must be byte arrays containing valid JSON

func NewPatch

func NewPatch(buf []byte) (res Patch, err error)

NewPatch takes a byte array and tries to unmarshal it.

func (Patch) Apply

func (p Patch) Apply(base []byte) (result []byte, err error, loc int)

ApplyJSON does the same thing as Apply, except the inputs should be JSON-containing byte arrays instead of unmarshalled JSON

type Pointer

type Pointer []pointerSegment

Pointer is a JSON Pointer

func NewPointer

func NewPointer(s string) (Pointer, error)

newpointer takes a string that conforms to RFC6901 and turns it into a JSON pointer.

func (Pointer) Append

func (p Pointer) Append(frag string) Pointer

func (Pointer) Chop

func (p Pointer) Chop() (string, Pointer)

Chop extracts the last element in the pointer, returning it and the rest of the pointer.

func (Pointer) Contains

func (a Pointer) Contains(b Pointer) bool

Contains tests to see if B is a subset of A

func (Pointer) Copy

func (p Pointer) Copy(from interface{}, at Pointer) (interface{}, error)

Copy deep-copies the value pointed to by p in from to the location pointed to by at.

func (Pointer) Get

func (p Pointer) Get(from interface{}) (interface{}, error)

Get takes an unmarshalled JSON blob, and returns the value pointed at by the pointer. The unmarshalled blob is left unchanged.

func (Pointer) MarshalJSON

func (p Pointer) MarshalJSON() ([]byte, error)

Allow a pointer to be marshalled to valid JSON.

func (Pointer) Move

func (p Pointer) Move(from interface{}, at Pointer) (interface{}, error)

Move moves the value pointed to by p in from to the location pointed to by at.

func (Pointer) Put

func (p Pointer) Put(to interface{}, val interface{}) (interface{}, error)

Put puts val into to at the position indicated by the pointer, returning a possibly new value for to. The position does not have to already exist or refer to a preexisting Value.

Put may have to return a new to if to happens to be a slice, since the semantics of Put necessarily involve growing the Slice.

func (*Pointer) Remove

func (p *Pointer) Remove(from interface{}) (interface{}, error)

Remove removes the value pointed to by the pointer from from, returning a possibly new value for from.

Remove may have to return a new from if it is a slice, because the semantics for Reomve on a Slice involve shrinking it, which involves reallocation the way we do it.

func (Pointer) Replace

func (p Pointer) Replace(to interface{}, val interface{}) (interface{}, error)

Replace replaces the pointed at value (which must exist) with val.

func (Pointer) Shift

func (p Pointer) Shift() (string, Pointer)

Shift extracts the first element in the pointer, returning it and the rest of the pointer.

func (Pointer) String

func (p Pointer) String() string

String takes a pointer and returns its string value.

func (*Pointer) Test

func (p *Pointer) Test(from interface{}, sample interface{}) error

func (*Pointer) UnmarshalJSON

func (p *Pointer) UnmarshalJSON(buf []byte) error

Allow unmarshalling from JSON

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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