-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathiface_example_test.go
More file actions
150 lines (118 loc) · 2.73 KB
/
iface_example_test.go
File metadata and controls
150 lines (118 loc) · 2.73 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// SPDX-FileCopyrightText: Copyright (c) 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package jsonpointer_test
import (
"fmt"
"github.com/go-openapi/jsonpointer"
)
var (
_ jsonpointer.JSONPointable = CustomDoc{}
_ jsonpointer.JSONSetable = &CustomDoc{}
)
// CustomDoc accepts 2 preset properties "propA" and "propB", plus any number of extra properties.
//
// All values are strings.
type CustomDoc struct {
a string
b string
c map[string]string
}
// JSONLookup implements [jsonpointer.JSONPointable].
func (d CustomDoc) JSONLookup(key string) (any, error) {
switch key {
case "propA":
return d.a, nil
case "propB":
return d.b, nil
default:
if len(d.c) == 0 {
return nil, fmt.Errorf("key %q not found: %w", key, ErrExampleIface)
}
extra, ok := d.c[key]
if !ok {
return nil, fmt.Errorf("key %q not found: %w", key, ErrExampleIface)
}
return extra, nil
}
}
// JSONSet implements [jsonpointer.JSONSetable].
func (d *CustomDoc) JSONSet(key string, value any) error {
asString, ok := value.(string)
if !ok {
return fmt.Errorf("a CustomDoc only access strings as values, but got %T: %w", value, ErrExampleIface)
}
switch key {
case "propA":
d.a = asString
return nil
case "propB":
d.b = asString
return nil
default:
if len(d.c) == 0 {
d.c = make(map[string]string)
}
d.c[key] = asString
return nil
}
}
func Example_iface() {
doc := CustomDoc{
a: "initial value for a",
b: "initial value for b",
// no extra values
}
pointerA, err := jsonpointer.New("/propA")
if err != nil {
fmt.Println(err)
return
}
// get the initial value for a
propA, kind, err := pointerA.Get(doc)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("propA (%v): %v\n", kind, propA)
pointerB, err := jsonpointer.New("/propB")
if err != nil {
fmt.Println(err)
return
}
// get the initial value for b
propB, kind, err := pointerB.Get(doc)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("propB (%v): %v\n", kind, propB)
pointerC, err := jsonpointer.New("/extra")
if err != nil {
fmt.Println(err)
return
}
// not found yet
_, _, err = pointerC.Get(doc)
fmt.Printf("propC: %v\n", err)
_, err = pointerA.Set(&doc, "new value for a") // doc is updated in place
if err != nil {
fmt.Println(err)
return
}
_, err = pointerB.Set(&doc, "new value for b")
if err != nil {
fmt.Println(err)
return
}
_, err = pointerC.Set(&doc, "new extra value")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("updated doc: %v", doc)
// output:
// propA (string): initial value for a
// propB (string): initial value for b
// propC: key "extra" not found: example error
// updated doc: {new value for a new value for b map[extra:new extra value]}
}