Skip to content

Commit

Permalink
feat: removed negation expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
valmoz committed Dec 7, 2021
1 parent 693c8d6 commit 04025eb
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 319 deletions.
33 changes: 18 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,33 @@ field op value
The **field** is a lowercase string, with dots and underscores.

The **op** is one of the following (unquoted):
- Equal: '='
- Greater than: '>'
- Greater than or equal to: '>='
- Less than: '<'
- Less than or equal to: '<='
- Not equal: '<>', '!='
- Like: 'like', 'LIKE'
- _Equal_: '='
- _Greater than_: '>'
- _Greater than or equal to_: '>='
- _Less than_: '<'
- _Less than or equal to_: '<='
- _Not equal_: '<>', '!='
- _Like_: 'like', 'LIKE'

The **value** can be one of:
- String: 'value'
- Booleam: true, false
- Int: 46
- Double: 12.34
- _String_: 'value'
- _Booleam_: true, false
- _Int_: 46
- _Double_: 12.34

Additionally, it is possible to check if field has a value or not, using NULL:
- _Null field_: 'IS NULL', 'is null', '= null', '= NULL'
- _Not Null field_: 'IS NOT NULL', 'is not null', '<> null', '!= null', '<> NULL', '!= NULL'

It is possible to use the following operators:
- Negation: not, NOT
- Conjunction: and, AND
- Disjunction: or, OR
- _Conjunction_: 'and', 'AND'
- _Disjunction_: 'or', 'OR'

Parenthesis can be used to compose complex expressions.

For example:
```
city = 'Bergamo' and (age < 30 or (dev = true and not (name = 'Giorgio')))
city = 'Bergamo' and (age < 30 or (dev = true and (name = 'Giorgio' and name is not null)))
```

## Testing
Expand Down
1 change: 0 additions & 1 deletion grammar/ApiFilter.g4
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ filter: expression EOF;
expression:
condition # conditionExp
| OPEN_PAR expression CLOSE_PAR # parenthesisExp
| NOT expression # negationExp
| expression AND expression # conjunctionExp
| expression OR expression # disjunctionExp;

Expand Down
37 changes: 0 additions & 37 deletions src/ApiFilter/Filter/Negation.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/ApiFilter/FilterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use FattureInCloud\ApiFilter\Filter\Expression;
use FattureInCloud\ApiFilter\Filter\FilledField;
use FattureInCloud\ApiFilter\Filter\Filter;
use FattureInCloud\ApiFilter\Filter\Negation;
use FattureInCloud\ApiFilter\Filter\Operator;
use FattureInCloud\ApiFilter\Parser\ApiFilterLexer;
use FattureInCloud\ApiFilter\Parser\ApiFilterParser;
Expand All @@ -25,7 +24,6 @@
use FattureInCloud\ApiFilter\Parser\Context\IntegerContext;
use FattureInCloud\ApiFilter\Parser\Context\DecimalContext;
use FattureInCloud\ApiFilter\Parser\Context\ParenthesisExpContext;
use FattureInCloud\ApiFilter\Parser\Context\NegationExpContext;
use FattureInCloud\ApiFilter\Parser\Context\ConjunctionExpContext;
use FattureInCloud\ApiFilter\Parser\Context\DisjunctionExpContext;
use FattureInCloud\ApiFilter\Parser\Context\ValueContext;
Expand Down Expand Up @@ -61,11 +59,6 @@ public function visitParenthesisExp(ParenthesisExpContext $context): Expression
return $this->visit($context->expression());
}

public function visitNegationExp(NegationExpContext $context): Negation
{
return new Negation($this->visit($context->expression()));
}

public function visitConjunctionExp(ConjunctionExpContext $context): Conjunction
{
$left = $this->visit($context->expression(0));
Expand Down
2 changes: 1 addition & 1 deletion src/ApiFilter/Parser/ApiFilter.interp
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ decimal


atn:
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 22, 82, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 34, 10, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 42, 10, 3, 12, 3, 14, 3, 45, 11, 3, 3, 4, 3, 4, 3, 4, 5, 4, 50, 10, 4, 3, 5, 3, 5, 3, 5, 3, 5, 3, 6, 3, 6, 3, 6, 3, 6, 3, 7, 3, 7, 3, 7, 3, 7, 5, 7, 64, 10, 7, 3, 7, 3, 7, 3, 8, 3, 8, 3, 9, 3, 9, 3, 9, 3, 9, 5, 9, 74, 10, 9, 3, 10, 3, 10, 3, 11, 3, 11, 3, 11, 3, 11, 3, 11, 2, 3, 4, 12, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 2, 4, 4, 2, 3, 3, 15, 15, 3, 2, 3, 9, 2, 81, 2, 22, 3, 2, 2, 2, 4, 33, 3, 2, 2, 2, 6, 49, 3, 2, 2, 2, 8, 51, 3, 2, 2, 2, 10, 55, 3, 2, 2, 2, 12, 59, 3, 2, 2, 2, 14, 67, 3, 2, 2, 2, 16, 73, 3, 2, 2, 2, 18, 75, 3, 2, 2, 2, 20, 77, 3, 2, 2, 2, 22, 23, 5, 4, 3, 2, 23, 24, 7, 2, 2, 3, 24, 3, 3, 2, 2, 2, 25, 26, 8, 3, 1, 2, 26, 34, 5, 6, 4, 2, 27, 28, 7, 17, 2, 2, 28, 29, 5, 4, 3, 2, 29, 30, 7, 18, 2, 2, 30, 34, 3, 2, 2, 2, 31, 32, 7, 14, 2, 2, 32, 34, 5, 4, 3, 5, 33, 25, 3, 2, 2, 2, 33, 27, 3, 2, 2, 2, 33, 31, 3, 2, 2, 2, 34, 43, 3, 2, 2, 2, 35, 36, 12, 4, 2, 2, 36, 37, 7, 12, 2, 2, 37, 42, 5, 4, 3, 5, 38, 39, 12, 3, 2, 2, 39, 40, 7, 13, 2, 2, 40, 42, 5, 4, 3, 4, 41, 35, 3, 2, 2, 2, 41, 38, 3, 2, 2, 2, 42, 45, 3, 2, 2, 2, 43, 41, 3, 2, 2, 2, 43, 44, 3, 2, 2, 2, 44, 5, 3, 2, 2, 2, 45, 43, 3, 2, 2, 2, 46, 50, 5, 8, 5, 2, 47, 50, 5, 10, 6, 2, 48, 50, 5, 12, 7, 2, 49, 46, 3, 2, 2, 2, 49, 47, 3, 2, 2, 2, 49, 48, 3, 2, 2, 2, 50, 7, 3, 2, 2, 2, 51, 52, 7, 21, 2, 2, 52, 53, 5, 14, 8, 2, 53, 54, 5, 16, 9, 2, 54, 9, 3, 2, 2, 2, 55, 56, 7, 21, 2, 2, 56, 57, 9, 2, 2, 2, 57, 58, 7, 16, 2, 2, 58, 11, 3, 2, 2, 2, 59, 63, 7, 21, 2, 2, 60, 64, 7, 8, 2, 2, 61, 62, 7, 15, 2, 2, 62, 64, 7, 14, 2, 2, 63, 60, 3, 2, 2, 2, 63, 61, 3, 2, 2, 2, 64, 65, 3, 2, 2, 2, 65, 66, 7, 16, 2, 2, 66, 13, 3, 2, 2, 2, 67, 68, 9, 3, 2, 2, 68, 15, 3, 2, 2, 2, 69, 74, 7, 10, 2, 2, 70, 74, 7, 11, 2, 2, 71, 74, 5, 18, 10, 2, 72, 74, 5, 20, 11, 2, 73, 69, 3, 2, 2, 2, 73, 70, 3, 2, 2, 2, 73, 71, 3, 2, 2, 2, 73, 72, 3, 2, 2, 2, 74, 17, 3, 2, 2, 2, 75, 76, 7, 20, 2, 2, 76, 19, 3, 2, 2, 2, 77, 78, 7, 20, 2, 2, 78, 79, 7, 19, 2, 2, 79, 80, 7, 20, 2, 2, 80, 21, 3, 2, 2, 2, 8, 33, 41, 43, 49, 63, 73]
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 22, 80, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 32, 10, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 40, 10, 3, 12, 3, 14, 3, 43, 11, 3, 3, 4, 3, 4, 3, 4, 5, 4, 48, 10, 4, 3, 5, 3, 5, 3, 5, 3, 5, 3, 6, 3, 6, 3, 6, 3, 6, 3, 7, 3, 7, 3, 7, 3, 7, 5, 7, 62, 10, 7, 3, 7, 3, 7, 3, 8, 3, 8, 3, 9, 3, 9, 3, 9, 3, 9, 5, 9, 72, 10, 9, 3, 10, 3, 10, 3, 11, 3, 11, 3, 11, 3, 11, 3, 11, 2, 3, 4, 12, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 2, 4, 4, 2, 3, 3, 15, 15, 3, 2, 3, 9, 2, 78, 2, 22, 3, 2, 2, 2, 4, 31, 3, 2, 2, 2, 6, 47, 3, 2, 2, 2, 8, 49, 3, 2, 2, 2, 10, 53, 3, 2, 2, 2, 12, 57, 3, 2, 2, 2, 14, 65, 3, 2, 2, 2, 16, 71, 3, 2, 2, 2, 18, 73, 3, 2, 2, 2, 20, 75, 3, 2, 2, 2, 22, 23, 5, 4, 3, 2, 23, 24, 7, 2, 2, 3, 24, 3, 3, 2, 2, 2, 25, 26, 8, 3, 1, 2, 26, 32, 5, 6, 4, 2, 27, 28, 7, 17, 2, 2, 28, 29, 5, 4, 3, 2, 29, 30, 7, 18, 2, 2, 30, 32, 3, 2, 2, 2, 31, 25, 3, 2, 2, 2, 31, 27, 3, 2, 2, 2, 32, 41, 3, 2, 2, 2, 33, 34, 12, 4, 2, 2, 34, 35, 7, 12, 2, 2, 35, 40, 5, 4, 3, 5, 36, 37, 12, 3, 2, 2, 37, 38, 7, 13, 2, 2, 38, 40, 5, 4, 3, 4, 39, 33, 3, 2, 2, 2, 39, 36, 3, 2, 2, 2, 40, 43, 3, 2, 2, 2, 41, 39, 3, 2, 2, 2, 41, 42, 3, 2, 2, 2, 42, 5, 3, 2, 2, 2, 43, 41, 3, 2, 2, 2, 44, 48, 5, 8, 5, 2, 45, 48, 5, 10, 6, 2, 46, 48, 5, 12, 7, 2, 47, 44, 3, 2, 2, 2, 47, 45, 3, 2, 2, 2, 47, 46, 3, 2, 2, 2, 48, 7, 3, 2, 2, 2, 49, 50, 7, 21, 2, 2, 50, 51, 5, 14, 8, 2, 51, 52, 5, 16, 9, 2, 52, 9, 3, 2, 2, 2, 53, 54, 7, 21, 2, 2, 54, 55, 9, 2, 2, 2, 55, 56, 7, 16, 2, 2, 56, 11, 3, 2, 2, 2, 57, 61, 7, 21, 2, 2, 58, 62, 7, 8, 2, 2, 59, 60, 7, 15, 2, 2, 60, 62, 7, 14, 2, 2, 61, 58, 3, 2, 2, 2, 61, 59, 3, 2, 2, 2, 62, 63, 3, 2, 2, 2, 63, 64, 7, 16, 2, 2, 64, 13, 3, 2, 2, 2, 65, 66, 9, 3, 2, 2, 66, 15, 3, 2, 2, 2, 67, 72, 7, 10, 2, 2, 68, 72, 7, 11, 2, 2, 69, 72, 5, 18, 10, 2, 70, 72, 5, 20, 11, 2, 71, 67, 3, 2, 2, 2, 71, 68, 3, 2, 2, 2, 71, 69, 3, 2, 2, 2, 71, 70, 3, 2, 2, 2, 72, 17, 3, 2, 2, 2, 73, 74, 7, 20, 2, 2, 74, 19, 3, 2, 2, 2, 75, 76, 7, 20, 2, 2, 76, 77, 7, 19, 2, 2, 77, 78, 7, 20, 2, 2, 78, 21, 3, 2, 2, 2, 8, 31, 39, 41, 47, 61, 71]
11 changes: 0 additions & 11 deletions src/ApiFilter/Parser/ApiFilterBaseVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,6 @@ public function visitFilter(Context\FilterContext $context)
return $this->visitChildren($context);
}

/**
* {@inheritdoc}
*
* The default implementation returns the result of calling
* {@see self::visitChildren()} on `context`.
*/
public function visitNegationExp(Context\NegationExpContext $context)
{
return $this->visitChildren($context);
}

/**
* {@inheritdoc}
*
Expand Down
Loading

0 comments on commit 04025eb

Please sign in to comment.