-
Notifications
You must be signed in to change notification settings - Fork 38
/
cql.go
254 lines (223 loc) · 6.46 KB
/
cql.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
//nolint:govet // there is too much issues with incompatible struct tags
package cql
import (
"fmt"
"strings"
"github.com/alecthomas/participle/v2"
"github.com/rotisserie/eris"
"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/types"
)
const (
opAnd cqlOperator = iota
opOr
)
var (
operatorMap = map[string]cqlOperator{"&": opAnd, "|": opOr}
internalCQLParser = participle.MustBuild[cqlTerm]()
)
type componentByName func(string) (types.Component, error)
type cqlAll struct{}
type cqlExact struct {
Components []*cqlComponent `"EXACT""(" (@@",")* @@ ")"`
}
type cqlContains struct {
Components []*cqlComponent `"CONTAINS" "(" (@@",")* @@ ")"`
}
type cqlNot struct {
SubExpression *cqlValue `"!" @@`
}
type cqlTerm struct {
Left *cqlFactor `@@`
Right []*cqlOpFactor `@@*`
}
type cqlValue struct {
All *cqlAll `@("ALL" "(" ")")`
Exact *cqlExact `| @@`
Contains *cqlContains `| @@`
Not *cqlNot `| @@`
Subexpression *cqlTerm `| "(" @@ ")"`
}
type cqlFactor struct {
Base *cqlValue `@@`
}
type cqlOpFactor struct {
Operator cqlOperator `@("&" | "|")`
Factor *cqlFactor `@@`
}
type cqlComponent struct {
Name string `@Ident`
}
type cqlOperator int
// Capture basically tells the parser library how to transform a string token that's parsed into the operator type.
func (o *cqlOperator) Capture(s []string) error {
if len(s) == 0 {
return eris.New("invalid operator")
}
operator, ok := operatorMap[s[0]]
if !ok {
return eris.New("invalid operator")
}
*o = operator
return nil
}
// Display
func (o cqlOperator) String() string {
switch o {
case opAnd:
return "&"
case opOr:
return "|"
}
panic("unsupported operator")
}
func (a *cqlAll) Capture(values []string) error {
if values[0] == "ALL" && values[1] == "(" && values[2] == ")" {
*a = cqlAll{}
}
return nil
}
func (a *cqlAll) String() string {
return "ALL()"
}
func (e *cqlExact) String() string {
parameters := ""
for i, comp := range e.Components {
parameters += comp.Name
if i < len(e.Components)-1 {
parameters += ", "
}
}
return "EXACT(" + parameters + ")"
}
func (e *cqlContains) String() string {
parameters := ""
for i, comp := range e.Components {
parameters += comp.Name
if i < len(e.Components)-1 {
parameters += ", "
}
}
return "CONTAINS(" + parameters + ")"
}
func (v *cqlValue) String() string {
switch {
case v.Exact != nil:
return v.Exact.String()
case v.Contains != nil:
return v.Contains.String()
case v.All != nil:
return v.All.String()
case v.Not != nil:
return "!(" + v.Not.SubExpression.String() + ")"
case v.Subexpression != nil:
return "(" + v.Subexpression.String() + ")"
}
panic("logic error displaying CQL ast. Check the code in cql.go")
}
func (f *cqlFactor) String() string {
out := f.Base.String()
return out
}
func (o *cqlOpFactor) String() string {
return fmt.Sprintf("%s %s", o.Operator, o.Factor)
}
func (t *cqlTerm) String() string {
out := []string{t.Left.String()}
for _, r := range t.Right {
out = append(out, r.String())
}
return strings.Join(out, " ")
}
// TODO: Msg is sum type is represented as a product type. There is a case where multiple properties are filled out.
// Only one property may not be nil, The parser should prevent this from happening but for safety this should eventually
// be checked.
func valueToComponentFilter(value *cqlValue, stringToComponent componentByName) (filter.ComponentFilter, error) {
if value.Not != nil { //nolint:gocritic,nestif // its fine.
resultFilter, err := valueToComponentFilter(value.Not.SubExpression, stringToComponent)
if err != nil {
return nil, err
}
return filter.Not(resultFilter), nil
} else if value.Exact != nil {
if len(value.Exact.Components) == 0 {
return nil, eris.New("EXACT cannot have zero parameters")
}
components := make([]filter.ComponentWrapper, 0, len(value.Exact.Components))
for _, componentName := range value.Exact.Components {
comp, err := stringToComponent(componentName.Name)
if err != nil {
return nil, eris.Wrap(err, "")
}
components = append(components, filter.ComponentWrapper{Component: comp})
}
return filter.Exact(components...), nil
} else if value.All != nil {
return filter.All(), nil
} else if value.Contains != nil {
if len(value.Contains.Components) == 0 {
return nil, eris.New("CONTAINS cannot have zero parameters")
}
components := make([]filter.ComponentWrapper, 0, len(value.Contains.Components))
for _, componentName := range value.Contains.Components {
comp, err := stringToComponent(componentName.Name)
if err != nil {
return nil, eris.Wrap(err, "")
}
components = append(components, filter.ComponentWrapper{Component: comp})
}
return filter.Contains(components...), nil
} else if value.Subexpression != nil {
return termToComponentFilter(value.Subexpression, stringToComponent)
}
return nil, eris.New("unknown error during conversion from CQL AST to ComponentFilter")
}
func factorToComponentFilter(factor *cqlFactor, stringToComponent componentByName) (
filter.ComponentFilter, error,
) {
return valueToComponentFilter(factor.Base, stringToComponent)
}
func opFactorToComponentFilter(opFactor *cqlOpFactor, stringToComponent componentByName) (
*cqlOperator, filter.ComponentFilter, error,
) {
resultFilter, err := factorToComponentFilter(opFactor.Factor, stringToComponent)
if err != nil {
return nil, nil, err
}
return &opFactor.Operator, resultFilter, nil
}
func termToComponentFilter(term *cqlTerm, stringToComponent componentByName) (filter.ComponentFilter, error) {
if term.Left == nil {
return nil, eris.New("not enough values in expression")
}
acc, err := factorToComponentFilter(term.Left, stringToComponent)
if err != nil {
return nil, err
}
for _, opFactor := range term.Right {
operator, resultFilter, err := opFactorToComponentFilter(opFactor, stringToComponent)
if err != nil {
return nil, err
}
switch *operator {
case opAnd:
acc = filter.And(acc, resultFilter)
case opOr:
acc = filter.Or(acc, resultFilter)
default:
return nil, eris.New("invalid operator")
}
}
return acc, nil
}
func Parse(cqlText string, stringToComponent componentByName) (filter.ComponentFilter, error) {
term, err := internalCQLParser.ParseString("", cqlText)
if err != nil {
return nil, eris.Wrap(err, "failed to parse CQL string")
}
resultFilter, err := termToComponentFilter(term, stringToComponent)
if err != nil {
return nil, err
}
return resultFilter, nil
}