jsonpatch

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

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

Go to latest
Published: Mar 3, 2018 License: MIT Imports: 6 Imported by: 0

README

Build Status Go Report Card GoDoc

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{
	{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

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

All available operations.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByPath

type ByPath []PatchOperation

func (ByPath) Len

func (a ByPath) Len() int

func (ByPath) Less

func (a ByPath) Less(i, j int) bool

func (ByPath) Swap

func (a ByPath) Swap(i, j int)

type OperationType

type OperationType string

Operation is a...

type Patch

type Patch []PatchOperation

Patch is a list of PatchOperations.

func CreatePatch

func CreatePatch(src, dst []byte) (Patch, error)

CreatePatch creates a patch as specified in http://jsonpatch.com/

'a' is original, 'b' is the modified document. Both are to be given as json encoded content. The function will return an array of JsonPatchOperations

An error will be returned if any of the two documents are invalid.

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) String

func (p Patch) String() string

type PatchOperation

type PatchOperation struct {
	From  string      `json:"from,omitempty"`
	Op    string      `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