Skip to content

Commit

Permalink
Merge pull request #41 from sidhant92/array_math_functions
Browse files Browse the repository at this point in the history
Support for null check
  • Loading branch information
sidhant92 authored Aug 16, 2024
2 parents 4491a78 + 31d79ad commit c9045a6
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import com.github.sidhant92.boolparser.constant.ContainerDataType;
import com.github.sidhant92.boolparser.constant.DataType;
import com.github.sidhant92.boolparser.constant.Operator;
import com.github.sidhant92.boolparser.domain.FieldNode;
import com.github.sidhant92.boolparser.domain.logical.ArrayNode;
import com.github.sidhant92.boolparser.domain.logical.BooleanNode;
import com.github.sidhant92.boolparser.domain.EvaluatedNode;
Expand Down Expand Up @@ -69,9 +71,11 @@ private boolean evaluateToken(final Node node, final Map<String, Object> data) {
}

private boolean evaluateComparisonToken(final ComparisonNode comparisonToken, final Map<String, Object> data) {
final Object fieldData = ValueUtils.getValueFromMap(comparisonToken.getField(), data)
.orElseThrow(() -> new DataNotFoundException(comparisonToken.getField()));
final Object value = comparisonToken.getValue() instanceof ArithmeticBaseNode ? arithmeticExpressionEvaluator.evaluate(
final Optional<Object> fieldDataOptional = ValueUtils.getValueFromMap(comparisonToken.getField(), data);

final Object fieldData = comparisonToken.isNullCheck() ? fieldDataOptional.orElse("null") : fieldDataOptional.orElseThrow(
() -> new DataNotFoundException(comparisonToken.getField()));
final Object value = comparisonToken.isNullCheck() ? "null" : comparisonToken.getValue() instanceof ArithmeticBaseNode ? arithmeticExpressionEvaluator.evaluate(
comparisonToken.getValue(), data) : comparisonToken.getValue();
return operatorService.evaluateLogicalOperator(comparisonToken.getOperator(), ContainerDataType.PRIMITIVE, comparisonToken.getDataType(),
fieldData, value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.sidhant92.boolparser.constant;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import com.github.sidhant92.boolparser.operator.logical.AbstractOperator;
import com.github.sidhant92.boolparser.operator.OperatorFactory;
Expand Down Expand Up @@ -49,4 +51,8 @@ public static Optional<Operator> getOperatorFromSymbol(final String symbol) {
.map(com.github.sidhant92.boolparser.operator.arithmetic.AbstractOperator::getOperator)
.findFirst();
}

public static List<Operator> getEqualityOperators() {
return Arrays.asList(EQUALS, NOT_EQUAL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ public class FieldNode extends ArithmeticBaseNode {
public NodeType getTokenType() {
return NodeType.FIELD;
}

public boolean isNull() {
return this.field.equalsIgnoreCase("null");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.sidhant92.boolparser.constant.DataType;
import com.github.sidhant92.boolparser.constant.NodeType;
import com.github.sidhant92.boolparser.constant.Operator;
import com.github.sidhant92.boolparser.domain.FieldNode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -29,4 +30,8 @@ public class ComparisonNode extends Node {
public NodeType getTokenType() {
return NodeType.COMPARISON;
}

public boolean isNullCheck() {
return Operator.getEqualityOperators().contains(this.operator) && this.value instanceof FieldNode && ((FieldNode) this.value).isNull();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.sidhant92.boolparser.parser.antlr;

import java.util.Optional;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.sidhant92.boolparser.domain.logical.Node;
Expand All @@ -25,6 +26,7 @@ public Try<Node> parseExpression(final String expression) {
}

private Node getNode(final String expression, final String defaultField) {
return cache.get(expression, ex -> super.parse(ex, defaultField));
final String cacheKey = expression + "_" + Optional.ofNullable(defaultField).orElse("");
return cache.get(cacheKey, ex -> super.parse(ex, defaultField));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -586,4 +586,40 @@ public void testComparisonWithArithmeticFalseCondition1() {
assertTrue(booleanOptional.isSuccess());
assertFalse(booleanOptional.get());
}

@Test
public void testNullCheck() {
final Map<String, Object> data = new HashMap<>();
data.put("a", 2.7);
final Try<Boolean> resultOptional = booleanExpressionEvaluator.evaluate("b = null", data);
assertTrue(resultOptional.isSuccess());
assertEquals(resultOptional.get(), true);
}

@Test
public void testNullCheck1() {
final Map<String, Object> data = new HashMap<>();
data.put("a", 2.7);
final Try<Boolean> resultOptional = booleanExpressionEvaluator.evaluate("a = null", data);
assertTrue(resultOptional.isSuccess());
assertEquals(resultOptional.get(), false);
}

@Test
public void testNotNullCheck() {
final Map<String, Object> data = new HashMap<>();
data.put("a", 2.7);
final Try<Boolean> resultOptional = booleanExpressionEvaluator.evaluate("a != null", data);
assertTrue(resultOptional.isSuccess());
assertEquals(resultOptional.get(), true);
}

@Test
public void testBooleanNullCheck() {
final Map<String, Object> data = new HashMap<>();
data.put("a", 3);
final Try<Boolean> resultOptional = booleanExpressionEvaluator.evaluate("b = null && a > 2", data);
assertTrue(resultOptional.isSuccess());
assertEquals(resultOptional.get(), true);
}
}

0 comments on commit c9045a6

Please sign in to comment.