-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
484 lines (429 loc) · 11.6 KB
/
node.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package njson
import (
"encoding"
"encoding/json"
"io"
"math"
"strconv"
"sync"
"github.com/alxarch/njson/numjson"
"github.com/alxarch/njson/strjson"
)
// Node is a reference to a node in a JSON Document.
// It is a versioned reference to avoid document manipulation after reset.
type Node struct {
id uint
rev uint
doc *Document
}
// Unmarshaler is the interface implemented by types that can unmarshal from a Node.
type Unmarshaler interface {
UnmarshalNodeJSON(n Node) error
}
// ID returns a node's id.
func (n Node) ID() uint {
return n.id
}
// With returns a document node for id.
func (n Node) With(id uint) Node {
n.id = id
return n
}
// Document returns a node's document.
func (n Node) Document() *Document {
if n.doc != nil && n.doc.rev == n.rev {
return n.doc
}
// Unlink invalid Document reference
n.doc = nil
return nil
}
func (n Node) get() *node {
if n.doc != nil && n.doc.rev == n.rev {
if n.id < uint(len(n.doc.nodes)) {
return &n.doc.nodes[n.id]
}
} else {
// Unlink invalid Document reference
n.doc = nil
}
return nil
}
// AppendJSON appends a node's JSON data to a byte slice.
func (n Node) AppendJSON(dst []byte) ([]byte, error) {
if nn := n.get(); nn != nil {
return n.doc.appendJSON(dst, nn)
}
return nil, &typeError{TypeInvalid, TypeAnyValue}
}
// Raw returns the JSON string of a Node's value.
// Object and Array nodes return an empty string.
func (n Node) Raw() string {
if n := n.get(); n != nil {
return n.raw
}
return ""
}
// Unescaped unescapes the value of a String Node.
func (n Node) Unescaped() string {
if n := n.get(); n != nil && n.info.IsString() {
return strjson.Unescaped(n.raw)
}
return ""
}
// ToFloat converts a node's value to float64.
func (n Node) ToFloat() (float64, bool) {
if n := n.get(); n != nil {
f := numjson.ParseFloat(n.raw)
return f, f == f
}
return 0, false
}
// ToInt converts a node's value to int64.
func (n Node) ToInt() (int64, bool) {
if n := n.get(); n != nil {
f := numjson.ParseFloat(n.raw)
return int64(f), math.MinInt64 <= f && f < math.MaxInt64 && math.Trunc(f) == f
}
return 0, false
}
// ToUint converts a node's value to uint64.
func (n Node) ToUint() (uint64, bool) {
if n := n.get(); n != nil {
f := numjson.ParseFloat(n.raw)
return uint64(f), 0 <= f && f < math.MaxUint64 && math.Trunc(f) == f
}
return 0, false
}
// ToBool converts a Node to bool.
func (n Node) ToBool() (bool, bool) {
if n := n.get(); n != nil && n.info.IsBoolean() {
switch n.raw {
case strTrue:
return true, true
case strFalse:
return false, true
}
}
return false, false
}
// Type returnsa a Node's type.
func (n Node) Type() Type {
if n := n.get(); n != nil {
return n.info.Type()
}
return TypeInvalid
}
// Values returns a value iterator over an Array or Object values.
func (n Node) Values() IterV {
if n := n.get(); n != nil {
return IterV{values: n.values}
}
return IterV{}
}
// TypeError returns an error for a type not matching a Node's type.
func (n Node) TypeError(want Type) error {
return typeError{n.Type(), want}
}
// Lookup finds a node by path
func (n Node) Lookup(path ...string) Node {
return n.With(n.Document().lookup(n.id, path))
}
// ToInterface converts a Node to a generic interface{}.
func (n Node) ToInterface() (interface{}, bool) {
return n.Document().toInterface(n.id)
}
var bufferpool = &sync.Pool{
New: func() interface{} {
return make([]byte, 2048)
},
}
// Appender is a Marshaler interface for buffer append workflows.
type Appender interface {
AppendJSON([]byte) ([]byte, error)
}
// PrintJSON is a helper to write an Appender to an io.Writer
func PrintJSON(w io.Writer, a Appender) (n int, err error) {
b := bufferpool.Get().([]byte)
if b, err = a.AppendJSON(b[:0]); err == nil {
n, err = w.Write(b)
}
bufferpool.Put(b)
return
}
// PrintJSON writes JSON to an io.Writer.
func (n Node) PrintJSON(w io.Writer) (int, error) {
return PrintJSON(w, n)
}
// WrapUnmarshalJSON wraps a call to the json.Unmarshaler interface
func (n Node) WrapUnmarshalJSON(u json.Unmarshaler) (err error) {
node := n.get()
if node == nil {
return typeError{TypeInvalid, TypeAnyValue}
}
switch node.info.Type() {
case TypeArray:
if len(node.values) == 0 {
return u.UnmarshalJSON([]byte{delimBeginArray, delimEndArray})
}
case TypeObject:
if len(node.values) == 0 {
return u.UnmarshalJSON([]byte{delimBeginObject, delimEndObject})
}
case TypeString:
if node.raw == "" {
return u.UnmarshalJSON([]byte{delimString, delimString})
}
case TypeInvalid:
return typeError{TypeInvalid, TypeAnyValue}
}
data := bufferpool.Get().([]byte)
data, err = n.AppendJSON(data[:0])
if err == nil {
err = u.UnmarshalJSON(data)
}
bufferpool.Put(data)
return
}
// WrapUnmarshalText wraps a call to the encoding.TextUnmarshaler interface
func (n Node) WrapUnmarshalText(u encoding.TextUnmarshaler) (err error) {
if node := n.get(); node != nil {
switch t := node.info.Type(); t {
case TypeString:
buf := bufferpool.Get().([]byte)
buf = append(buf[:0], node.raw...)
err = u.UnmarshalText(buf)
bufferpool.Put(buf)
return
default:
return newTypeError(t, TypeString)
}
}
return newTypeError(TypeInvalid, TypeString)
}
// Get gets a Node by key.
// If the key is not found the returned node's id
// will be MaxID and the Node will behave as empty.
func (n Node) Get(key string) Node {
if nn := n.get(); nn != nil && nn.info.IsObject() {
for i := range nn.values {
if key == nn.values[i].key {
n.id = nn.values[i].id
return n
}
}
}
n.id = maxUint
return n
}
// Index gets the Node at offset i of an Array.
// If the index is out of bounds it sets the id to MaxUint
// and the Node will behave as empty but still have a usable
// reference to Document.
func (n Node) Index(i int) Node {
if nn := n.get(); nn != nil && nn.info.IsArray() && 0 <= i && i < len(nn.values) {
n.id = nn.values[i].id
return n
}
n.id = maxUint
return n
}
// Set assigns a Node to the key of an Object Node.
// Since most keys need no escaping it doesn't escape the key.
// If the key needs escaping use strjson.Escaped.
func (n Node) Set(key string, value Node) {
if nn := n.get(); nn != nil && nn.info.IsObject() {
// Make a copy of the value if it's not Orphan to avoid recursion infinite loops.
id := n.doc.copyOrAdopt(value.Document(), value.ID(), n.id)
if id < maxUint {
// copyOrAdopt might grow nodes array invalidating nn pointer
nn = &n.doc.nodes[n.id]
var v *V
for i := range nn.values {
v = &nn.values[i]
if v.key == key {
v.id = id
return
}
}
nn.values = append(nn.values, V{
id: id,
key: key,
})
}
}
}
// Append appends a node id to an Array node's values.
func (n Node) Append(values ...Node) {
if len(values) == 0 {
return
}
if nn := n.get(); nn != nil && nn.info.IsArray() {
vv := nn.values
for _, v := range values {
vv = append(vv, V{
id: n.doc.copyOrAdopt(v.Document(), v.ID(), n.id),
key: "",
})
}
// copyOrAdopt might grow nodes array invalidating nn pointer
n.doc.nodes[n.id].values = vv
}
}
// TODO: Splice, Prepend
// Slice reslices an Array node.
func (n Node) Slice(i, j int) {
if n := n.get(); n != nil && n.info.IsArray() && 0 <= i && i < j && j < len(n.values) {
n.values = n.values[i:j]
}
}
// Replace replaces the value at offset i of an Array node.
func (n Node) Replace(i int, value Node) {
if nn := n.get(); nn != nil && nn.info.IsArray() && 0 <= i && i < len(nn.values) {
// Make a copy of the value if it's not Orphan to avoid recursion infinite loops.
id := n.doc.copyOrAdopt(value.Document(), value.ID(), n.id)
if id < maxUint {
// copyOrAdopt might grow nodes array invalidating nn pointer
n.doc.nodes[n.id].values[i] = V{id, ""}
}
}
}
// Remove removes the value at offset i of an Array node.
func (n Node) Remove(i int) {
if n := n.get(); n != nil && n.info.IsArray() && 0 <= i && i < len(n.values) {
if j := i + 1; 0 <= j && j < len(n.values) {
copy(n.values[i:], n.values[j:])
}
if j := len(n.values) - 1; 0 <= j && j < len(n.values) {
n.values[j] = V{}
n.values = n.values[:j]
}
}
}
// Strip recursively deletes a key from a node.
func (n Node) Strip(key string) {
if nn := n.get(); nn != nil && nn.info.IsObject() {
for i := range nn.values {
v := &nn.values[i]
if key == v.key {
if j := len(nn.values) - 1; 0 <= j && j < len(nn.values) {
nn.values[i] = nn.values[j]
nn.values[j] = V{}
nn.values = nn.values[:j]
for j := i; 0 <= j && j < len(nn.values); j++ {
n.With(nn.values[j].id).Strip(key)
}
}
return
}
n.With(v.id).Strip(key)
}
}
}
// Del finds a key in an Object node's values and removes it.
// It does not keep the order of keys.
func (n Node) Del(key string) {
if n := n.get(); n != nil && n.info.IsObject() {
for i := range n.values {
if n.values[i].key == key {
if j := len(n.values) - 1; 0 <= j && j < len(n.values) {
n.values[i] = n.values[j]
n.values[j] = V{}
n.values = n.values[:j]
}
return
}
}
}
}
// SetInt sets a Node's value to an integer.
func (n Node) SetInt(i int64) {
if n := n.get(); n != nil {
n.reset(vNumber|n.info.Flags(), strconv.FormatInt(i, 10), n.values[:0])
}
}
// SetUint sets a Node's value to an unsigned integer.
func (n Node) SetUint(u uint64) {
if n := n.get(); n != nil {
n.reset(vNumber|n.info.Flags(), strconv.FormatUint(u, 10), n.values[:0])
}
}
// SetFloat sets a Node's value to a float number.
func (n Node) SetFloat(f float64) {
if n := n.get(); n != nil {
n.reset(vNumber|n.info.Flags(), numjson.FormatFloat(f, 64), n.values[:0])
}
}
// SetString sets a Node's value to a string escaping invalid JSON characters.
func (n Node) SetString(s string) {
n.SetStringRaw(strjson.Escaped(s, false, false))
}
// SetStringHTML sets a Node's value to a string escaping invalid JSON and unsafe HTML characters.
func (n Node) SetStringHTML(s string) {
n.SetStringRaw(strjson.Escaped(s, true, false))
}
// SetStringRaw sets a Node's value to a string without escaping.
// Unless the provided string is guaranteed to not contain any JSON invalid characters,
// JSON output from this Node will be invalid.
func (n Node) SetStringRaw(s string) {
if n := n.get(); n != nil {
n.reset(vString|n.info.Flags(), s, n.values[:0])
}
}
// SetFalse sets a Node's value to false.
func (n Node) SetFalse() {
if n := n.get(); n != nil {
n.reset(vBoolean|n.info.Flags(), strFalse, n.values[:0])
}
}
// SetTrue sets a Node's value to true.
func (n Node) SetTrue() {
if n := n.get(); n != nil {
n.reset(vBoolean|n.info.Flags(), strTrue, n.values[:0])
}
}
// SetNull sets a Node's value to null.
func (n Node) SetNull() {
if n := n.get(); n != nil {
n.reset(vNull|n.info.Flags(), strNull, n.values[:0])
}
}
// IterV is an iterator over a node's values.
type IterV struct {
*V
index int
values []V
}
// Reset resets the iterator.
func (i *IterV) Reset() {
i.index = 0
i.V = nil
}
// Close closes the iterator unlinking the values slice.
func (i *IterV) Close() {
i.values = nil
i.index = -1
i.V = nil
}
// Next increments the iteration cursor and checks if the iterarion finished.
func (i *IterV) Next() bool {
if 0 <= i.index && i.index < len(i.values) {
i.V = &i.values[i.index]
i.index++
return true
}
i.V = nil
// Set index to -1 so every Next() returns false until Reset() is called.
i.index = -1
return false
}
// Len returns the length of the values.
func (i *IterV) Len() int {
return len(i.values)
}
// Index returns the current iteration index.
// Before Next() is called for the first time it returns -1.
// After the iteration has finished it returns -2.
func (i *IterV) Index() int {
return i.index - 1
}