Skip to content

Commit 3651cda

Browse files
Add a method to simplify casting (#396)
Signed-off-by: Francesco Guardiani <[email protected]>
1 parent ee4c85b commit 3651cda

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

sql/src/main/java/io/cloudevents/sql/EvaluationRuntime.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ public interface EvaluationRuntime {
2727
*/
2828
Object cast(EvaluationContext ctx, Object value, Type target);
2929

30+
/**
31+
* Return the {@code value} casted to the {@code target} type. If fails, this is going to throw an exception without the evaluation context.
32+
*
33+
* @param value the value to cast
34+
* @param target the type cast target
35+
* @return the casted value, if the cast succeeds, otherwise the default value of the target type
36+
*/
37+
Object cast(Object value, Type target) throws EvaluationException;
38+
3039
/**
3140
* Resolve a {@link Function} starting from its name and the concrete number of arguments.
3241
*

sql/src/main/java/io/cloudevents/sql/impl/EvaluationRuntimeImpl.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package io.cloudevents.sql.impl;
22

3-
import io.cloudevents.sql.EvaluationContext;
4-
import io.cloudevents.sql.EvaluationRuntime;
5-
import io.cloudevents.sql.Function;
6-
import io.cloudevents.sql.Type;
3+
import io.cloudevents.sql.*;
74

85
public class EvaluationRuntimeImpl implements EvaluationRuntime {
96

@@ -36,6 +33,11 @@ public Object cast(EvaluationContext ctx, Object value, Type target) {
3633
return this.typeCastingProvider.cast(ctx, value, target);
3734
}
3835

36+
@Override
37+
public Object cast(Object value, Type target) throws EvaluationException {
38+
return this.typeCastingProvider.cast(FailFastExceptionThrower.getInstance(), value, target);
39+
}
40+
3941
@Override
4042
public Function resolveFunction(String name, int args) throws IllegalStateException {
4143
return functionTable.resolve(name, args);

sql/src/main/java/io/cloudevents/sql/impl/FailFastExceptionThrower.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package io.cloudevents.sql.impl;
22

3+
import io.cloudevents.sql.EvaluationContext;
34
import io.cloudevents.sql.EvaluationException;
5+
import org.antlr.v4.runtime.misc.Interval;
46

5-
class FailFastExceptionThrower implements ExceptionThrower {
7+
class FailFastExceptionThrower implements ExceptionThrower, EvaluationContext {
68

79
private static class SingletonContainer {
810
private final static FailFastExceptionThrower INSTANCE = new FailFastExceptionThrower();
@@ -16,4 +18,24 @@ static FailFastExceptionThrower getInstance() {
1618
public void throwException(EvaluationException exception) {
1719
throw exception;
1820
}
21+
22+
@Override
23+
public Interval expressionInterval() {
24+
return Interval.INVALID;
25+
}
26+
27+
@Override
28+
public String expressionText() {
29+
return "";
30+
}
31+
32+
@Override
33+
public void appendException(EvaluationException exception) {
34+
throwException(exception);
35+
}
36+
37+
@Override
38+
public void appendException(EvaluationException.EvaluationExceptionFactory exceptionFactory) {
39+
throwException(exceptionFactory.create(expressionInterval(), expressionText()));
40+
}
1941
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.cloudevents.sql.impl;
2+
3+
import io.cloudevents.sql.EvaluationException;
4+
import io.cloudevents.sql.EvaluationRuntime;
5+
import io.cloudevents.sql.Type;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.assertj.core.api.Assertions.assertThatCode;
10+
11+
public class EvaluationRuntimeImplTest {
12+
13+
@Test
14+
void castingFails() {
15+
assertThatCode(() -> EvaluationRuntime.getDefault().cast("123", Type.BOOLEAN))
16+
.isInstanceOf(EvaluationException.class);
17+
}
18+
19+
@Test
20+
void castingSucceeds() {
21+
assertThat(EvaluationRuntime.getDefault().cast("TRUE", Type.BOOLEAN))
22+
.isEqualTo(true);
23+
}
24+
25+
}

0 commit comments

Comments
 (0)