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 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]
func (Patch) MarshalJSON ¶
func (*Patch) UnmarshalJSON ¶
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
Click to show internal directories.
Click to hide internal directories.