-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_test.go
281 lines (247 loc) · 6.85 KB
/
types_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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2022 Chance Dinkins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// The License can be found in the LICENSE file.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package jsonpointer_test
import (
"encoding/json"
"fmt"
"reflect"
"github.com/chanced/jsonpointer"
)
type Root struct {
Nested Nested `json:"nested"`
NestedPtr *Nested `json:"nestedptr"`
}
type Nested struct {
Inline `json:",inline"`
Embedded `json:"embedded"`
private string
InterContainer InterContainer `json:"interface"`
Nested *Nested `json:"nested,omitempty"`
Empty *Nested `json:"empty,omitempty"`
Str string `json:"str,omitempty"`
Int int `json:"int,omitempty"`
IntPtr *int `json:"intptr,omitempty"`
Float float64 `json:"float,omitempty"`
FloatPtr *float64 `json:"floatptr,omitempty"`
Bool bool `json:"bool"`
BoolPtr *bool `json:"boolptr,omitempty"`
Uint uint `json:"uint,omitempty"`
EntryMap map[string]*Entry `json:"entrymap,omitempty"`
StrMap map[string]string `json:"strmap,omitempty"`
IntMap map[int]int `json:"intmap,omitempty"`
UintMap map[uint]uint `json:"uintmap,omitempty"`
CustomMap map[Key]string `json:"custommap,omitempty"`
AnonStruct struct{ Value string } `json:"anon,omitempty"`
StrSlice []string `json:"strslice,omitempty"`
IntSlice []int `json:"intslice,omitempty"`
EntrySlice []*Entry `json:"entryslice,omitempty"`
StrArray [3]string `json:"strarray,omitempty"`
IntArray [3]int `json:"intarray,omitempty"`
Yield Yield `json:"yield"`
Deleter DeleterImpl `json:"deleter"`
JSON json.RawMessage `json:"json"`
AnonStructPtr *struct {
Value string
} `json:"anonptr"`
}
type DeleterImpl struct {
Values map[string]string
}
func (d *DeleterImpl) DeleteByJSONPointer(ptr *jsonpointer.Pointer) error {
t, ok := ptr.NextToken()
if !ok {
panic("token not available? pointer: " + ptr.String())
}
delete(d.Values, t.String())
return nil
}
type InterContainer struct {
Interface Interface `json:",inline"`
}
func (ic *InterContainer) AssignByJSONPointer(ptr *jsonpointer.Pointer, v interface{}) error {
switch typ := v.(type) {
case Interface:
ic.Interface = typ
return nil
default:
panic("unexpected type: " + reflect.TypeOf(v).String())
}
}
func (ic InterContainer) ResolveJSONPointer(ptr *jsonpointer.Pointer, op jsonpointer.Operation) (interface{}, error) {
p, t, ok := ptr.Next()
if !ok {
return nil, fmt.Errorf("unexpected root pointer: %s", ptr.String())
}
switch t {
case "private":
if in, ok := ic.Interface.(*privateImpl); ok {
*ptr = p
return in, nil
}
if op.IsAssigning() {
*ptr = p
x := &privateImpl{private: &struct{ value uint }{value: 5}}
return x, nil
}
case "public":
if in, ok := ic.Interface.(*PublicImpl); ok {
*ptr = p
return in, nil
}
if op.IsAssigning() {
*ptr = p
return &PublicImpl{}, nil
}
}
panic("unexpected pointer: " + ptr.String())
}
type Interface interface {
jsonpointer.Assigner
jsonpointer.Deleter
jsonpointer.Resolver
Value() uint
}
type privateImpl struct {
private *struct {
value uint
}
}
func (pi *privateImpl) MarshalJSON() ([]byte, error) {
if pi.private == nil {
return []byte(`{"value": null}`), nil
}
return []byte(fmt.Sprintf(`{"value":%d}`, pi.private.value)), nil
}
func (pi *privateImpl) AssignByJSONPointer(ptr *jsonpointer.Pointer, v interface{}) error {
if v == nil {
pi.private = nil
return nil
}
pi.private = &struct {
value uint
}{
value: v.(uint),
}
return nil
}
func (pi *privateImpl) DeleteByJSONPointer(ptr *jsonpointer.Pointer) error {
t, ok := ptr.NextToken()
if !ok {
panic("token not available? pointer: " + ptr.String())
}
if t != "value" {
panic("unexpected ptr: " + ptr.String())
}
pi.private = nil
return nil
}
func (pi privateImpl) ResolveJSONPointer(ptr *jsonpointer.Pointer, op jsonpointer.Operation) (interface{}, error) {
np, t, ok := ptr.Next()
if !ok {
panic("token not available? pointer: " + ptr.String())
}
if t != "value" {
np, t, ok = np.Next()
if !ok {
return nil, fmt.Errorf("unexpected root pointer: %s", ptr.String())
}
*ptr = np
}
if t != "value" {
panic("unexpected pointer: " + ptr.String())
}
if pi.private == nil {
return uint(0), nil
}
return pi.private.value, nil
}
func (p *privateImpl) Value() uint {
if p.private == nil {
return uint(0)
}
return p.private.value
}
var _ Interface = (*privateImpl)(nil)
type PublicImpl struct {
value uint
}
func (pi *PublicImpl) AssignByJSONPointer(ptr *jsonpointer.Pointer, v interface{}) error {
t, ok := ptr.NextToken()
if !ok {
panic("token not available? pointer: " + ptr.String())
}
if t != "value" {
panic("unexpected pointer: " + ptr.String())
}
pi.value = v.(uint)
return nil
}
func (pi *PublicImpl) DeleteByJSONPointer(ptr *jsonpointer.Pointer) error {
t, ok := ptr.NextToken()
if !ok {
panic("token not available? pointer: " + ptr.String())
}
if t != "value" {
panic("unexpected ptr: " + ptr.String())
}
pi = nil
return nil
}
func (pi *PublicImpl) ResolveJSONPointer(ptr *jsonpointer.Pointer, op jsonpointer.Operation) (interface{}, error) {
t, ok := ptr.NextToken()
if !ok {
panic("token not available? pointer: " + ptr.String())
}
if t != "value" {
panic("unexpected ptr: " + ptr.String())
}
if pi == nil {
return nil, nil
}
return pi.value, nil
}
func (p PublicImpl) Value() uint {
return p.value
}
var _ Interface = (*PublicImpl)(nil)
type Key struct {
key string
}
func (k *Key) UnmarshalText(data []byte) error {
k.key = string(data)
return nil
}
func (k Key) MarshalText() ([]byte, error) {
return []byte(k.key), nil
}
type Entry struct {
Name string `json:"name,omitempty"`
Value float64 `json:"value,omitempty"`
}
type Inline struct {
InlineStr string `json:"inline,omitempty"`
}
type Embedded struct {
Value string `json:"value,omitempty"`
}
type Yield struct {
Value string
}
func (y Yield) ResolveJSONPointer(p *jsonpointer.Pointer, op jsonpointer.Operation) (interface{}, error) {
return nil, jsonpointer.YieldOperation
}
type JSONValue struct {
Obj struct {
Value string `json:"value"`
} `json:"obj"`
}