-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
268 lines (255 loc) · 5.01 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
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
package yt
import (
"io/ioutil"
"strconv"
"strings"
"unicode"
)
const (
kindKey int = iota
kindIndex
kindFile
kindValue
)
type stateVal int
const (
invalid stateVal = iota
startElement
key
index
file
)
type queryElement struct {
kind int
key string
index int
file string
}
func Query(v interface{}, query string,
visited map[string]bool) (interface{}, error) {
elements, err := lex(query)
if err != nil {
return nil, err
}
value, err := execQuery(v, elements, visited)
return value, err
}
func Insert(src, value interface{}, query string) (interface{}, error) {
elements, err := lex(query)
if err != nil {
return nil, err
}
v, err := insertQuery(src, value, elements)
return v, err
}
func lex(query string) ([]queryElement, error) {
state := invalid
var token string
qe := make([]queryElement, 0)
for _, r := range query {
switch {
case r == '.':
switch state {
case invalid:
state = startElement
case startElement:
return nil, ErrEmptyToken{}
case key:
qe = append(qe, queryElement{
kind: kindKey,
key: token,
})
state = startElement
token = ""
case index:
index, err := strconv.ParseInt(token, 10, 0)
if err != nil {
return nil, err
}
qe = append(qe, queryElement{
kind: kindIndex,
index: int(index),
})
state = startElement
token = ""
case file:
token += string(r)
}
case unicode.IsNumber(r):
switch state {
case invalid:
token += string(r)
state = file
case startElement:
token = string(r)
state = index
case key:
fallthrough
case index:
fallthrough
case file:
token += string(r)
}
case r == '\'' || r == '"':
switch state {
case invalid:
token += string(r)
state = file
case startElement:
fallthrough
case key:
fallthrough
case index:
return nil, ErrUnexpectedRune{
Rune: r,
}
case file:
fileName := strings.TrimLeft(strings.TrimRight(token, `'"`),
`'"`)
qe = append(qe, queryElement{
kind: kindFile,
file: fileName,
})
state = invalid
token = ""
}
default:
switch state {
case invalid:
token += string(r)
state = file
case startElement:
token = string(r)
state = key
case key:
token += string(r)
case index:
return nil, ErrUnexpectedRune{
Rune: r,
}
case file:
token += string(r)
}
}
}
switch state {
case invalid:
if len(qe) == 0 {
return nil, ErrInvalidQuery{}
}
case key:
qe = append(qe, queryElement{
kind: kindKey,
key: token,
})
case index:
index, err := strconv.ParseInt(token, 10, 0)
if err != nil {
return nil, err
}
qe = append(qe, queryElement{
kind: kindIndex,
index: int(index),
})
case file:
fileName := strings.TrimLeft(strings.TrimRight(token, `'"`), `'"`)
qe = append(qe, queryElement{
kind: kindFile,
file: fileName,
})
}
return qe, nil
}
//Parsing is implied by query elements. If the first query element is a file,
// the value of v is ignored.
func execQuery(v interface{}, query []queryElement,
visited map[string]bool) (interface{}, error) {
vPart := v
var err error
for i, qe := range query {
switch qe.kind {
case kindKey:
vPart, err = getKey(qe.key, vPart)
if err != nil {
return nil, err
}
case kindIndex:
vPart, err = getIndex(qe.index, vPart)
if err != nil {
return nil, err
}
case kindFile:
if _, ok := visited[qe.file]; ok {
return nil, ErrCycleDetected{Source: qe.file}
}
visited[qe.file] = true
if i > 0 {
return nil, ErrInvalidQuery{}
}
f, fileError := ioutil.ReadFile(qe.file)
if fileError != nil {
return nil, fileError
}
vPart, err = Compile(f, visited)
if err != nil {
return nil, err
}
default:
return nil, ErrInvalidQuery{}
}
}
return vPart, nil
}
func getKey(key string, v interface{}) (interface{}, error) {
m, ok := v.(map[interface{}]interface{})
if !ok {
return nil, ErrExpectedMap{}
}
value, ok := m[key]
if !ok {
return nil, ErrKeyNotFound{
Key: key,
}
}
return value, nil
}
func getIndex(index int, v interface{}) (interface{}, error) {
a, ok := v.([]interface{})
if !ok {
return nil, ErrExpectedArray{}
}
if index >= len(a) || index < 0 {
return nil, ErrOutOfBounds{}
}
value := a[index]
return value, nil
}
//Recursive insert; does not support file kinds
func insertQuery(src, insert interface{}, qe []queryElement) (interface{}, error) {
if len(qe) == 0 {
return insert, nil
}
var err error
q := qe[0]
switch q.kind {
case kindKey:
m, ok := src.(map[interface{}]interface{})
if !ok {
return nil, ErrExpectedMap{}
}
m[q.key], err = insertQuery(m[q.key], insert, qe[1:])
return m, err
case kindIndex:
a, ok := src.([]interface{})
if !ok {
return nil, ErrExpectedArray{}
}
if q.index < 0 || q.index >= len(a) {
return nil, ErrOutOfBounds{}
}
a[q.index], err = insertQuery(a[q.index], insert, qe[1:])
return a, err
default:
return nil, ErrUnknown{Message: "insert query: bad kind"}
}
}