Skip to content

Commit

Permalink
Merge pull request #20 from sidhant92/develop
Browse files Browse the repository at this point in the history
Default Field Support
  • Loading branch information
sidhant92 authored Oct 29, 2023
2 parents de0161e + 8636afe commit 209b6b7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sourceCompatibility = 1.8
targetCompatibility = 1.8

group 'com.github.sidhant92'
version = "1.0.1"
version = "1.1.0"

apply plugin: "com.dipien.semantic-version"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ public BooleanExpressionEvaluator(final BoolExpressionParser boolExpressionParse
operatorService = new OperatorService();
}

public Try<Boolean> evaluate(final String expression, final Map<String, Object> data) {
final Try<Node> tokenOptional = boolExpressionParser.parseExpression(expression);
public Try<Boolean> evaluate(final String expression, final Map<String, Object> data, final String defaultField) {
final Try<Node> tokenOptional = boolExpressionParser.parseExpression(expression, defaultField);
return tokenOptional.map(node -> evaluateToken(node, data));
}

public Try<Boolean> evaluate(final String expression, final Map<String, Object> data) {
return evaluate(expression, data, null);
}

private boolean evaluateToken(final Node node, final Map<String, Object> data) {
switch (node.getTokenType()) {
case COMPARISON:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
*/
public interface BoolExpressionParser {
Try<Node> parseExpression(final String expression);

Try<Node> parseExpression(final String expression, final String defaultField);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,30 @@ public BoolParser(final int maxCacheSize) {
this.cache = Caffeine.newBuilder().maximumSize(maxCacheSize).build();
}

@Override
public Try<Node> parseExpression(final String expression, final String defaultField) {
return Try.of(() -> getNode(expression, defaultField));
}

@Override
public Try<Node> parseExpression(final String expression) {
return Try.of(() -> getNode(expression));
return Try.of(() -> getNode(expression, null));
}

private Node getNode(final String expression) {
private Node getNode(final String expression, final String defaultField) {
if (useCache) {
return cache.get(expression, this::parse);
return cache.get(expression, ex -> parse(ex, defaultField));
}
return parse(expression);
return parse(expression, defaultField);
}

private Node parse(final String expression) {
private Node parse(final String expression, final String defaultField) {
final BooleanExpressionLexer filterLexer = new BooleanExpressionLexer(CharStreams.fromString(expression));
final CommonTokenStream commonTokenStream = new CommonTokenStream(filterLexer);
final BooleanExpressionParser filterParser = new BooleanExpressionParser(commonTokenStream);
final ParseTree parseTree = filterParser.parse();

final BooleanFilterListener listener = new BooleanFilterListener();
final BooleanFilterListener listener = new BooleanFilterListener(defaultField);
parseTreeWalker.walk(listener, parseTree);

return listener.getNode();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.github.sidhant92.boolparser.parser.antlr;

import java.util.List;
import java.util.Objects;
import java.util.Stack;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import com.github.sidhant92.boolparser.constant.DataType;
import com.github.sidhant92.boolparser.constant.LogicalOperationType;
Expand All @@ -25,7 +27,10 @@ public class BooleanFilterListener extends BooleanExpressionBaseListener {

private org.antlr.v4.runtime.Token lastToken;

public BooleanFilterListener() {
private String defaultField;

public BooleanFilterListener(final String defaultField) {
this.defaultField = defaultField;
this.node = null;
this.lastToken = null;
this.currentNodes = new Stack<>();
Expand All @@ -37,7 +42,7 @@ public Node getNode() {

@Override
public void exitComparatorExpression(BooleanExpressionParser.ComparatorExpressionContext ctx) {
final String variableName = ctx.left.getText();
final String variableName = getField(ctx.left.getText());
final DataType dataType = getDataType(ctx.right.getStart());
final Operator operator = Operator.getOperatorFromSymbol(ctx.op.getText()).orElse(Operator.EQUALS);
currentNodes.add(new ComparisonNode(variableName, ValueUtils.convertValue(ctx.right.getText(), dataType), operator, dataType));
Expand All @@ -46,7 +51,7 @@ public void exitComparatorExpression(BooleanExpressionParser.ComparatorExpressio

@Override
public void exitToExpression(BooleanExpressionParser.ToExpressionContext ctx) {
final String field = ctx.field.getText();
final String field = getField(ctx.field.getText());
final DataType lowerDataType = getDataType(ctx.lower.start);
final Object lowerValue = ValueUtils.convertValue(ctx.lower.start.getText(), lowerDataType);
final DataType upperDataType = getDataType(ctx.upper.start);
Expand All @@ -57,7 +62,7 @@ public void exitToExpression(BooleanExpressionParser.ToExpressionContext ctx) {

@Override
public void exitInExpression(BooleanExpressionParser.InExpressionContext ctx) {
final String field = ctx.field.getText();
final String field = getField(ctx.field.getText());
final List<Pair<DataType, Object>> items = ctx.data.children
.stream()
.filter(child -> child instanceof BooleanExpressionParser.TypesContext)
Expand All @@ -71,6 +76,13 @@ public void exitInExpression(BooleanExpressionParser.InExpressionContext ctx) {
super.exitInExpression(ctx);
}

private String getField(final String field) {
if (Objects.isNull(defaultField)) {
return field;
}
return StringUtils.isBlank(field) ? defaultField : field;
}

private DataType getDataType(final org.antlr.v4.runtime.Token token) {
switch (token.getType()) {
case BooleanExpressionLexer.DECIMAL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,22 @@ public void testStringEqualityWithQuotes() {
assertTrue(booleanOptional.isSuccess());
assertTrue(booleanOptional.get());
}

@Test
public void testDefaultFieldTrue() {
final Map<String, Object> data = new HashMap<>();
data.put("age", 19);
final Try<Boolean> booleanOptional = booleanExpressionEvaluator.evaluate(">= 18 AND < 20", data, "age");
assertTrue(booleanOptional.isSuccess());
assertTrue(booleanOptional.get());
}

@Test
public void testDefaultFieldFalse() {
final Map<String, Object> data = new HashMap<>();
data.put("age", 17);
final Try<Boolean> booleanOptional = booleanExpressionEvaluator.evaluate(">= 18 AND < 20", data, "age");
assertTrue(booleanOptional.isSuccess());
assertFalse(booleanOptional.get());
}
}

0 comments on commit 209b6b7

Please sign in to comment.