-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
214 lines (177 loc) · 4.52 KB
/
query.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
package jzon
import (
"fmt"
"strings"
)
// state indicates the inner state of the `parsePath` state machine
type state int64
const (
// $.key1[1].big-array[1:4]
_Start state = 2 << iota
_Dollar // $ root
_Dot // . key mark
_LeftSB // [ index mark
_RightSB // ] index end
_Key // .* object key
_Index // [1-9]\d+ array index
_Colon // : slice mark
_Semicolon // ; line tail
)
var stateStrings = map[state]string{
_Start: "_Start",
_Dollar: "_Dollar",
_Dot: "_Dot",
_LeftSB: "_LeftSB",
_RightSB: "_RightSB",
_Key: "_Key",
_Index: "_Index",
_Colon: "_Colon",
_Semicolon: "_Semicolon",
}
func (st state) match(states ...state) bool {
for _, s := range states {
if uint64(st)&uint64(s) > 0 {
return true
}
}
return false
}
// Query searches a child node in an object or an array, if the
// node at the path doesn't exist, an error will be thrown out
func (jz *Jzon) Query(path string) (g *Jzon, err error) {
// in the implements of function `parsePath()` we don't handle
// exceptions about slice bounds out of range. here we simply
// throw the error recovered from those unhandled exceptions
defer func() {
e := recover()
if e != nil {
err = fmt.Errorf("maybe out of bound: %v", e)
}
}()
return parsePath(jz, append([]byte(path), ';'))
}
// Search determines whether there exists the node on the given path
func (jz *Jzon) Search(path string) (exists bool) {
found, _ := jz.Query(path)
return found != nil
}
func expectState(real state, ex []state) error {
var sa []string
for _, s := range ex {
sa = append(sa, stateStrings[s])
}
expectStates := strings.Join(sa, " | ")
return fmt.Errorf("expect state %s, but the real state is %s", expectStates, stateStrings[real])
}
func parsePath(root *Jzon, path []byte) (curr *Jzon, err error) {
var st = _Start
var ex = []state{_Dollar}
var key string
// a typical state machine model
for {
switch {
case path[0] == '$' && st.match(_Start):
ex = []state{_Dot, _LeftSB, _Semicolon}
st = _Dollar
curr = root
path = path[1:]
case path[0] == ';' && st.match(_Dollar, _RightSB, _Key):
ex = []state{}
st = _Semicolon
return
case path[0] == '.' && st.match(_Dollar, _Key, _RightSB):
ex = []state{_Key}
st = _Dot
path = path[1:]
case path[0] == '[' && st.match(_Dollar, _Key):
ex = []state{_Index}
st = _LeftSB
path = path[1:]
case isDigit(path[0]) && st.match(_LeftSB):
ex = []state{_RightSB}
st = _Index
var n int64
var f float64
var isInt bool
n, f, isInt, path, err = parseNumeric(path)
if err != nil {
return
}
if !isInt {
err = fmt.Errorf("expect an integer index, but found float: %v", f)
return
}
curr, err = curr.ValueAt(int(n))
if err != nil {
return
}
case path[0] == ']' && st.match(_Index):
ex = []state{_Dot, _Semicolon}
st = _RightSB
path = path[1:]
case st.match(_Dot):
ex = []state{_Dot, _LeftSB, _Semicolon}
st = _Key
key, path, err = parsePathKey(path)
if err != nil {
return
}
curr, err = curr.ValueOf(key)
if err != nil {
return
}
default:
return nil, expectState(st, ex)
}
}
}
// parsePathKey parses as `parseKey()`, except that the given string
// isn't surrounded with ", and it will escape some more characters
func parsePathKey(path []byte) (k string, rem []byte, err error) {
var parsed = make([]byte, 0, SHORT_STRING_OPTIMIZED_CAP)
var c byte
rem = path
for {
switch {
case rem[0] == '\\' && rem[1] == 'u':
utf8str := make([]byte, 0, SHORT_STRING_OPTIMIZED_CAP)
utf8str, rem, err = parseUnicode(rem)
for _, c := range utf8str {
parsed = append(parsed, c)
}
continue
case rem[0] == '\\' && rem[1] == '.':
parsed = append(parsed, '.')
rem = rem[2:]
continue
case rem[0] == '\\' && rem[1] == '[':
parsed = append(parsed, '[')
rem = rem[2:]
continue
case rem[0] == '\\' && rem[1] == ']':
parsed = append(parsed, ']')
rem = rem[2:]
continue
case rem[0] == '\\' && rem[1] == ';':
parsed = append(parsed, ';')
rem = rem[2:]
continue
case rem[0] == '\\' && rem[1] != 'u':
c, rem, err = parseEscaped(rem)
if err != nil {
return
}
parsed = append(parsed, c)
continue
case rem[0] == '.' || rem[0] == '[' || rem[0] == ';':
goto End
default:
parsed = append(parsed, rem[0])
pos.col += 1
rem = rem[1:]
continue
}
}
End:
return string(parsed), rem, nil
}