This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
120 lines (102 loc) · 2.69 KB
/
query.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
package query
import (
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/carlosabalde/pgp-tomb/internal/core/query/parser"
)
// Representation of a tree node modeling a boolean query expression.
type Query interface {
Eval(Context) bool
}
// Defines the interface needed by 'Query' in order to be able to evaluate
// query expressions.
type Context interface {
GetIdentifier(string) string
}
// Map is a simple implementation of Context using a map of strings. Mainly
// useful for testing purposes.
type Map map[string]string
func (self Map) GetIdentifier(key string) string {
return self[key]
}
func Parse(query string) (Query, error) {
tree, err := parser.Parse(query)
if err != nil {
return nil, errors.Wrap(err, "failed to parse query")
}
return visit(tree)
}
func visit(tree parser.Tree) (Query, error) {
token := tree.Value()
switch token.Type {
case parser.T_BOOLEAN:
switch token.Value {
case "true":
return True, nil
case "false":
return False, nil
default:
return nil, errors.Errorf("unexpected boolean value '%s'", token.Value)
}
case parser.T_LOGICAL_AND, parser.T_LOGICAL_OR:
left, err := visit(tree.Left())
if err != nil {
return nil, err
}
right, err := visit(tree.Right())
if err != nil {
return nil, err
}
if token.Type == parser.T_LOGICAL_AND {
return And(left, right), nil
}
return Or(left, right), nil
case parser.T_LOGICAL_NOT:
left, err := visit(tree.Left())
if err != nil {
return nil, err
}
return Not(left), nil
case parser.T_IS_EQUAL, parser.T_IS_NOT_EQUAL:
if tree.Left().Value().Type != parser.T_IDENTIFIER ||
tree.Right().Value().Type != parser.T_STRING {
return nil, errors.New("invalid comparison")
}
identifier := tree.Left().Value().Value
str := tree.Right().Value().Value
query := Equal(identifier, str)
if token.Type == parser.T_IS_EQUAL {
return query, nil
}
return Not(query), nil
case parser.T_MATCHES, parser.T_NOT_MATCHES:
if tree.Left().Value().Type != parser.T_IDENTIFIER ||
tree.Right().Value().Type != parser.T_STRING {
return nil, errors.New("invalid comparison")
}
identifier := tree.Left().Value().Value
regexp := tree.Right().Value().Value
query, err := Match(identifier, regexp)
if err != nil {
return nil, err
}
if token.Type == parser.T_MATCHES {
return query, nil
}
return Not(query), nil
default:
return nil, errors.Errorf("unexpected token %s", token.Type)
}
return nil, nil
}
func sprintf(format string, v ...interface{}) string {
return fmt.Sprintf(format, v...)
}
func join(items []Query, sep string) string {
s := make([]string, len(items))
for i, item := range items {
s[i] = sprintf("%s", item)
}
return strings.Join(s, sep)
}