8000 Cesql v1 fixes by Cali0707 · Pull Request #641 · cloudevents/sdk-java · GitHub
[go: up one dir, main page]

Skip to content

Cesql v1 fixes #641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: expressions use results instead of a thrower interface
Signed-off-by: Calum Murray <cmurray@redhat.com>
  • Loading branch information
Cali0707 committed Jun 19, 2024
commit 762b6658495a658d2e9821230ebb76185b4d2cbd
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
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;
Expand All @@ -24,18 +24,15 @@ public AccessAttributeExpression(Interval expressionInterval, String expressionT
}

@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
Expand Down
8000
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -12,12 +15,15 @@ public AndExpression(Interval expressionInterval, String expressionText, Express
}

@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

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 {
Expand All @@ -18,14 +20,7 @@ protected BaseBinaryExpression(Interval expressionInterval, String expressionTex
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -27,25 +28,25 @@ public String expressionText() {
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
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -11,16 +15,26 @@ public BaseIntegerBinaryExpression(Interval expressionInterval, String expressio
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

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 {
Expand All @@ -16,11 +19,17 @@ public BaseUnaryExpression(Interval expressionInterval, String expressionText, E
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,26 @@
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);
}
}

private final Comparison comparison;

public ComparisonExpression(Interval expressionInterval, String expressionText, ExpressionInternal leftOperand, ExpressionInternal rightOperand) {
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
Expand All @@ -36,6 +51,6 @@ public EvaluationResult evaluate(EvaluationRuntime runtime, CloudEvent event, Ex
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);
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -12,8 +14,8 @@ public DifferenceExpression(Interval expressionInterval, String expressionText,
}

@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);
}

}
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -13,14 +13,11 @@ public DivisionExpression(Interval expressionInterval, String expressionText, Ex
}

@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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

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 {
Expand All @@ -16,8 +18,8 @@ public ExistsExpression(Interval expressionInterval, String expressionText, Stri
}

@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
Expand Down
Loading
0