-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexample_test.go
68 lines (57 loc) · 1.3 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package jsonpatch_test
import (
"encoding/json"
"fmt"
"log"
"github.com/cameront/go-jsonpatch"
)
func 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]
}
func ExampleMakePatch() {
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]
}