-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestqueryopts.go
358 lines (280 loc) · 7.17 KB
/
requestqueryopts.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package gossamer
import (
"errors"
"log"
"strconv"
"strings"
)
// Checks if a given query option is a valid one
func IsQueryOption(s string) bool {
if strings.HasPrefix(s, "$expand") || strings.HasPrefix(s, "$select") || strings.HasPrefix(s, "$orderby") ||
strings.HasPrefix(s, "$top") || strings.HasPrefix(s, "$skip") || strings.HasPrefix(s, "$count") ||
strings.HasPrefix(s, "$filter") {
return true
}
return false
}
// Returns the QueryOptionType for a given query option string
func DiscoverQueryOptionType(s string) QueryOptionType {
switch {
case strings.HasPrefix(s, string(QUERYOPT_EXPAND)):
return QUERYOPT_EXPAND
case strings.HasPrefix(s, string(QUERYOPT_SELECT)):
return QUERYOPT_SELECT
case strings.HasPrefix(s, string(QUERYOPT_ORDERBY)):
return QUERYOPT_ORDERBY
case strings.HasPrefix(s, string(QUERYOPT_TOP)):
return QUERYOPT_TOP
case strings.HasPrefix(s, string(QUERYOPT_SKIP)):
return QUERYOPT_SKIP
case strings.HasPrefix(s, string(QUERYOPT_COUNT)):
return QUERYOPT_COUNT
case strings.HasPrefix(s, string(QUERYOPT_FILTER)):
return QUERYOPT_FILTER
default:
return QUERYOPT_UNKNOWN
}
}
func CreateQueryOptions(q string) (QueryOptions, error) {
opts := &GossamerQueryOption{}
if q == "" {
return opts, nil
}
if q == "" {
return opts, ERR_QUERYOPTION_BLANK
}
optsSplit := strings.Split(q, "&")
for _, optItem := range optsSplit {
firstEq := strings.Index(optItem, "=")
opt := optItem[0:firstEq]
if IsQueryOption(opt) {
optionType := DiscoverQueryOptionType(opt)
optValue, err := CreateQueryOption(optionType, optItem[firstEq+1:])
if err != nil {
return nil, ERR_QUERYOPTION_INVALID_VALUE
}
opts.Set(optionType, optValue)
} else {
log.Println("ERROR: Not a query option - ", opt)
}
}
return opts, nil
}
func CreateQueryOption(o QueryOptionType, s string) (QueryOption, error) {
switch o {
case QUERYOPT_EXPAND:
return CreateExpandOption(s)
case QUERYOPT_TOP:
return CreateTopOption(s)
case QUERYOPT_COUNT:
return CreateCountOption(s)
case QUERYOPT_FILTER:
return CreateFilterOption(s)
case QUERYOPT_SKIP:
return CreateSkipOption(s)
case QUERYOPT_ORDERBY:
return CreateOrderByOption(s)
case QUERYOPT_SELECT:
return CreateSelectOption(s)
}
return nil, errors.New("Unknown Option types not allowed")
}
// Creates an Expand QueryOption given a string value
func CreateExpandOption(s string) (ExpandOption, error) {
splitValues := strings.Split(s, ",")
return &GossamerExpandOption{
values: splitValues,
}, nil
}
func CreateTopOption(s string) (TopOption, error) {
i, err := strconv.Atoi(s)
if err != nil {
return nil, err
}
return &GossamerTopOption{
value: i,
}, nil
}
func CreateCountOption(s string) (CountOption, error) {
i, err := strconv.ParseBool(s)
if err != nil {
return nil, err
}
return &GossamerCountOption{
value: i,
}, nil
}
func CreateFilterOption(s string) (FilterOption, error) {
return &GossamerFilterOption{}, nil
}
func CreateSkipOption(s string) (SkipOption, error) {
i, err := strconv.Atoi(s)
if err != nil {
return nil, err
}
return &GossamerSkipOption{
value: i,
}, nil
}
func CreateOrderByOption(s string) (OrderByOption, error) {
vals := strings.Split(s, ",")
orderByVals := []OrderByOptionValue{}
for _, v := range vals {
orderByVals = append(orderByVals, GossamerOrderByOptionValue{
s: v,
})
}
return &GossamerOrderByOption{
values: orderByVals,
}, nil
}
func CreateSelectOption(s string) (SelectOption, error) {
splitValues := strings.Split(s, ",")
return &GossamerSelectOption{
values: splitValues,
}, nil
}
type GossamerExpandOption struct {
values []string
}
func (o *GossamerExpandOption) GetType() QueryOptionType {
return QUERYOPT_EXPAND
}
func (o *GossamerExpandOption) GetValue() []string {
return o.values
}
type GossamerSelectOption struct {
values []string
}
func (o *GossamerSelectOption) GetType() QueryOptionType {
return QUERYOPT_SELECT
}
func (o *GossamerSelectOption) GetValue() []string {
return o.values
}
type GossamerOrderByOption struct {
values []OrderByOptionValue
}
func (o *GossamerOrderByOption) GetType() QueryOptionType {
return QUERYOPT_ORDERBY
}
func (o *GossamerOrderByOption) GetValue() []OrderByOptionValue {
return o.values
}
func (o *GossamerOrderByOption) GetSortProperties() []string {
vals := []string{}
for _, v := range o.values {
vals = append(vals, v.GetSortProperty())
}
return vals
}
type GossamerOrderByOptionValue struct {
s string
}
func (o GossamerOrderByOptionValue) GetSortProperty() string {
return o.s
}
type GossamerTopOption struct {
value int
}
func (o *GossamerTopOption) GetValue() int {
return o.value
}
func (o *GossamerTopOption) GetType() QueryOptionType {
return QUERYOPT_TOP
}
type GossamerSkipOption struct {
value int
}
func (o *GossamerSkipOption) GetType() QueryOptionType {
return QUERYOPT_SKIP
}
func (o *GossamerSkipOption) GetValue() int {
return o.value
}
type GossamerCountOption struct {
value bool
}
func (o *GossamerCountOption) GetType() QueryOptionType {
return QUERYOPT_COUNT
}
func (o *GossamerCountOption) GetValue() bool {
return o.value
}
type GossamerFilterOption struct {
}
func (o *GossamerFilterOption) GetType() QueryOptionType {
return QUERYOPT_FILTER
}
type GossamerQueryOption struct {
expandOption QueryOption
selectOption QueryOption
orderByOption QueryOption
topOption QueryOption
skipOption QueryOption
countOption QueryOption
filterOption QueryOption
}
func (o *GossamerQueryOption) Set(optType QueryOptionType, value QueryOption) {
switch optType {
case QUERYOPT_EXPAND:
o.expandOption = value
case QUERYOPT_COUNT:
o.countOption = value
case QUERYOPT_FILTER:
o.filterOption = value
case QUERYOPT_TOP:
o.topOption = value
case QUERYOPT_SKIP:
o.skipOption = value
case QUERYOPT_ORDERBY:
o.orderByOption = value
case QUERYOPT_SELECT:
o.selectOption = value
case QUERYOPT_UNKNOWN:
log.Println("Attempting to set unknown Query Option")
return
}
}
func (o *GossamerQueryOption) ExpandSet() bool {
return o.expandOption != nil
}
func (o *GossamerQueryOption) SelectSet() bool {
return o.selectOption != nil
}
func (o *GossamerQueryOption) OrderBySet() bool {
return o.orderByOption != nil
}
func (o *GossamerQueryOption) TopSet() bool {
return o.topOption != nil
}
func (o *GossamerQueryOption) SkipSet() bool {
return o.skipOption != nil
}
func (o *GossamerQueryOption) CountSet() bool {
return o.countOption != nil
}
func (o *GossamerQueryOption) FilterSet() bool {
return o.filterOption != nil
}
func (o *GossamerQueryOption) GetExpandOption() ExpandOption {
return o.expandOption.(ExpandOption)
}
func (o *GossamerQueryOption) GetSelectOption() SelectOption {
return o.selectOption.(SelectOption)
}
func (o *GossamerQueryOption) GetOrderByOption() OrderByOption {
return o.orderByOption.(OrderByOption)
}
func (o *GossamerQueryOption) GetTopOption() TopOption {
return o.topOption.(TopOption)
}
func (o *GossamerQueryOption) GetSkipOption() SkipOption {
return o.skipOption.(SkipOption)
}
func (o *GossamerQueryOption) GetCountOption() CountOption {
return o.countOption.(CountOption)
}
func (o *GossamerQueryOption) GetFilterOption() FilterOption {
return o.filterOption.(FilterOption)
}