-
Notifications
You must be signed in to change notification settings - Fork 36
/
expand_parser.go
335 lines (290 loc) · 7.31 KB
/
expand_parser.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
package godata
import (
"strconv"
)
const (
ExpandTokenOpenParen = iota
ExpandTokenCloseParen
ExpandTokenNav
ExpandTokenComma
ExpandTokenSemicolon
ExpandTokenEquals
ExpandTokenLiteral
)
var GlobalExpandTokenizer = ExpandTokenizer()
// Represents an item to expand in an OData query. Tracks the path of the entity
// to expand and also the filter, levels, and reference options, etc.
type ExpandItem struct {
Path []*Token
Filter *GoDataFilterQuery
Search *GoDataSearchQuery
OrderBy *GoDataOrderByQuery
Skip *GoDataSkipQuery
Top *GoDataTopQuery
Select *GoDataSelectQuery
Expand *GoDataExpandQuery
Levels int
}
func ExpandTokenizer() *Tokenizer {
t := Tokenizer{}
t.Add("^\\(", ExpandTokenOpenParen)
t.Add("^\\)", ExpandTokenCloseParen)
t.Add("^/", ExpandTokenNav)
t.Add("^,", ExpandTokenComma)
t.Add("^;", ExpandTokenSemicolon)
t.Add("^=", ExpandTokenEquals)
t.Add("^[a-zA-Z0-9_\\'\\.:\\$ \\*]+", ExpandTokenLiteral)
return &t
}
func ParseExpandString(expand string) (*GoDataExpandQuery, error) {
tokens, err := GlobalExpandTokenizer.Tokenize(expand)
if err != nil {
return nil, err
}
stack := tokenStack{}
queue := tokenQueue{}
items := make([]*ExpandItem, 0)
for len(tokens) > 0 {
token := tokens[0]
tokens = tokens[1:]
if token.Value == "(" {
queue.Enqueue(token)
stack.Push(token)
} else if token.Value == ")" {
queue.Enqueue(token)
stack.Pop()
} else if token.Value == "," {
if stack.Empty() {
// no paren on the stack, parse this item and start a new queue
item, err := ParseExpandItem(queue)
if err != nil {
return nil, err
}
items = append(items, item)
queue = tokenQueue{}
} else {
// this comma is inside a nested expression, keep it in the queue
queue.Enqueue(token)
}
} else {
queue.Enqueue(token)
}
}
if !stack.Empty() {
return nil, BadRequestError("Mismatched parentheses in expand clause.")
}
item, err := ParseExpandItem(queue)
if err != nil {
return nil, err
}
items = append(items, item)
return &GoDataExpandQuery{ExpandItems: items}, nil
}
func ParseExpandItem(input tokenQueue) (*ExpandItem, error) {
item := &ExpandItem{}
item.Path = []*Token{}
stack := &tokenStack{}
queue := &tokenQueue{}
for !input.Empty() {
token := input.Dequeue()
if token.Value == "(" {
if !stack.Empty() {
// this is a nested slash, it belongs on the queue
queue.Enqueue(token)
} else {
// top level slash means we're done parsing the path
item.Path = append(item.Path, queue.Dequeue())
}
stack.Push(token)
} else if token.Value == ")" {
stack.Pop()
if !stack.Empty() {
// this is a nested slash, it belongs on the queue
queue.Enqueue(token)
} else {
// top level slash means we're done parsing the options
err := ParseExpandOption(queue, item)
if err != nil {
return nil, err
}
// reset the queue
queue = &tokenQueue{}
}
} else if token.Value == "/" && stack.Empty() {
// at root level, slashes separate path segments
item.Path = append(item.Path, queue.Dequeue())
} else if token.Value == ";" && stack.Size == 1 {
// semicolons only split expand options at the first level
err := ParseExpandOption(queue, item)
if err != nil {
return nil, err
}
// reset the queue
queue = &tokenQueue{}
} else {
queue.Enqueue(token)
}
}
if !stack.Empty() {
return nil, BadRequestError("Mismatched parentheses in expand clause.")
}
if !queue.Empty() {
item.Path = append(item.Path, queue.Dequeue())
}
return item, nil
}
func ParseExpandOption(queue *tokenQueue, item *ExpandItem) error {
head := queue.Dequeue().Value
if queue.Head == nil {
return BadRequestError("Invalid expand clause.")
}
queue.Dequeue() // drop the '=' from the front of the queue
body := queue.String()
if head == "$filter" {
filter, err := ParseFilterString(body)
if err == nil {
item.Filter = filter
} else {
return err
}
}
if head == "$search" {
search, err := ParseSearchString(body)
if err == nil {
item.Search = search
} else {
return err
}
}
if head == "$orderby" {
orderby, err := ParseOrderByString(body)
if err == nil {
item.OrderBy = orderby
} else {
return err
}
}
if head == "$skip" {
skip, err := ParseSkipString(body)
if err == nil {
item.Skip = skip
} else {
return err
}
}
if head == "$top" {
top, err := ParseTopString(body)
if err == nil {
item.Top = top
} else {
return err
}
}
if head == "$select" {
sel, err := ParseSelectString(body)
if err == nil {
item.Select = sel
} else {
return err
}
}
if head == "$expand" {
expand, err := ParseExpandString(body)
if err == nil {
item.Expand = expand
} else {
return err
}
}
if head == "$levels" {
i, err := strconv.Atoi(body)
if err != nil {
return err
}
item.Levels = i
}
return nil
}
func SemanticizeExpandQuery(
expand *GoDataExpandQuery,
service *GoDataService,
entity *GoDataEntityType,
) error {
if expand == nil {
return nil
}
// Replace $levels with a nested expand clause
for _, item := range expand.ExpandItems {
if item.Levels > 0 {
if item.Expand == nil {
item.Expand = &GoDataExpandQuery{[]*ExpandItem{}}
}
// Future recursive calls to SemanticizeExpandQuery() will build out
// this expand tree completely
item.Expand.ExpandItems = append(
item.Expand.ExpandItems,
&ExpandItem{
Path: item.Path,
Levels: item.Levels - 1,
},
)
item.Levels = 0
}
}
// we're gonna rebuild the items list, replacing wildcards where possible
// TODO: can we save the garbage collector some heartache?
newItems := []*ExpandItem{}
for _, item := range expand.ExpandItems {
if item.Path[0].Value == "*" {
// replace wildcard with a copy of every navigation property
for _, navProp := range service.NavigationPropertyLookup[entity] {
path := []*Token{&Token{Value: navProp.Name, Type: ExpandTokenLiteral}}
newItem := &ExpandItem{
Path: append(path, item.Path[1:]...),
Levels: item.Levels,
Expand: item.Expand,
}
newItems = append(newItems, newItem)
}
// TODO: check for duplicates?
} else {
newItems = append(newItems, item)
}
}
expand.ExpandItems = newItems
for _, item := range expand.ExpandItems {
err := semanticizeExpandItem(item, service, entity)
if err != nil {
return err
}
}
return nil
}
func semanticizeExpandItem(
item *ExpandItem,
service *GoDataService,
entity *GoDataEntityType,
) error {
// TODO: allow multiple path segments in expand clause
// TODO: handle $ref
if len(item.Path) > 1 {
return NotImplementedError("Multiple path segments not currently supported in expand clauses.")
}
navProps := service.NavigationPropertyLookup[entity]
target := item.Path[len(item.Path)-1]
if prop, ok := navProps[target.Value]; ok {
target.SemanticType = SemanticTypeEntity
entityType, err := service.LookupEntityType(prop.Type)
if err != nil {
return err
}
target.SemanticReference = entityType
SemanticizeFilterQuery(item.Filter, service, entityType)
SemanticizeExpandQuery(item.Expand, service, entityType)
SemanticizeSelectQuery(item.Select, service, entityType)
SemanticizeOrderByQuery(item.OrderBy, service, entityType)
} else {
return BadRequestError("Entity type " + entity.Name + " has no navigational property " + target.Value)
}
return nil
}