Skip to content

Commit 8f8936d

Browse files
committed
Make logical operator names CamelCase
1 parent f46110a commit 8f8936d

File tree

4 files changed

+74
-72
lines changed

4 files changed

+74
-72
lines changed

internal/filter/parser.go

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/filter/parser.y

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ package filter
88
// Otherwise, it will pop the last pushed rule of that chain (second argument) and append it to the new *And chain.
99
//
1010
// Example: `foo=bar|bar~foo&col!~val`
11-
// The second argument `rule` is supposed to be a filter.ANY *Chain contains the first two conditions.
11+
// The second argument `rule` is supposed to be a filter.Any *Chain contains the first two conditions.
1212
// We then call this function when the parser is processing the logical `&` op and the Unlike condition,
1313
// and what this function will do is logically re-group the conditions into `foo=bar|(bar~foo&col!~val)`.
1414
func reduceFilter(op string, rule Filter, rules ...Filter) Filter {
1515
chain, ok := rule.(*Chain);
16-
if ok && chain.op == ANY && LogicalOp(op) == ALL {
16+
if ok && chain.op == Any && LogicalOp(op) == All {
1717
// Retrieve the last pushed condition and append it to the new "And" chain instead
18-
andChain, _ := NewChain(ALL, chain.pop())
18+
andChain, _ := NewChain(All, chain.pop())
1919
andChain.add(rules...)
2020

2121
chain.add(andChain)
@@ -25,7 +25,7 @@ func reduceFilter(op string, rule Filter, rules ...Filter) Filter {
2525

2626
// If the given operator is the same as the already existsing chains operator (*chain),
2727
// we don't need to create another chain of the same operator type. Avoids something
28-
// like &Chain{op: ALL, &Chain{op: ALL, ...}}
28+
// like &Chain{op: All, &Chain{op: All, ...}}
2929
if chain == nil || chain.op != LogicalOp(op) {
3030
newChain, err := NewChain(LogicalOp(op), rule)
3131
if err != nil {
@@ -123,12 +123,13 @@ maybe_negated_condition_expr: optional_negation condition_expr
123123
{
124124
if $1 != "" {
125125
// NewChain is only going to return an error if an invalid operator is specified, and since
126-
// we explicitly provide the NONE operator, we don't expect an error to be returned.
127-
$$, _ = NewChain(NONE, $2)
126+
// we explicitly provide the None operator, we don't expect an error to be returned.
127+
$$, _ = NewChain(None, $2)
128128
} else {
129129
$$ = $2
130130
}
131131
}
132+
;
132133

133134
condition_expr: "(" filter_chain ")"
134135
{

internal/filter/parser_test.go

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,22 @@ func TestParser(t *testing.T) {
149149

150150
var expected Filter
151151
rule, err := Parse("!foo=bar")
152-
expected = &Chain{op: NONE, rules: []Filter{&Condition{op: Equal, column: "foo", value: "bar"}}}
152+
expected = &Chain{op: None, rules: []Filter{&Condition{op: Equal, column: "foo", value: "bar"}}}
153+
assert.Nil(t, err, "There should be no errors but got: %s", err)
153154
assert.Equal(t, expected, rule)
154155

155156
rule, err = Parse("foo=bar&bar=foo")
156157
assert.Nil(t, err, "There should be no errors but got: %s", err)
157-
expected = &Chain{op: ALL, rules: []Filter{
158+
expected = &Chain{op: All, rules: []Filter{
158159
&Condition{op: Equal, column: "foo", value: "bar"},
159160
&Condition{op: Equal, column: "bar", value: "foo"},
160161
}}
161162
assert.Equal(t, expected, rule)
162163

163164
rule, err = Parse("foo=bar&bar=foo|col=val")
164165
assert.Nil(t, err, "There should be no errors but got: %s", err)
165-
expected = &Chain{op: ANY, rules: []Filter{
166-
&Chain{op: ALL, rules: []Filter{
166+
expected = &Chain{op: Any, rules: []Filter{
167+
&Chain{op: All, rules: []Filter{
167168
&Condition{op: Equal, column: "foo", value: "bar"},
168169
&Condition{op: Equal, column: "bar", value: "foo"},
169170
}},
@@ -173,7 +174,7 @@ func TestParser(t *testing.T) {
173174

174175
rule, err = Parse("foo=bar|bar=foo")
175176
assert.Nil(t, err, "There should be no errors but got: %s", err)
176-
expected = &Chain{op: ANY, rules: []Filter{
177+
expected = &Chain{op: Any, rules: []Filter{
177178
&Condition{op: Equal, column: "foo", value: "bar"},
178179
&Condition{op: Equal, column: "bar", value: "foo"},
179180
}}
@@ -186,27 +187,27 @@ func TestParser(t *testing.T) {
186187

187188
rule, err = Parse("(!foo=bar)")
188189
assert.Nil(t, err, "There should be no errors but got: %s", err)
189-
expected = &Chain{op: NONE, rules: []Filter{&Condition{op: Equal, column: "foo", value: "bar"}}}
190+
expected = &Chain{op: None, rules: []Filter{&Condition{op: Equal, column: "foo", value: "bar"}}}
190191
assert.Equal(t, expected, rule)
191192

192193
rule, err = Parse("!(foo=bar)")
193194
assert.Nil(t, err, "There should be no errors but got: %s", err)
194-
expected = &Chain{op: NONE, rules: []Filter{&Condition{op: Equal, column: "foo", value: "bar"}}}
195+
expected = &Chain{op: None, rules: []Filter{&Condition{op: Equal, column: "foo", value: "bar"}}}
195196
assert.Equal(t, expected, rule)
196197

197198
rule, err = Parse("!(!foo=bar)")
198199
assert.Nil(t, err, "There should be no errors but got: %s", err)
199-
expected = &Chain{op: NONE, rules: []Filter{
200-
&Chain{op: NONE, rules: []Filter{
200+
expected = &Chain{op: None, rules: []Filter{
201+
&Chain{op: None, rules: []Filter{
201202
&Condition{op: Equal, column: "foo", value: "bar"},
202203
}},
203204
}}
204205
assert.Equal(t, expected, rule)
205206

206207
rule, err = Parse("!(foo=bar|bar=foo)")
207208
assert.Nil(t, err, "There should be no errors but got: %s", err)
208-
expected = &Chain{op: NONE, rules: []Filter{
209-
&Chain{op: ANY, rules: []Filter{
209+
expected = &Chain{op: None, rules: []Filter{
210+
&Chain{op: Any, rules: []Filter{
210211
&Condition{op: Equal, column: "foo", value: "bar"},
211212
&Condition{op: Equal, column: "bar", value: "foo"},
212213
}},
@@ -215,35 +216,35 @@ func TestParser(t *testing.T) {
215216

216217
rule, err = Parse("((!foo=bar)&bar!=foo)")
217218
assert.Nil(t, err, "There should be no errors but got: %s", err)
218-
expected = &Chain{op: ALL, rules: []Filter{
219-
&Chain{op: NONE, rules: []Filter{&Condition{op: Equal, column: "foo", value: "bar"}}},
219+
expected = &Chain{op: All, rules: []Filter{
220+
&Chain{op: None, rules: []Filter{&Condition{op: Equal, column: "foo", value: "bar"}}},
220221
&Condition{op: UnEqual, column: "bar", value: "foo"},
221222
}}
222223
assert.Equal(t, expected, rule)
223224

224225
rule, err = Parse("!foo&!bar")
225226
assert.Nil(t, err, "There should be no errors but got: %s", err)
226-
expected = &Chain{op: ALL, rules: []Filter{
227-
&Chain{op: NONE, rules: []Filter{&Exists{column: "foo"}}},
228-
&Chain{op: NONE, rules: []Filter{&Exists{column: "bar"}}},
227+
expected = &Chain{op: All, rules: []Filter{
228+
&Chain{op: None, rules: []Filter{&Exists{column: "foo"}}},
229+
&Chain{op: None, rules: []Filter{&Exists{column: "bar"}}},
229230
}}
230231
assert.Equal(t, expected, rule)
231232

232233
rule, err = Parse("!(!foo|bar)")
233234
assert.Nil(t, err, "There should be no errors but got: %s", err)
234-
expected = &Chain{op: NONE, rules: []Filter{
235-
&Chain{op: ANY, rules: []Filter{
236-
&Chain{op: NONE, rules: []Filter{&Exists{column: "foo"}}},
235+
expected = &Chain{op: None, rules: []Filter{
236+
&Chain{op: Any, rules: []Filter{
237+
&Chain{op: None, rules: []Filter{&Exists{column: "foo"}}},
237238
&Exists{column: "bar"},
238239
}},
239240
}}
240241
assert.Equal(t, expected, rule)
241242

242243
rule, err = Parse("!(!(foo|bar))")
243244
assert.Nil(t, err, "There should be no errors but got: %s", err)
244-
expected = &Chain{op: NONE, rules: []Filter{
245-
&Chain{op: NONE, rules: []Filter{
246-
&Chain{op: ANY, rules: []Filter{
245+
expected = &Chain{op: None, rules: []Filter{
246+
&Chain{op: None, rules: []Filter{
247+
&Chain{op: Any, rules: []Filter{
247248
&Exists{column: "foo"},
248249
&Exists{column: "bar"}},
249250
},
@@ -253,22 +254,22 @@ func TestParser(t *testing.T) {
253254

254255
rule, err = Parse("foo=bar&bar!=foo")
255256
assert.Nil(t, err, "There should be no errors but got: %s", err)
256-
expected = &Chain{op: ALL, rules: []Filter{
257+
expected = &Chain{op: All, rules: []Filter{
257258
&Condition{op: Equal, column: "foo", value: "bar"},
258259
&Condition{op: UnEqual, column: "bar", value: "foo"},
259260
}}
260261
assert.Equal(t, expected, rule)
261262

262263
rule, err = Parse("!(foo=bar|bar=foo)&(foo!=bar|bar!=foo)")
263264
assert.Nil(t, err, "There should be no errors but got: %s", err)
264-
expected = &Chain{op: ALL, rules: []Filter{
265-
&Chain{op: NONE, rules: []Filter{
266-
&Chain{op: ANY, rules: []Filter{
265+
expected = &Chain{op: All, rules: []Filter{
266+
&Chain{op: None, rules: []Filter{
267+
&Chain{op: Any, rules: []Filter{
267268
&Condition{op: Equal, column: "foo", value: "bar"},
268269
&Condition{op: Equal, column: "bar", value: "foo"},
269270
}},
270271
}},
271-
&Chain{op: ANY, rules: []Filter{
272+
&Chain{op: Any, rules: []Filter{
272273
&Condition{op: UnEqual, column: "foo", value: "bar"},
273274
&Condition{op: UnEqual, column: "bar", value: "foo"},
274275
}},
@@ -278,14 +279,14 @@ func TestParser(t *testing.T) {
278279
rule, err = Parse("foo=bar&bar!=foo&john>doe|doe<john&column!=value|column=value")
279280
assert.Nil(t, err, "There should be no errors but got: %s", err)
280281

281-
expected = &Chain{op: ANY, rules: []Filter{
282-
&Chain{op: ALL, rules: []Filter{
282+
expected = &Chain{op: Any, rules: []Filter{
283+
&Chain{op: All, rules: []Filter{
283284
&Condition{op: Equal, column: "foo", value: "bar"},
284285
&Condition{op: UnEqual, column: "bar", value: "foo"},
285286
&Condition{op: GreaterThan, column: "john", value: "doe"},
286287
}},
287-
&Chain{op: ANY, rules: []Filter{
288-
&Chain{op: ALL, rules: []Filter{
288+
&Chain{op: Any, rules: []Filter{
289+
&Chain{op: All, rules: []Filter{
289290
&Condition{op: LessThan, column: "doe", value: "john"},
290291
&Condition{op: UnEqual, column: "column", value: "value"},
291292
}},
@@ -297,13 +298,13 @@ func TestParser(t *testing.T) {
297298
rule, err = Parse("foo=bar&bar!=foo&(john>doe|doe<john&column!=value)|column=value")
298299
assert.Nil(t, err, "There should be no errors but got: %s", err)
299300

300-
expected = &Chain{op: ANY, rules: []Filter{
301-
&Chain{op: ALL, rules: []Filter{
301+
expected = &Chain{op: Any, rules: []Filter{
302+
&Chain{op: All, rules: []Filter{
302303
&Condition{op: Equal, column: "foo", value: "bar"},
303304
&Condition{op: UnEqual, column: "bar", value: "foo"},
304-
&Chain{op: ANY, rules: []Filter{
305+
&Chain{op: Any, rules: []Filter{
305306
&Condition{op: GreaterThan, column: "john", value: "doe"},
306-
&Chain{op: ALL, rules: []Filter{
307+
&Chain{op: All, rules: []Filter{
307308
&Condition{op: LessThan, column: "doe", value: "john"},
308309
&Condition{op: UnEqual, column: "column", value: "value"},
309310
}},
@@ -316,16 +317,16 @@ func TestParser(t *testing.T) {
316317
rule, err = Parse("foo=bar&bar!=foo|(john>doe|doe<john&column!=value)&column=value")
317318
assert.Nil(t, err, "There should be no errors but got: %s", err)
318319

319-
expected = &Chain{op: ANY, rules: []Filter{
320+
expected = &Chain{op: Any, rules: []Filter{
320321
// The first two filter conditions
321-
&Chain{op: ALL, rules: []Filter{
322+
&Chain{op: All, rules: []Filter{
322323
&Condition{op: Equal, column: "foo", value: "bar"},
323324
&Condition{op: UnEqual, column: "bar", value: "foo"},
324325
}},
325-
&Chain{op: ALL, rules: []Filter{
326-
&Chain{op: ANY, rules: []Filter{ // Represents the filter conditions within the parentheses
326+
&Chain{op: All, rules: []Filter{
327+
&Chain{op: Any, rules: []Filter{ // Represents the filter conditions within the parentheses
327328
&Condition{op: GreaterThan, column: "john", value: "doe"},
328-
&Chain{op: ALL, rules: []Filter{
329+
&Chain{op: All, rules: []Filter{
329330
&Condition{op: LessThan, column: "doe", value: "john"},
330331
&Condition{op: UnEqual, column: "column", value: "value"},
331332
}},
@@ -339,18 +340,18 @@ func TestParser(t *testing.T) {
339340
rule, err = Parse("foo=bar&bar!=foo|(john>doe|doe<john&(column!=value|value!~column))&column=value")
340341
assert.Nil(t, err, "There should be no errors but got: %s", err)
341342

342-
expected = &Chain{op: ANY, rules: []Filter{
343+
expected = &Chain{op: Any, rules: []Filter{
343344
// The first two filter conditions
344-
&Chain{op: ALL, rules: []Filter{
345+
&Chain{op: All, rules: []Filter{
345346
&Condition{op: Equal, column: "foo", value: "bar"},
346347
&Condition{op: UnEqual, column: "bar", value: "foo"},
347348
}},
348-
&Chain{op: ALL, rules: []Filter{
349-
&Chain{op: ANY, rules: []Filter{ // Represents the filter conditions within the parentheses
349+
&Chain{op: All, rules: []Filter{
350+
&Chain{op: Any, rules: []Filter{ // Represents the filter conditions within the parentheses
350351
&Condition{op: GreaterThan, column: "john", value: "doe"},
351-
&Chain{op: ALL, rules: []Filter{
352+
&Chain{op: All, rules: []Filter{
352353
&Condition{op: LessThan, column: "doe", value: "john"},
353-
&Chain{op: ANY, rules: []Filter{ // Represents the filter conditions within the nested parentheses
354+
&Chain{op: Any, rules: []Filter{ // Represents the filter conditions within the nested parentheses
354355
&Condition{op: UnEqual, column: "column", value: "value"},
355356
&Condition{op: UnLike, column: "value", value: "column"},
356357
}},

internal/filter/types.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99
type LogicalOp string
1010

1111
const (
12-
// NONE represents a filter chain type that matches when none of its ruleset matches.
13-
NONE LogicalOp = "!"
14-
// ALL represents a filter chain type that matches when all of its ruleset matches.
15-
ALL LogicalOp = "&"
16-
// ANY represents a filter chain type that matches when at least one of its ruleset matches.
17-
ANY LogicalOp = "|"
12+
// None represents a filter chain type that matches when none of its ruleset matches.
13+
None LogicalOp = "!"
14+
// All represents a filter chain type that matches when all of its ruleset matches.
15+
All LogicalOp = "&"
16+
// Any represents a filter chain type that matches when at least one of its ruleset matches.
17+
Any LogicalOp = "|"
1818
)
1919

2020
// Chain is a filter type that wraps other filter rules and itself.
@@ -26,7 +26,7 @@ type Chain struct {
2626
}
2727

2828
func NewChain(op LogicalOp, rules ...Filter) (*Chain, error) {
29-
if op != NONE && op != ALL && op != ANY {
29+
if op != None && op != All && op != Any {
3030
return nil, fmt.Errorf("invalid logical operator provided: %s", op)
3131
}
3232

@@ -36,7 +36,7 @@ func NewChain(op LogicalOp, rules ...Filter) (*Chain, error) {
3636
// Eval evaluates the filter rule sets recursively based on their operator type.
3737
func (c *Chain) Eval(filterable Filterable) (bool, error) {
3838
switch c.op {
39-
case NONE:
39+
case None:
4040
for _, rule := range c.rules {
4141
matched, err := rule.Eval(filterable)
4242
if err != nil {
@@ -49,7 +49,7 @@ func (c *Chain) Eval(filterable Filterable) (bool, error) {
4949
}
5050

5151
return true, nil
52-
case ALL:
52+
case All:
5353
for _, rule := range c.rules {
5454
matched, err := rule.Eval(filterable)
5555
if err != nil {
@@ -62,7 +62,7 @@ func (c *Chain) Eval(filterable Filterable) (bool, error) {
6262
}
6363

6464
return true, nil
65-
case ANY:
65+
case Any:
6666
for _, rule := range c.rules {
6767
matched, err := rule.Eval(filterable)
6868
if err != nil {

0 commit comments

Comments
 (0)