Skip to content

Commit f32e8f0

Browse files
add context argument (#34)
* add context argument * add context argument * add context and lenient mode flag * fix typos
1 parent 96ebfbb commit f32e8f0

21 files changed

+466
-261
lines changed

apply_parser.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package godata
22

3-
func ParseApplyString(apply string) (*GoDataApplyQuery, error) {
3+
import "context"
4+
5+
func ParseApplyString(ctx context.Context, apply string) (*GoDataApplyQuery, error) {
46
result := GoDataApplyQuery(apply)
57
return &result, nil
68
}

count_parser.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package godata
22

33
import (
4+
"context"
45
"strconv"
56
)
67

7-
func ParseCountString(count string) (*GoDataCountQuery, error) {
8+
func ParseCountString(ctx context.Context, count string) (*GoDataCountQuery, error) {
89
i, err := strconv.ParseBool(count)
910
if err != nil {
1011
return nil, err

expand_parser.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package godata
22

33
import (
4+
"context"
45
"strconv"
56
)
67

@@ -50,8 +51,8 @@ func ExpandTokenizer() *Tokenizer {
5051
return &t
5152
}
5253

53-
func ParseExpandString(expand string) (*GoDataExpandQuery, error) {
54-
tokens, err := GlobalExpandTokenizer.Tokenize(expand)
54+
func ParseExpandString(ctx context.Context, expand string) (*GoDataExpandQuery, error) {
55+
tokens, err := GlobalExpandTokenizer.Tokenize(ctx, expand)
5556

5657
if err != nil {
5758
return nil, err
@@ -74,7 +75,7 @@ func ParseExpandString(expand string) (*GoDataExpandQuery, error) {
7475
} else if token.Value == "," {
7576
if stack.Empty() {
7677
// no paren on the stack, parse this item and start a new queue
77-
item, err := ParseExpandItem(queue)
78+
item, err := ParseExpandItem(ctx, queue)
7879
if err != nil {
7980
return nil, err
8081
}
@@ -93,7 +94,7 @@ func ParseExpandString(expand string) (*GoDataExpandQuery, error) {
9394
return nil, BadRequestError("Mismatched parentheses in expand clause.")
9495
}
9596

96-
item, err := ParseExpandItem(queue)
97+
item, err := ParseExpandItem(ctx, queue)
9798
if err != nil {
9899
return nil, err
99100
}
@@ -102,7 +103,7 @@ func ParseExpandString(expand string) (*GoDataExpandQuery, error) {
102103
return &GoDataExpandQuery{ExpandItems: items}, nil
103104
}
104105

105-
func ParseExpandItem(input tokenQueue) (*ExpandItem, error) {
106+
func ParseExpandItem(ctx context.Context, input tokenQueue) (*ExpandItem, error) {
106107

107108
item := &ExpandItem{}
108109
item.Path = []*Token{}
@@ -128,7 +129,7 @@ func ParseExpandItem(input tokenQueue) (*ExpandItem, error) {
128129
queue.Enqueue(token)
129130
} else {
130131
// top level slash means we're done parsing the options
131-
err := ParseExpandOption(queue, item)
132+
err := ParseExpandOption(ctx, queue, item)
132133
if err != nil {
133134
return nil, err
134135
}
@@ -140,7 +141,7 @@ func ParseExpandItem(input tokenQueue) (*ExpandItem, error) {
140141
item.Path = append(item.Path, queue.Dequeue())
141142
} else if token.Value == ";" && stack.Size == 1 {
142143
// semicolons only split expand options at the first level
143-
err := ParseExpandOption(queue, item)
144+
err := ParseExpandOption(ctx, queue, item)
144145
if err != nil {
145146
return nil, err
146147
}
@@ -162,7 +163,7 @@ func ParseExpandItem(input tokenQueue) (*ExpandItem, error) {
162163
return item, nil
163164
}
164165

165-
func ParseExpandOption(queue *tokenQueue, item *ExpandItem) error {
166+
func ParseExpandOption(ctx context.Context, queue *tokenQueue, item *ExpandItem) error {
166167
head := queue.Dequeue().Value
167168
if queue.Head == nil {
168169
return BadRequestError("Invalid expand clause.")
@@ -171,7 +172,7 @@ func ParseExpandOption(queue *tokenQueue, item *ExpandItem) error {
171172
body := queue.GetValue()
172173

173174
if head == "$filter" {
174-
filter, err := ParseFilterString(body)
175+
filter, err := ParseFilterString(ctx, body)
175176
if err == nil {
176177
item.Filter = filter
177178
} else {
@@ -180,7 +181,7 @@ func ParseExpandOption(queue *tokenQueue, item *ExpandItem) error {
180181
}
181182

182183
if head == "at" {
183-
at, err := ParseFilterString(body)
184+
at, err := ParseFilterString(ctx, body)
184185
if err == nil {
185186
item.At = at
186187
} else {
@@ -189,7 +190,7 @@ func ParseExpandOption(queue *tokenQueue, item *ExpandItem) error {
189190
}
190191

191192
if head == "$search" {
192-
search, err := ParseSearchString(body)
193+
search, err := ParseSearchString(ctx, body)
193194
if err == nil {
194195
item.Search = search
195196
} else {
@@ -198,7 +199,7 @@ func ParseExpandOption(queue *tokenQueue, item *ExpandItem) error {
198199
}
199200

200201
if head == "$orderby" {
201-
orderby, err := ParseOrderByString(body)
202+
orderby, err := ParseOrderByString(ctx, body)
202203
if err == nil {
203204
item.OrderBy = orderby
204205
} else {
@@ -207,7 +208,7 @@ func ParseExpandOption(queue *tokenQueue, item *ExpandItem) error {
207208
}
208209

209210
if head == "$skip" {
210-
skip, err := ParseSkipString(body)
211+
skip, err := ParseSkipString(ctx, body)
211212
if err == nil {
212213
item.Skip = skip
213214
} else {
@@ -216,7 +217,7 @@ func ParseExpandOption(queue *tokenQueue, item *ExpandItem) error {
216217
}
217218

218219
if head == "$top" {
219-
top, err := ParseTopString(body)
220+
top, err := ParseTopString(ctx, body)
220221
if err == nil {
221222
item.Top = top
222223
} else {
@@ -225,7 +226,7 @@ func ParseExpandOption(queue *tokenQueue, item *ExpandItem) error {
225226
}
226227

227228
if head == "$select" {
228-
sel, err := ParseSelectString(body)
229+
sel, err := ParseSelectString(ctx, body)
229230
if err == nil {
230231
item.Select = sel
231232
} else {
@@ -234,7 +235,7 @@ func ParseExpandOption(queue *tokenQueue, item *ExpandItem) error {
234235
}
235236

236237
if head == "$expand" {
237-
expand, err := ParseExpandString(body)
238+
expand, err := ParseExpandString(ctx, body)
238239
if err == nil {
239240
item.Expand = expand
240241
} else {

expand_parser_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package godata
22

33
import (
4+
"context"
45
"testing"
56
)
67

78
func TestTrivialExpand(t *testing.T) {
89
input := "Products/Categories"
9-
10-
output, err := ParseExpandString(input)
10+
ctx := context.Background()
11+
output, err := ParseExpandString(ctx, input)
1112

1213
if err != nil {
1314
t.Error(err)
@@ -26,8 +27,8 @@ func TestTrivialExpand(t *testing.T) {
2627

2728
func TestSimpleExpand(t *testing.T) {
2829
input := "Products($filter=DiscontinuedDate eq null)"
29-
30-
output, err := ParseExpandString(input)
30+
ctx := context.Background()
31+
output, err := ParseExpandString(ctx, input)
3132

3233
if err != nil {
3334
t.Error(err)
@@ -57,8 +58,8 @@ func TestSimpleExpand(t *testing.T) {
5758

5859
func TestExpandNestedCommas(t *testing.T) {
5960
input := "DirectReports($select=FirstName,LastName;$levels=4)"
60-
61-
output, err := ParseExpandString(input)
61+
ctx := context.Background()
62+
output, err := ParseExpandString(ctx, input)
6263

6364
if err != nil {
6465
t.Error(err)
@@ -91,8 +92,8 @@ func TestExpandNestedCommas(t *testing.T) {
9192

9293
func TestExpandNestedParens(t *testing.T) {
9394
input := "Products($filter=not (DiscontinuedDate eq null))"
94-
95-
output, err := ParseExpandString(input)
95+
ctx := context.Background()
96+
output, err := ParseExpandString(ctx, input)
9697

9798
if err != nil {
9899
t.Error(err)

expression_parser.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package godata
22

33
import (
4+
"context"
45
"strings"
56
)
67

@@ -103,18 +104,17 @@ type ExpressionParser struct {
103104
// ParseExpressionString converts a ODATA expression input string into a parse
104105
// tree that can be used by providers to create a response.
105106
// Expressions can be used within $filter and $orderby query options.
106-
func (p *ExpressionParser) ParseExpressionString(expression string) (*GoDataExpression, error) {
107-
108-
tokens, err := p.tokenizer.Tokenize(expression)
107+
func (p *ExpressionParser) ParseExpressionString(ctx context.Context, expression string) (*GoDataExpression, error) {
108+
tokens, err := p.tokenizer.Tokenize(ctx, expression)
109109
if err != nil {
110110
return nil, err
111111
}
112112
// TODO: can we do this in one fell swoop?
113-
postfix, err := p.InfixToPostfix(tokens)
113+
postfix, err := p.InfixToPostfix(ctx, tokens)
114114
if err != nil {
115115
return nil, err
116116
}
117-
tree, err := p.PostfixToTree(postfix)
117+
tree, err := p.PostfixToTree(ctx, postfix)
118118
if err != nil {
119119
return nil, err
120120
}

0 commit comments

Comments
 (0)