forked from crestonbunch/godata
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfilter_parser.go
67 lines (57 loc) · 1.8 KB
/
filter_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
package godata
import "context"
var GlobalFilterTokenizer *Tokenizer
var GlobalFilterParser *ExpressionParser
// ParseFilterString converts an input string from the $filter part of the URL into a parse
// tree that can be used by providers to create a response.
func ParseFilterString(ctx context.Context, filter string) (*GoDataFilterQuery, error) {
tokens, err := GlobalFilterTokenizer.Tokenize(ctx, filter)
if err != nil {
return nil, err
}
// TODO: can we do this in one fell swoop?
postfix, err := GlobalFilterParser.InfixToPostfix(ctx, tokens)
if err != nil {
return nil, err
}
tree, err := GlobalFilterParser.PostfixToTree(ctx, postfix)
if err != nil {
return nil, err
}
if tree == nil || tree.Token == nil ||
(len(tree.Children) == 0 && tree.Token.Type != ExpressionTokenBoolean) {
return nil, BadRequestError("Value must be a boolean expression")
}
return &GoDataFilterQuery{tree, filter}, nil
}
func SemanticizeFilterQuery(
filter *GoDataFilterQuery,
service *GoDataService,
entity *GoDataEntityType,
) error {
if filter == nil || filter.Tree == nil {
return nil
}
var semanticizeFilterNode func(node *ParseNode) error
semanticizeFilterNode = func(node *ParseNode) error {
if node.Token.Type == ExpressionTokenLiteral {
prop, ok := service.PropertyLookup[entity][node.Token.Value]
if !ok {
return BadRequestError("No property found " + node.Token.Value + " on entity " + entity.Name)
}
node.Token.SemanticType = SemanticTypeProperty
node.Token.SemanticReference = prop
} else {
node.Token.SemanticType = SemanticTypePropertyValue
node.Token.SemanticReference = &node.Token.Value
}
for _, child := range node.Children {
err := semanticizeFilterNode(child)
if err != nil {
return err
}
}
return nil
}
return semanticizeFilterNode(filter.Tree)
}