-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathexamples_test.go
More file actions
131 lines (98 loc) · 2.22 KB
/
examples_test.go
File metadata and controls
131 lines (98 loc) · 2.22 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// SPDX-FileCopyrightText: Copyright (c) 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package jsonpointer
import (
"encoding/json"
"errors"
"fmt"
)
var ErrExampleStruct = errors.New("example error")
type exampleDocument struct {
Foo []string `json:"foo"`
}
func ExampleNew() {
empty, err := New("")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("empty pointer: %q\n", empty.String())
key, err := New("/foo")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("pointer to object key: %q\n", key.String())
elem, err := New("/foo/1")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("pointer to array element: %q\n", elem.String())
escaped0, err := New("/foo~0")
if err != nil {
fmt.Println(err)
return
}
// key contains "~"
fmt.Printf("pointer to key %q: %q\n", Unescape("foo~0"), escaped0.String())
escaped1, err := New("/foo~1")
if err != nil {
fmt.Println(err)
return
}
// key contains "/"
fmt.Printf("pointer to key %q: %q\n", Unescape("foo~1"), escaped1.String())
// output:
// empty pointer: ""
// pointer to object key: "/foo"
// pointer to array element: "/foo/1"
// pointer to key "foo~": "/foo~0"
// pointer to key "foo/": "/foo~1"
}
func ExamplePointer_Get() {
var doc exampleDocument
if err := json.Unmarshal(testDocumentJSONBytes, &doc); err != nil { // populates doc
fmt.Println(err)
return
}
pointer, err := New("/foo/1")
if err != nil {
fmt.Println(err)
return
}
value, kind, err := pointer.Get(doc)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf(
"value: %q\nkind: %v\n",
value, kind,
)
// Output:
// value: "baz"
// kind: string
}
func ExamplePointer_Set() {
var doc exampleDocument
if err := json.Unmarshal(testDocumentJSONBytes, &doc); err != nil { // populates doc
fmt.Println(err)
return
}
pointer, err := New("/foo/1")
if err != nil {
fmt.Println(err)
return
}
result, err := pointer.Set(&doc, "hey my")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("result: %#v\n", result)
fmt.Printf("doc: %#v\n", doc)
// Output:
// result: &jsonpointer.exampleDocument{Foo:[]string{"bar", "hey my"}}
// doc: jsonpointer.exampleDocument{Foo:[]string{"bar", "hey my"}}
}