-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathurl_parser.go
321 lines (285 loc) · 7.83 KB
/
url_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
package godata
import (
"net/url"
"strings"
)
// Parse a request from the HTTP server and format it into a GoDaataRequest type
// to be passed to a provider to produce a result.
func ParseRequest(path string, query url.Values) (*GoDataRequest, error) {
firstSegment, lastSegment, err := ParseUrlPath(path)
if err != nil {
return nil, err
}
parsedQuery, err := ParseUrlQuery(query)
if err != nil {
return nil, err
}
return &GoDataRequest{firstSegment, lastSegment, parsedQuery, RequestKindUnknown}, nil
}
// Compare a request to a given service, and validate the semantics and update
// the request with semantics included
func SemanticizeRequest(req *GoDataRequest, service *GoDataService) error {
// if request kind is a resource
for segment := req.FirstSegment; segment != nil; segment = segment.Next {
err := SemanticizePathSegment(segment, service)
if err != nil {
return err
}
}
switch req.LastSegment.SemanticReference.(type) {
case *GoDataEntitySet:
entitySet := req.LastSegment.SemanticReference.(*GoDataEntitySet)
entityType, err := service.LookupEntityType(entitySet.EntityType)
if err != nil {
return err
}
err = SemanticizeFilterQuery(req.Query.Filter, service, entityType)
if err != nil {
return err
}
err = SemanticizeExpandQuery(req.Query.Expand, service, entityType)
if err != nil {
return err
}
err = SemanticizeSelectQuery(req.Query.Select, service, entityType)
if err != nil {
return err
}
err = SemanticizeOrderByQuery(req.Query.OrderBy, service, entityType)
if err != nil {
return err
}
// TODO: disallow invalid query params
case *GoDataEntityType:
entityType := req.LastSegment.SemanticReference.(*GoDataEntityType)
SemanticizeExpandQuery(req.Query.Expand, service, entityType)
SemanticizeSelectQuery(req.Query.Select, service, entityType)
}
if req.LastSegment.SemanticType == SemanticTypeMetadata {
req.RequestKind = RequestKindMetadata
} else if req.LastSegment.SemanticType == SemanticTypeRef {
req.RequestKind = RequestKindRef
} else if req.LastSegment.SemanticType == SemanticTypeEntitySet {
if req.LastSegment.Identifier == nil {
req.RequestKind = RequestKindCollection
} else {
req.RequestKind = RequestKindEntity
}
} else if req.LastSegment.SemanticType == SemanticTypeCount {
req.RequestKind = RequestKindCount
} else if req.FirstSegment == nil && req.LastSegment == nil {
req.RequestKind = RequestKindService
}
return nil
}
func ParseUrlPath(path string) (*GoDataSegment, *GoDataSegment, error) {
parts := strings.Split(path, "/")
firstSegment := &GoDataSegment{
RawValue: parts[0],
Name: ParseName(parts[0]),
Identifier: ParseIdentifiers(parts[0]),
}
currSegment := firstSegment
for _, v := range parts[1:] {
temp := &GoDataSegment{
RawValue: v,
Name: ParseName(v),
Identifier: ParseIdentifiers(v),
Prev: currSegment,
}
currSegment.Next = temp
currSegment = temp
}
lastSegment := currSegment
return firstSegment, lastSegment, nil
}
func SemanticizePathSegment(segment *GoDataSegment, service *GoDataService) error {
var err error = nil
if segment.RawValue == "$metadata" {
if segment.Next != nil || segment.Prev != nil {
return BadRequestError("A metadata segment must be alone.")
}
segment.SemanticType = SemanticTypeMetadata
segment.SemanticReference = service.Metadata
return nil
}
if segment.RawValue == "$ref" {
// this is a ref segment
if segment.Next != nil {
return BadRequestError("A $ref segment must be last.")
}
if segment.Prev == nil {
return BadRequestError("A $ref segment must be preceded by something.")
}
segment.SemanticType = SemanticTypeRef
segment.SemanticReference = segment.Prev
return nil
}
if segment.RawValue == "$count" {
// this is a ref segment
if segment.Next != nil {
return BadRequestError("A $count segment must be last.")
}
if segment.Prev == nil {
return BadRequestError("A $count segment must be preceded by something.")
}
segment.SemanticType = SemanticTypeCount
segment.SemanticReference = segment.Prev
return nil
}
if _, ok := service.EntitySetLookup[segment.Name]; ok {
// this is an entity set
segment.SemanticType = SemanticTypeEntitySet
segment.SemanticReference, err = service.LookupEntitySet(segment.Name)
if err != nil {
return err
}
if segment.Prev == nil {
// this is the first segment
if segment.Next == nil {
// this is the only segment
return nil
} else {
// there is at least one more segment
if segment.Identifier != nil {
return BadRequestError("An entity set must be the last segment.")
}
// if it has an identifier, it is allowed
return nil
}
} else if segment.Next == nil {
// this is the last segment in a sequence of more than one
return nil
} else {
// this is a middle segment
if segment.Identifier != nil {
return BadRequestError("An entity set must be the last segment.")
}
// if it has an identifier, it is allowed
return nil
}
}
if segment.Prev != nil && segment.Prev.SemanticType == SemanticTypeEntitySet {
// previous segment was an entity set
semanticRef := segment.Prev.SemanticReference.(*GoDataEntitySet)
entity, err := service.LookupEntityType(semanticRef.EntityType)
if err != nil {
return err
}
for _, p := range entity.Properties {
if p.Name == segment.Name {
segment.SemanticType = SemanticTypeProperty
segment.SemanticReference = p
return nil
}
}
return BadRequestError("A valid entity property must follow entity set.")
}
return BadRequestError("Invalid segment " + segment.RawValue)
}
func ParseUrlQuery(query url.Values) (*GoDataQuery, error) {
filter := query.Get("$filter")
apply := query.Get("$apply")
expand := query.Get("$expand")
sel := query.Get("$select")
orderby := query.Get("$orderby")
top := query.Get("$top")
skip := query.Get("$skip")
count := query.Get("$count")
inlinecount := query.Get("$inlinecount")
search := query.Get("$search")
format := query.Get("$format")
result := &GoDataQuery{}
var err error = nil
if filter != "" {
result.Filter, err = ParseFilterString(filter)
}
if err != nil {
return nil, err
}
if apply != "" {
result.Apply, err = ParseApplyString(apply)
}
if err != nil {
return nil, err
}
if expand != "" {
result.Expand, err = ParseExpandString(expand)
}
if err != nil {
return nil, err
}
if sel != "" {
result.Select, err = ParseSelectString(sel)
}
if err != nil {
return nil, err
}
if orderby != "" {
result.OrderBy, err = ParseOrderByString(orderby)
}
if err != nil {
return nil, err
}
if top != "" {
result.Top, err = ParseTopString(top)
}
if err != nil {
return nil, err
}
if skip != "" {
result.Skip, err = ParseSkipString(skip)
}
if err != nil {
return nil, err
}
if count != "" {
result.Count, err = ParseCountString(count)
}
if err != nil {
return nil, err
}
if inlinecount != "" {
result.InlineCount, err = ParseInlineCountString(inlinecount)
}
if err != nil {
return nil, err
}
if search != "" {
result.Search, err = ParseSearchString(search)
}
if err != nil {
return nil, err
}
if format != "" {
err = NotImplementedError("Format is not supported")
}
if err != nil {
return nil, err
}
return result, err
}
func ParseIdentifiers(segment string) *GoDataIdentifier {
if !(strings.Contains(segment, "(") && strings.Contains(segment, ")")) {
return nil
}
rawIds := segment[strings.LastIndex(segment, "(")+1 : strings.LastIndex(segment, ")")]
parts := strings.Split(rawIds, ",")
result := make(GoDataIdentifier)
for _, v := range parts {
if strings.Contains(v, "=") {
split := strings.SplitN(v, "=", 2)
result[split[0]] = split[1]
} else {
result[v] = ""
}
}
return &result
}
func ParseName(segment string) string {
if strings.Contains(segment, "(") {
return segment[:strings.LastIndex(segment, "(")]
} else {
return segment
}
}