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
type Patch ¶
type Patch []PatchOperation
Patch is a list of PatchOperations.
func CreatePatch ¶
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 MakePatch ¶
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]
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
Click to show internal directories.
Click to hide internal directories.