-
Notifications
You must be signed in to change notification settings - Fork 0
/
seek.go
95 lines (76 loc) · 1.58 KB
/
seek.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
package json
import "errors"
// Seek errors.
var (
ErrNoSuchKey = errors.New("no such object key")
ErrOutOfBounds = errors.New("out of array bounds")
)
// Seek seeks to the beginning of the value at the path – list of object keys and array indexes.
// If you parse multiple object and you only need one value from each,
// it's good to use Break(len(path)) to move to the beginning of the next object.
func (d *Decoder) Seek(b []byte, st int, path ...interface{}) (i int, err error) {
i = st
for _, p := range path {
switch p := p.(type) {
case string:
i, err = d.seekObj(b, i, p)
case int:
i, err = d.seekArr(b, i, p)
}
if err != nil {
return i, err
}
}
return i, nil
}
func (d *Decoder) seekObj(b []byte, st int, key string) (i int, err error) {
i, err = d.Enter(b, st, Object)
if err != nil {
return
}
var k []byte
for err == nil && d.ForMore(b, &i, Object, &err) {
k, i, err = d.Key(b, i)
if err != nil {
return
}
if string(k) == key {
_, i, err = d.Type(b, i)
return
}
i, err = d.Skip(b, i)
}
if err != nil {
return
}
return i, ErrNoSuchKey
}
func (d *Decoder) seekArr(b []byte, st, idx int) (i int, err error) {
if idx < 0 {
l, i, err := d.Length(b, st)
if err != nil {
return i, err
}
idx = l + idx
}
if idx < 0 {
return st, ErrOutOfBounds
}
i, err = d.Enter(b, st, Array)
if err != nil {
return
}
j := 0
for err == nil && d.ForMore(b, &i, Array, &err) {
if j == idx {
_, i, err = d.Type(b, i)
return
}
i, err = d.Skip(b, i)
j++
}
if err != nil {
return
}
return i, ErrOutOfBounds
}