Skip to content

Commit 30737cb

Browse files
committed
throw error for invalid data type
1 parent 94cefda commit 30737cb

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

src/main/java/com/github/sidhant92/boolparser/constant/ContainerDataType.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,28 @@
22

33
import java.util.Optional;
44
import com.github.sidhant92.boolparser.datatype.DataTypeFactory;
5+
import com.github.sidhant92.boolparser.exception.InvalidDataType;
56
import lombok.AllArgsConstructor;
67
import lombok.Getter;
8+
import lombok.extern.slf4j.Slf4j;
79

810
/**
911
* @author sidhant.aggarwal
1012
* @since 05/02/2023
1113
*/
1214
@Getter
1315
@AllArgsConstructor
16+
@Slf4j
1417
public enum ContainerDataType {
1518
primitive() {
1619
@Override
1720
public <T> Optional<T> getValue(final DataType dataType, final Object value) {
18-
return DataTypeFactory.getDataType(dataType).getValue(value);
21+
final Optional<T> result = DataTypeFactory.getDataType(dataType).getValue(value);
22+
if (!result.isPresent()) {
23+
log.error("Invalid data type for value {} for data type {}", value.toString(), dataType.name());
24+
throw new InvalidDataType(String.format("Invalid data type for value %s for data type %s", value, dataType.name()));
25+
}
26+
return result;
1927
}
2028

2129
@Override
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.github.sidhant92.boolparser.exception;
2+
3+
public class InvalidDataType extends RuntimeException {
4+
public InvalidDataType(final String message) {
5+
super(message);
6+
}
7+
8+
public InvalidDataType() {
9+
super();
10+
}
11+
12+
@Override
13+
public String getMessage() {
14+
return "Invalid Data Type";
15+
}
16+
}

src/main/java/com/github/sidhant92/boolparser/operator/AbstractOperator.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,4 @@ public abstract <T extends Comparable<? super T>> boolean evaluate(final Contain
1515
public abstract Operator getOperator();
1616

1717
public abstract String getSymbol();
18-
19-
//public boolean validate(final ContainerDataType containerDataType, final DataType dataType, final Object leftOperand)
2018
}

src/test/java/com/github/sidhant92/boolparser/application/BooleanExpressionEvaluatorTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.HashMap;
66
import java.util.Map;
77
import org.junit.jupiter.api.Test;
8+
import com.github.sidhant92.boolparser.exception.InvalidDataType;
89
import com.github.sidhant92.boolparser.exception.InvalidUnaryOperand;
910
import com.github.sidhant92.boolparser.parser.antlr.BoolParser;
1011
import io.vavr.control.Try;
@@ -62,6 +63,15 @@ public void testSimpleTrueCorrectExpressions() {
6263
assertTrue(booleanOptional.get());
6364
}
6465

66+
@Test
67+
public void testInvalidDataType() {
68+
final Map<String, Object> data = new HashMap<>();
69+
data.put("name", "abc-");
70+
final Try<Boolean> booleanOptional = booleanExpressionEvaluator.evaluate("name > 123", data);
71+
assertTrue(booleanOptional.isFailure());
72+
assertTrue(booleanOptional.getCause() instanceof InvalidDataType);
73+
}
74+
6575
@Test
6676
public void testSimpleFalseIncorrectExpression() {
6777
final Map<String, Object> data = new HashMap<>();
@@ -311,8 +321,8 @@ public void testWrongDataType1() {
311321
final Map<String, Object> data = new HashMap<>();
312322
data.put("age", "sf");
313323
final Try<Boolean> booleanOptional = booleanExpressionEvaluator.evaluate("age = 24", data);
314-
assertTrue(booleanOptional.isSuccess());
315-
assertFalse(booleanOptional.get());
324+
assertTrue(booleanOptional.isFailure());
325+
assertTrue(booleanOptional.getCause() instanceof InvalidDataType);
316326
}
317327

318328
@Test

src/test/java/com/github/sidhant92/boolparser/parser/antlr/benchmark/ParserBenchmarkService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void setup() {
4747
@Warmup (iterations = 2)
4848
@BenchmarkMode (Mode.SampleTime) //change here to check for specific mode
4949
public void benchmarkEvaluation() {
50-
final String rule = "b>0";
50+
final String rule = "b>0 AND z IN ('c1', 'c2')";
5151
parser.parseExpression(rule);
5252
}
5353
}

0 commit comments

Comments
 (0)