jsonpatch

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

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

Go to latest
Published: Feb 23, 2018 License: MIT Imports: 6 Imported by: 0

README

Build Status

go-jsonpatch

A jsonpatch library for go.

Status

This library currently serves my (limited) purposes, but to my knowledge hasn't been thoroughly tested or deployed in a production environment yet.

Documentation

Overview

Ported from github.com/stefankoegl/python-json-patch

Example
patch := jsonpatch.Patch{
	Operations: []jsonpatch.PatchOperation{
		{Op: jsonpatch.Add, Path: "/foo", Value: "bar"},
		{Op: jsonpatch.Add, Path: "/baz", Value: []interface{}{1, 2, 3}},
		{Op: jsonpatch.Remove, Path: "/baz/1"},
		{Op: jsonpatch.Test, Path: "/baz", Value: []interface{}{1, 3}},
		{Op: jsonpatch.Replace, Path: "/baz/0", Value: 42},
		{Op: jsonpatch.Remove, Path: "/baz/1"},
	},
}

// apply the patch to an empty document
doc := make(map[string]interface{})
err := patch.Apply(&doc)
if err != nil {
	log.Fatalf("apply: %v", err)
}

fmt.Println(doc["foo"])
fmt.Println(doc["baz"])
Output:

bar
[42]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OperationType

type OperationType string

Operation is a...

const (
	Remove  OperationType = "remove"
	Add     OperationType = "add"
	Replace OperationType = "replace"
	Move    OperationType = "move"
	Test    OperationType = "test"
	Copy    OperationType = "copy"
)

All available operations.

type Patch

type Patch struct {
	Operations []PatchOperation
}

Patch is a list of PatchOperations.

func FromString

func FromString(str string) (Patch, error)

func MakeDiff

func MakeDiff(src, dst interface{}) (Patch, error)

func MakePatch

func MakePatch(src interface{}, dst interface{}) (Patch, error)

MakePatch generates a patch by comparing two documents.

Example
var src map[string]interface{}
rawsrc := `{"foo":"bar","numbers":[1,3,4,8]}`
err := json.Unmarshal([]byte(rawsrc), &src)
if err != nil {
	panic(err)
}

var dst map[string]interface{}
rawdst := `{"foo":"qux","numbers":[1,4,7]}`
err = json.Unmarshal([]byte(rawdst), &dst)
if err != nil {
	panic(err)
}

patch, err := jsonpatch.MakePatch(src, dst)
if err != nil {
	panic(err)
}
err = patch.Apply(&src)
if err != nil {
	panic(err)
}

fmt.Println(src["foo"])
fmt.Println(src["numbers"])
Output:

qux
[1 4 7]

func (*Patch) Apply

func (p *Patch) Apply(doc interface{}) (err error)

func (Patch) MarshalJSON

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

func (*Patch) UnmarshalJSON

func (p *Patch) UnmarshalJSON(b []byte) error

type PatchOperation

type PatchOperation struct {
	From  string        `json:"from,omitempty"`
	Op    OperationType `json:"op"`
	Path  string        `json:"path"`
	Value interface{}   `json:"value,omitempty"`
}

func (*PatchOperation) Apply

func (self *PatchOperation) Apply(doc interface{}) error

Jump to

Keyboard shortcuts

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