-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryParser.go
280 lines (246 loc) · 5.45 KB
/
queryParser.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
package hypeql
import (
"fmt"
"strconv"
"strings"
)
// Converts the request body content to an interfaces slice to process it in "Process" function
func (a queryParser) Parse(body string) ([]interface{}, error) {
return a.parseRecursion(body, 1)
}
func (a queryParser) parseRecursion(body string, deep uint64) ([]interface{}, error) {
body = strings.Trim(strings.ReplaceAll(body, "\t", ""), " ")
res := []interface{}{}
varOpened := false
keyWrite := true
vars := map[string]interface{}{}
key := ""
quoteOpened := false
valueSlash := false
comment := false
value := ""
varIsString := false
k := ""
for _, c := range body {
if c != '\\' && c != '"' && c != 'n' && varOpened && !keyWrite && valueSlash {
valueSlash = false
}
if comment && c != '\n' {
continue
}
switch c {
case '{':
if cleanUp(k) != "" {
k += string(c)
}
case '\n':
if varOpened {
return []interface{}{}, fmt.Errorf("argument declaration have interputted by \\n symbol")
}
cu := cleanUp(k)
if cu != "" {
if strings.Contains(cu, "{") {
k += string(c)
} else {
res = append(res, cu)
k = ""
}
}
comment = false
case '#':
if !varOpened {
comment = true
}
case '(':
if varOpened {
if !keyWrite {
value += string(c)
}
} else {
if !strings.Contains(k, "{") {
varIsString = false
varOpened = true
key = ""
value = ""
vars = map[string]interface{}{}
keyWrite = true
quoteOpened = false
}
}
case ':':
if varOpened {
if keyWrite {
keyWrite = false
} else {
value += string(c)
}
}
case ' ':
if varOpened {
if !keyWrite {
value += string(c)
}
} else {
cu := cleanUp(k)
if cu != "" {
if strings.Contains(cu, "{") {
k += string(c)
}
}
}
case '\\':
if varOpened {
if !keyWrite {
if valueSlash {
value += string(c)
valueSlash = false
} else {
valueSlash = true
}
}
}
case '"':
if varOpened {
if !keyWrite {
if valueSlash {
value += string(c)
valueSlash = false
} else if quoteOpened {
quoteOpened = false
} else {
varIsString = true
quoteOpened = true
}
}
}
case ',':
if varOpened {
if !keyWrite {
if !quoteOpened {
if cleanUp(key) != "" {
if len(value) > 0 && value[0] == ' ' {
value = value[1:]
}
vars[key] = func(a string) any {
if varIsString {
return a
}
if i, err := strconv.Atoi(a); err == nil {
return i
}
return a
}(value)
varIsString = false
key = ""
value = ""
keyWrite = true
}
} else {
value += string(c)
}
}
} else {
cu := cleanUp(k)
if cu != "" {
if !strings.Contains(cu, "{") {
res = append(res, cu)
k = ""
} else {
k += string(c)
}
}
}
case ')':
if varOpened {
if keyWrite {
varOpened = false
} else {
if !quoteOpened {
if cleanUp(key) != "" {
if len(value) > 0 && value[0] == ' ' {
value = value[1:]
}
// Two aviables types in the args: int, string
vars[key] = func(a string) any {
if varIsString {
return a
}
if i, err := strconv.Atoi(a); err == nil {
return i
}
return a
}(value)
varOpened = false
}
} else {
value += string(c)
}
}
}
case '}':
if varOpened {
if !keyWrite {
value += string(c)
}
} else {
cu := cleanUp(k)
if cu != "" {
if !strings.Contains(cu, "{") {
res = append(res, cu)
k = ""
} else if strings.Count(cu, "{") == strings.Count(cu, "}")+1 {
if a.Config.MaxDeepRecursion != 0 && deep+1 > a.Config.MaxDeepRecursion {
return []interface{}{}, fmt.Errorf("max deep recursion reached")
}
k += string(c)
i := strings.Index(k, "{")
name := cleanUp(k[:i])
block := k[i:]
u, err := a.parseRecursion(block, deep+1)
if err != nil {
return []interface{}{}, err
}
a := []interface{}{
name,
u,
}
if len(vars) != 0 {
a = append(a, vars)
}
res = append(res, a)
k = ""
vars = map[string]interface{}{}
} else {
k += string(c)
}
}
}
default:
if varOpened {
if keyWrite {
key += string(c)
} else {
if c == 'n' && valueSlash {
value += "\n"
valueSlash = false
} else {
value += string(c)
}
}
} else {
k += string(c)
}
}
}
if strings.Contains(k, "{") {
return []interface{}{}, fmt.Errorf("the curly bracket is not closed")
} else if varOpened {
return []interface{}{}, fmt.Errorf("the arguments in parentheses were not written")
}
return res, nil
}
// Cleans whitespace symbols.
// Use it for the body parsing, but DON'T use it to clean the full code to process it because space symbols also removing from an argument brackets value
func cleanUp(a string) string {
return strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(a, " ", ""), "\t", ""), "\n", "")
}