feat: expressions use results instead of a thrower interface
Signed-off-by: Calum Murray <cmurray@redhat.com>
This commit is contained in:
parent
1c895dd07f
commit
762b665849
|
@ -3,9 +3,9 @@ package io.cloudevents.sql.impl.expressions;
|
|||
import io.cloudevents.CloudEvent;
|
||||
import io.cloudevents.SpecVersion;
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionFactory;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.impl.ExpressionInternalVisitor;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
import java.util.Base64;
|
||||
|
@ -24,18 +24,15 @@ public class AccessAttributeExpression extends BaseExpression {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThrower thrower) {
|
||||
public EvaluationResult evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionFactory exceptionFactory) {
|
||||
Object value = this.getter.apply(event);
|
||||
if (value == null) {
|
||||
thrower.throwException(
|
||||
ExceptionFactory.missingAttribute(this.expressionInterval(), this.expressionText(), key)
|
||||
);
|
||||
return "";
|
||||
return new EvaluationResult(false, exceptionFactory.missingAttribute(this.expressionInterval(), this.expressionText(), key));
|
||||
}
|
||||
|
||||
// Because the CESQL type system is smaller than the CE type system,
|
||||
// we need to coherce some values to string
|
||||
return coherceTypes(value);
|
||||
return new EvaluationResult(coherceTypes(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package io.cloudevents.sql.impl.expressions;
|
||||
|
||||
import io.cloudevents.CloudEvent;
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.Type;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
public class AndExpression extends BaseBinaryExpression {
|
||||
|
@ -12,12 +15,15 @@ public class AndExpression extends BaseBinaryExpression {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object evaluate(EvaluationRuntime runtime, Object left, Object right, ExceptionThrower exceptions) {
|
||||
boolean x = castToBoolean(runtime, exceptions, left);
|
||||
if (!x) {
|
||||
public EvaluationResult evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionFactory exceptionFactory) {
|
||||
EvaluationResult left = this.getLeftOperand().evaluate(runtime, event, exceptionFactory);
|
||||
EvaluationResult x = castToBoolean(exceptionFactory, left);
|
||||
if (!(Boolean)x.value()) {
|
||||
// Short circuit
|
||||
return false;
|
||||
return x;
|
||||
}
|
||||
return castToBoolean(runtime, exceptions, right);
|
||||
|
||||
EvaluationResult right = this.getRightOperand().evaluate(runtime, event, exceptionFactory);
|
||||
return castToBoolean(exceptionFactory, right).wrap(left);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,9 +2,11 @@ package io.cloudevents.sql.impl.expressions;
|
|||
|
||||
import io.cloudevents.CloudEvent;
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.impl.ExceptionFactoryImpl;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.ExpressionInternalVisitor;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
public abstract class BaseBinaryExpression extends BaseExpression {
|
||||
|
@ -18,14 +20,7 @@ public abstract class BaseBinaryExpression extends BaseExpression {
|
|||
this.rightOperand = rightOperand;
|
||||
}
|
||||
|
||||
public abstract Object evaluate(EvaluationRuntime runtime, Object left, Object right, ExceptionThrower exceptions);
|
||||
|
||||
@Override
|
||||
public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThrower thrower) {
|
||||
Object left = leftOperand.evaluate(runtime, event, thrower);
|
||||
Object right = rightOperand.evaluate(runtime, event, thrower);
|
||||
return evaluate(runtime, left, right, thrower);
|
||||
}
|
||||
public abstract EvaluationResult evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionFactory exceptionFactory);
|
||||
|
||||
@Override
|
||||
public <T> T visit(ExpressionInternalVisitor<T> visitor) {
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
package io.cloudevents.sql.impl.expressions;
|
||||
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.Type;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationContextImpl;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import io.cloudevents.sql.impl.runtime.TypeCastingProvider;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
public abstract class BaseExpression implements ExpressionInternal {
|
||||
|
@ -27,25 +28,25 @@ public abstract class BaseExpression implements ExpressionInternal {
|
|||
return this.expressionText;
|
||||
}
|
||||
|
||||
public Boolean castToBoolean(EvaluationRuntime runtime, ExceptionThrower exceptions, Object value) {
|
||||
return (Boolean) runtime.cast(
|
||||
new EvaluationContextImpl(expressionInterval(), expressionText(), exceptions),
|
||||
public EvaluationResult castToBoolean(ExceptionFactory exceptionFactory, EvaluationResult value) {
|
||||
return TypeCastingProvider.cast(
|
||||
new EvaluationContextImpl(expressionInterval(), expressionText(), exceptionFactory),
|
||||
value,
|
||||
Type.BOOLEAN
|
||||
);
|
||||
}
|
||||
|
||||
public Integer castToInteger(EvaluationRuntime runtime, ExceptionThrower exceptions, Object value) {
|
||||
return (Integer) runtime.cast(
|
||||
new EvaluationContextImpl(expressionInterval(), expressionText(), exceptions),
|
||||
public EvaluationResult castToInteger(ExceptionFactory exceptionFactory, EvaluationResult value) {
|
||||
return TypeCastingProvider.cast(
|
||||
new EvaluationContextImpl(expressionInterval(), expressionText(), exceptionFactory),
|
||||
value,
|
||||
Type.INTEGER
|
||||
);
|
||||
}
|
||||
|
||||
public String castToString(EvaluationRuntime runtime, ExceptionThrower exceptions, Object value) {
|
||||
return (String) runtime.cast(
|
||||
new EvaluationContextImpl(expressionInterval(), expressionText(), exceptions),
|
||||
public EvaluationResult castToString(ExceptionFactory exceptionFactory, EvaluationResult value) {
|
||||
return TypeCastingProvider.cast(
|
||||
new EvaluationContextImpl(expressionInterval(), expressionText(), exceptionFactory),
|
||||
value,
|
||||
Type.STRING
|
||||
);
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
package io.cloudevents.sql.impl.expressions;
|
||||
|
||||
import io.cloudevents.CloudEvent;
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.Type;
|
||||
import io.cloudevents.sql.impl.ExceptionFactoryImpl;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
public abstract class BaseIntegerBinaryExpression extends BaseBinaryExpression {
|
||||
|
@ -11,16 +15,26 @@ public abstract class BaseIntegerBinaryExpression extends BaseBinaryExpression {
|
|||
super(expressionInterval, expressionText, leftOperand, rightOperand);
|
||||
}
|
||||
|
||||
abstract Object evaluate(EvaluationRuntime runtime, int left, int right, ExceptionThrower exceptions);
|
||||
abstract EvaluationResult evaluate(EvaluationRuntime runtime, int left, int right, ExceptionFactory exceptionFactory);
|
||||
|
||||
@Override
|
||||
public Object evaluate(EvaluationRuntime runtime, Object left, Object right, ExceptionThrower exceptions) {
|
||||
public EvaluationResult evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionFactory exceptionFactory) {
|
||||
EvaluationResult left = this.getLeftOperand().evaluate(runtime, event, exceptionFactory);
|
||||
EvaluationResult right = this.getRightOperand().evaluate(runtime, event, exceptionFactory);
|
||||
|
||||
if (left.isMissingAttributeException() || right.isMissingAttributeException()) {
|
||||
return left.wrap(right).copyWithDefaultValueForType(Type.INTEGER);
|
||||
}
|
||||
|
||||
EvaluationResult x = castToInteger(exceptionFactory, left);
|
||||
EvaluationResult y = castToInteger(exceptionFactory, right);
|
||||
|
||||
return this.evaluate(
|
||||
runtime,
|
||||
castToInteger(runtime, exceptions, left).intValue(),
|
||||
castToInteger(runtime, exceptions, right).intValue(),
|
||||
exceptions
|
||||
);
|
||||
(Integer)x.value(),
|
||||
(Integer)y.value(),
|
||||
exceptionFactory
|
||||
).wrap(x).wrap(y);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,9 +2,12 @@ package io.cloudevents.sql.impl.expressions;
|
|||
|
||||
import io.cloudevents.CloudEvent;
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.Type;
|
||||
import io.cloudevents.sql.impl.ExceptionFactoryImpl;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.ExpressionInternalVisitor;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
public abstract class BaseUnaryExpression extends BaseExpression {
|
||||
|
@ -16,11 +19,17 @@ public abstract class BaseUnaryExpression extends BaseExpression {
|
|||
this.internal = internal;
|
||||
}
|
||||
|
||||
public abstract Object evaluate(EvaluationRuntime runtime, Object value, ExceptionThrower exceptions);
|
||||
public abstract EvaluationResult evaluate(EvaluationRuntime runtime, EvaluationResult result, ExceptionFactory exceptionFactory);
|
||||
|
||||
public abstract Type returnType();
|
||||
|
||||
@Override
|
||||
public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThrower thrower) {
|
||||
return evaluate(runtime, internal.evaluate(runtime, event, thrower), thrower);
|
||||
public EvaluationResult evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionFactory exceptionFactory) {
|
||||
EvaluationResult value = internal.evaluate(runtime, event, exceptionFactory);
|
||||
if (value.isMissingAttributeException()) {
|
||||
return value.copyWithDefaultValueForType(this.returnType());
|
||||
}
|
||||
return evaluate(runtime, value, exceptionFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -11,11 +11,26 @@ import io.cloudevents.sql.impl.runtime.TypeCastingProvider;
|
|||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class ComparisonExpression extends BaseBinaryExpression {
|
||||
public enum Comparison {
|
||||
EQUALS(Objects::equals),
|
||||
NOT_EQUALS((x, y) -> !Objects.equals(x, y));
|
||||
private final BiFunction<Object, Object, Boolean> fn;
|
||||
Comparison(BiFunction<Object, Object, Boolean> fn) {
|
||||
this.fn = fn;
|
||||
}
|
||||
boolean evaluate(Object a, Object b) {
|
||||
return this.fn.apply(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
public ComparisonExpression(Interval expressionInterval, String expressionText, ExpressionInternal leftOperand, ExpressionInternal rightOperand) {
|
||||
private final Comparison comparison;
|
||||
|
||||
public ComparisonExpression(Interval expressionInterval, String expressionText, ExpressionInternal leftOperand, ExpressionInternal rightOperand, Comparison comparison) {
|
||||
super(expressionInterval, expressionText, leftOperand, rightOperand);
|
||||
this.comparison = comparison;
|
||||
}
|
||||
|
||||
// x = y: Boolean x Boolean -> Boolean
|
||||
|
@ -36,6 +51,6 @@ public class ComparisonExpression extends BaseBinaryExpression {
|
|||
Type.fromValue(right.value())
|
||||
);
|
||||
|
||||
return new EvaluationResult(Objects.equals(left.value(), right.value()), null, left, right);
|
||||
return new EvaluationResult(this.comparison.evaluate(left.value(), right.value()), null, left, right);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package io.cloudevents.sql.impl.expressions;
|
||||
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.impl.ExceptionFactoryImpl;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
public class DifferenceExpression extends BaseIntegerBinaryExpression {
|
||||
|
@ -12,8 +14,8 @@ public class DifferenceExpression extends BaseIntegerBinaryExpression {
|
|||
}
|
||||
|
||||
@Override
|
||||
Object evaluate(EvaluationRuntime runtime, int left, int right, ExceptionThrower exceptions) {
|
||||
return left - right;
|
||||
EvaluationResult evaluate(EvaluationRuntime runtime, int left, int right, ExceptionFactory exceptions) {
|
||||
return new EvaluationResult(left - right);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package io.cloudevents.sql.impl.expressions;
|
||||
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionFactory;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
public class DivisionExpression extends BaseIntegerBinaryExpression {
|
||||
|
@ -13,14 +13,11 @@ public class DivisionExpression extends BaseIntegerBinaryExpression {
|
|||
}
|
||||
|
||||
@Override
|
||||
Object evaluate(EvaluationRuntime runtime, int left, int right, ExceptionThrower exceptions) {
|
||||
EvaluationResult evaluate(EvaluationRuntime runtime, int left, int right, ExceptionFactory exceptionFactory) {
|
||||
if (right == 0) {
|
||||
exceptions.throwException(
|
||||
ExceptionFactory.divisionByZero(expressionInterval(), expressionText(), left)
|
||||
);
|
||||
return 0;
|
||||
return new EvaluationResult(0, exceptionFactory.divisionByZero(expressionInterval(), expressionText(), left));
|
||||
}
|
||||
return left / right;
|
||||
return new EvaluationResult(left / right);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,8 +2,10 @@ package io.cloudevents.sql.impl.expressions;
|
|||
|
||||
import io.cloudevents.CloudEvent;
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.impl.ExceptionFactoryImpl;
|
||||
import io.cloudevents.sql.impl.ExpressionInternalVisitor;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
public class ExistsExpression extends BaseExpression {
|
||||
|
@ -16,8 +18,8 @@ public class ExistsExpression extends BaseExpression {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThrower thrower) {
|
||||
return hasContextAttribute(event, key);
|
||||
public EvaluationResult evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionFactory exceptionFactory) {
|
||||
return new EvaluationResult(hasContextAttribute(event, key));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
package io.cloudevents.sql.impl.expressions;
|
||||
|
||||
import io.cloudevents.CloudEvent;
|
||||
import io.cloudevents.sql.EvaluationContext;
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.Function;
|
||||
import io.cloudevents.sql.impl.ExceptionFactory;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.*;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.ExpressionInternalVisitor;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationContextImpl;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import io.cloudevents.sql.impl.runtime.TypeCastingProvider;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -26,26 +24,27 @@ public class FunctionInvocationExpression extends BaseExpression {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThrower thrower) {
|
||||
EvaluationContext context = new EvaluationContextImpl(expressionInterval(), expressionText(), thrower);
|
||||
public EvaluationResult evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionFactory exceptionFactory) {
|
||||
EvaluationContext context = new EvaluationContextImpl(expressionInterval(), expressionText(), exceptionFactory);
|
||||
|
||||
Function function;
|
||||
try {
|
||||
function = runtime.resolveFunction(functionName, arguments.size());
|
||||
} catch (Exception e) {
|
||||
thrower.throwException(
|
||||
ExceptionFactory.cannotDispatchFunction(expressionInterval(), expressionText(), functionName, e)
|
||||
);
|
||||
return "";
|
||||
return new EvaluationResult(false, exceptionFactory.cannotDispatchFunction(expressionInterval(), expressionText(), functionName, e));
|
||||
}
|
||||
|
||||
List<Object> computedArguments = new ArrayList<>(arguments.size());
|
||||
List<EvaluationException> exceptions = new ArrayList<>(); // used to accumulate any exceptions encountered while evaluating the arguments to the function
|
||||
for (int i = 0; i < arguments.size(); i++) {
|
||||
ExpressionInternal expr = arguments.get(i);
|
||||
Object computed = expr.evaluate(runtime, event, thrower);
|
||||
Object casted = runtime
|
||||
EvaluationResult computed = expr.evaluate(runtime, event, exceptionFactory);
|
||||
EvaluationResult casted = TypeCastingProvider
|
||||
.cast(context, computed, function.typeOfParameter(i));
|
||||
computedArguments.add(casted);
|
||||
if (casted.causes() != null) {
|
||||
exceptions.addAll(casted.causes());
|
||||
}
|
||||
computedArguments.add(casted.value());
|
||||
}
|
||||
|
||||
return function.invoke(
|
||||
|
@ -53,7 +52,7 @@ public class FunctionInvocationExpression extends BaseExpression {
|
|||
runtime,
|
||||
event,
|
||||
computedArguments
|
||||
);
|
||||
).wrap(exceptions);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -2,11 +2,13 @@ package io.cloudevents.sql.impl.expressions;
|
|||
|
||||
import io.cloudevents.CloudEvent;
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.Type;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.ExpressionInternalVisitor;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationContextImpl;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import io.cloudevents.sql.impl.runtime.TypeCastingProvider;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -26,18 +28,22 @@ public class InExpression extends BaseExpression {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThrower thrower) {
|
||||
Object leftValue = leftExpression.evaluate(runtime, event, thrower);
|
||||
return setExpressions.stream()
|
||||
.anyMatch(expr -> {
|
||||
Object rightValue = runtime.cast(
|
||||
new EvaluationContextImpl(expressionInterval(), expressionText(), thrower),
|
||||
expr.evaluate(runtime, event, thrower),
|
||||
Type.fromValue(leftValue)
|
||||
);
|
||||
public EvaluationResult evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionFactory exceptionFactory) {
|
||||
EvaluationResult leftValue = leftExpression.evaluate(runtime, event, exceptionFactory);
|
||||
for (ExpressionInternal setExpression : this.setExpressions) {
|
||||
EvaluationResult rightValue = TypeCastingProvider.cast(
|
||||
new EvaluationContextImpl(expressionInterval(), expressionText(), exceptionFactory),
|
||||
setExpression.evaluate(runtime, event, exceptionFactory),
|
||||
Type.fromValue(leftValue.value())
|
||||
);
|
||||
|
||||
return Objects.equals(leftValue, rightValue);
|
||||
});
|
||||
if (Objects.equals(leftValue.value(), rightValue.value())) {
|
||||
return new EvaluationResult(true, null, leftValue, rightValue);
|
||||
} else {
|
||||
leftValue.wrap(rightValue);
|
||||
}
|
||||
}
|
||||
return leftValue.copyWithValue(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
package io.cloudevents.sql.impl.expressions;
|
||||
|
||||
import io.cloudevents.CloudEvent;
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.Type;
|
||||
import io.cloudevents.sql.impl.ExceptionFactoryImpl;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
@ -34,11 +38,21 @@ public class IntegerComparisonBinaryExpression extends BaseBinaryExpression {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object evaluate(EvaluationRuntime runtime, Object left, Object right, ExceptionThrower exceptions) {
|
||||
return this.operation.evaluate(
|
||||
castToInteger(runtime, exceptions, left),
|
||||
castToInteger(runtime, exceptions, right)
|
||||
);
|
||||
public EvaluationResult evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionFactory exceptionFactory) {
|
||||
EvaluationResult left = this.getLeftOperand().evaluate(runtime, event, exceptionFactory);
|
||||
EvaluationResult right = this.getRightOperand().evaluate(runtime, event, exceptionFactory);
|
||||
|
||||
if (left.isMissingAttributeException() || right.isMissingAttributeException()) {
|
||||
return left.wrap(right).copyWithDefaultValueForType(Type.BOOLEAN);
|
||||
}
|
||||
|
||||
EvaluationResult x = castToInteger(exceptionFactory, left);
|
||||
EvaluationResult y = castToInteger(exceptionFactory, right);
|
||||
|
||||
return new EvaluationResult(this.operation.evaluate(
|
||||
(Integer)x.value(),
|
||||
(Integer)y.value()
|
||||
)).wrap(x).wrap(y);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,9 +2,11 @@ package io.cloudevents.sql.impl.expressions;
|
|||
|
||||
import io.cloudevents.CloudEvent;
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.impl.ExceptionFactoryImpl;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.ExpressionInternalVisitor;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
@ -22,14 +24,13 @@ public class LikeExpression extends BaseExpression {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThrower thrower) {
|
||||
String value = castToString(
|
||||
runtime,
|
||||
thrower,
|
||||
internal.evaluate(runtime, event, thrower)
|
||||
public EvaluationResult evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionFactory exceptionFactory) {
|
||||
EvaluationResult result = castToString(
|
||||
exceptionFactory,
|
||||
internal.evaluate(runtime, event, exceptionFactory)
|
||||
);
|
||||
|
||||
return pattern.matcher(value).matches();
|
||||
return result.copyWithValue(pattern.matcher((String) result.value()).matches());
|
||||
}
|
||||
|
||||
private Pattern convertLikePatternToRegex(String pattern) {
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package io.cloudevents.sql.impl.expressions;
|
||||
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionFactory;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
public class ModuleExpression extends BaseIntegerBinaryExpression {
|
||||
|
@ -13,14 +13,11 @@ public class ModuleExpression extends BaseIntegerBinaryExpression {
|
|||
}
|
||||
|
||||
@Override
|
||||
Object evaluate(EvaluationRuntime runtime, int left, int right, ExceptionThrower exceptions) {
|
||||
EvaluationResult evaluate(EvaluationRuntime runtime, int left, int right, ExceptionFactory exceptionFactory) {
|
||||
if (right == 0) {
|
||||
exceptions.throwException(
|
||||
ExceptionFactory.divisionByZero(expressionInterval(), expressionText(), left)
|
||||
);
|
||||
return 0;
|
||||
return new EvaluationResult(0, exceptionFactory.divisionByZero(expressionInterval(), expressionText(), left));
|
||||
}
|
||||
return left % right;
|
||||
return new EvaluationResult(left % right);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package io.cloudevents.sql.impl.expressions;
|
||||
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.impl.ExceptionFactoryImpl;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
public class MultiplicationExpression extends BaseIntegerBinaryExpression {
|
||||
|
@ -12,8 +14,8 @@ public class MultiplicationExpression extends BaseIntegerBinaryExpression {
|
|||
}
|
||||
|
||||
@Override
|
||||
Object evaluate(EvaluationRuntime runtime, int left, int right, ExceptionThrower exceptions) {
|
||||
return left * right;
|
||||
EvaluationResult evaluate(EvaluationRuntime runtime, int left, int right, ExceptionFactory exceptions) {
|
||||
return new EvaluationResult(left * right);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package io.cloudevents.sql.impl.expressions;
|
||||
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.Type;
|
||||
import io.cloudevents.sql.impl.ExceptionFactoryImpl;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
public class NegateExpression extends BaseUnaryExpression {
|
||||
|
@ -12,7 +15,13 @@ public class NegateExpression extends BaseUnaryExpression {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object evaluate(EvaluationRuntime runtime, Object value, ExceptionThrower exceptions) {
|
||||
return -castToInteger(runtime, exceptions, value);
|
||||
public Type returnType() {
|
||||
return Type.INTEGER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EvaluationResult evaluate(EvaluationRuntime runtime, EvaluationResult result, ExceptionFactory exceptions) {
|
||||
EvaluationResult x = castToInteger(exceptions, result);
|
||||
return x.copyWithValue(-(Integer)x.value());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package io.cloudevents.sql.impl.expressions;
|
||||
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.Type;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
public class NotExpression extends BaseUnaryExpression {
|
||||
|
@ -12,7 +14,13 @@ public class NotExpression extends BaseUnaryExpression {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object evaluate(EvaluationRuntime runtime, Object value, ExceptionThrower exceptions) {
|
||||
return !castToBoolean(runtime, exceptions, value);
|
||||
public Type returnType() {
|
||||
return Type.BOOLEAN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EvaluationResult evaluate(EvaluationRuntime runtime, EvaluationResult value, ExceptionFactory exceptions) {
|
||||
EvaluationResult x = castToBoolean(exceptions, value);
|
||||
return x.copyWithValue(!(Boolean)x.value());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package io.cloudevents.sql.impl.expressions;
|
||||
|
||||
import io.cloudevents.CloudEvent;
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.impl.ExceptionFactoryImpl;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
public class OrExpression extends BaseBinaryExpression {
|
||||
|
@ -12,12 +15,15 @@ public class OrExpression extends BaseBinaryExpression {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object evaluate(EvaluationRuntime runtime, Object left, Object right, ExceptionThrower exceptions) {
|
||||
boolean x = castToBoolean(runtime, exceptions, left);
|
||||
if (x) {
|
||||
public EvaluationResult evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionFactory exceptions) {
|
||||
EvaluationResult left = this.getLeftOperand().evaluate(runtime, event, exceptions);
|
||||
EvaluationResult x = castToBoolean(exceptions, left);
|
||||
if ((Boolean)x.value()) {
|
||||
// Short circuit
|
||||
return true;
|
||||
return x;
|
||||
}
|
||||
return castToBoolean(runtime, exceptions, right);
|
||||
|
||||
EvaluationResult right = this.getRightOperand().evaluate(runtime, event, exceptions);
|
||||
return castToBoolean(exceptions, right);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package io.cloudevents.sql.impl.expressions;
|
||||
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.impl.ExceptionFactoryImpl;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
public class SumExpression extends BaseIntegerBinaryExpression {
|
||||
|
@ -12,8 +14,8 @@ public class SumExpression extends BaseIntegerBinaryExpression {
|
|||
}
|
||||
|
||||
@Override
|
||||
Object evaluate(EvaluationRuntime runtime, int left, int right, ExceptionThrower exceptions) {
|
||||
return left + right;
|
||||
EvaluationResult evaluate(EvaluationRuntime runtime, int left, int right, ExceptionFactory exceptions) {
|
||||
return new EvaluationResult(left + right);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,9 +2,11 @@ package io.cloudevents.sql.impl.expressions;
|
|||
|
||||
import io.cloudevents.CloudEvent;
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.impl.ExceptionFactoryImpl;
|
||||
import io.cloudevents.sql.impl.ExpressionInternalVisitor;
|
||||
import io.cloudevents.sql.impl.parser.LiteralUtils;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||
|
||||
|
@ -18,8 +20,8 @@ public class ValueExpression extends BaseExpression {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThrower thrower) {
|
||||
return value;
|
||||
public EvaluationResult evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionFactory thrower) {
|
||||
return new EvaluationResult(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
package io.cloudevents.sql.impl.expressions;
|
||||
|
||||
import io.cloudevents.CloudEvent;
|
||||
import io.cloudevents.sql.EvaluationRuntime;
|
||||
import io.cloudevents.sql.impl.ExceptionThrower;
|
||||
import io.cloudevents.sql.ExceptionFactory;
|
||||
import io.cloudevents.sql.Type;
|
||||
import io.cloudevents.sql.impl.ExceptionFactoryImpl;
|
||||
import io.cloudevents.sql.impl.ExpressionInternal;
|
||||
import io.cloudevents.sql.impl.runtime.EvaluationResult;
|
||||
import org.antlr.v4.runtime.misc.Interval;
|
||||
|
||||
public class XorExpression extends BaseBinaryExpression {
|
||||
|
@ -12,10 +16,15 @@ public class XorExpression extends BaseBinaryExpression {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object evaluate(EvaluationRuntime runtime, Object left, Object right, ExceptionThrower exceptions) {
|
||||
return Boolean.logicalXor(
|
||||
castToBoolean(runtime, exceptions, left),
|
||||
castToBoolean(runtime, exceptions, right)
|
||||
);
|
||||
public EvaluationResult evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionFactory exceptions) {
|
||||
EvaluationResult left = this.getLeftOperand().evaluate(runtime, event, exceptions);
|
||||
EvaluationResult right = this.getRightOperand().evaluate(runtime, event, exceptions);
|
||||
|
||||
EvaluationResult x = castToBoolean(exceptions, left);
|
||||
EvaluationResult y = castToBoolean(exceptions, right);
|
||||
return new EvaluationResult(Boolean.logicalXor(
|
||||
(Boolean)x.value(),
|
||||
(Boolean)y.value()
|
||||
)).wrap(x).wrap(y);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue