-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
107 lines (84 loc) · 1.91 KB
/
examples_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package jsonpointer_test
import (
"encoding/json"
"fmt"
"github.com/chanced/jsonpointer"
)
func ExampleNew() {
ptr := jsonpointer.New("foo", "bar") // => "/foo/bar"
fmt.Println(`"` + ptr + `"`)
ptr = jsonpointer.New("foo/bar") // => "/foo~1bar"
fmt.Println(`"` + ptr + `"`)
ptr = jsonpointer.New() // => ""
fmt.Println(`"` + ptr + `"`)
ptr = jsonpointer.New("") // => "/"
fmt.Println(`"` + ptr + `"`)
ptr = jsonpointer.New("/") // => "/~1"
fmt.Println(`"` + ptr + `"`)
ptr = jsonpointer.New("~") // => "/~0"
fmt.Println(`"` + ptr + `"`)
ptr = jsonpointer.New("#/foo/bar") // => "/#~1foo~1bar"
fmt.Println(`"` + ptr + `"`)
// Output:
// "/foo/bar"
// "/foo~1bar"
// ""
// "/"
// "/~1"
// "/~0"
// "/#~1foo~1bar"
}
func ExampleAssign() {
type Bar struct {
Baz string `json:"baz"`
}
type Foo struct {
Bar Bar `json:"bar"`
}
var foo Foo
jsonpointer.Assign(&foo, "/bar/baz", "qux")
fmt.Println(foo.Bar.Baz)
// Assigning JSON by JSONPointer
foo.Bar.Baz = "quux"
b, _ := json.Marshal(foo)
jsonpointer.Assign(&b, "/bar/baz", "corge")
fmt.Println(string(b))
//Output: qux
//{"bar":{"baz":"corge"}}
}
func ExampleResolve() {
type Bar struct {
Baz string `json:"baz"`
}
type Foo struct {
Bar Bar `json:"bar,omitempty"`
}
foo := Foo{Bar{Baz: "qux"}}
var s string
jsonpointer.Resolve(foo, "/bar/baz", &s)
fmt.Println(s)
// Resolving JSON by JSONPointer
b, _ := json.Marshal(foo)
jsonpointer.Resolve(b, "/bar/baz", &s)
fmt.Println(s)
// Output: qux
// qux
}
func ExampleDelete() {
type Bar struct {
Baz string `json:"baz,omitempty"`
}
type Foo struct {
Bar Bar `json:"bar"`
}
foo := Foo{Bar{Baz: "qux"}}
jsonpointer.Delete(foo, "/bar/baz")
fmt.Printf("foo.Bar.Baz: %v\n", foo.Bar.Baz)
// Deleting JSON by JSONPointer
foo.Bar.Baz = "quux"
b, _ := json.Marshal(foo)
jsonpointer.Delete(&b, "/bar/baz")
fmt.Println(string(b))
// Output: foo.Bar.Baz: qux
// {"bar":{}}
}