From c41a2c3ba7868b1f3981065c62fda9508fc4a1f6 Mon Sep 17 00:00:00 2001 From: Francesco Guardiani Date: Mon, 14 Jun 2021 16:30:07 +0200 Subject: [PATCH] Cleanup CloudEventUtils (#398) Signed-off-by: Francesco Guardiani --- .../cloudevents/sql/impl/CloudEventUtils.java | 52 ------------------- .../AccessAttributeExpression.java | 38 ++++++++++++-- .../impl/expressions/ExistsExpression.java | 8 ++- 3 files changed, 41 insertions(+), 57 deletions(-) delete mode 100644 sql/src/main/java/io/cloudevents/sql/impl/CloudEventUtils.java diff --git a/sql/src/main/java/io/cloudevents/sql/impl/CloudEventUtils.java b/sql/src/main/java/io/cloudevents/sql/impl/CloudEventUtils.java deleted file mode 100644 index 74b3a4d9..00000000 --- a/sql/src/main/java/io/cloudevents/sql/impl/CloudEventUtils.java +++ /dev/null @@ -1,52 +0,0 @@ -package io.cloudevents.sql.impl; - -import io.cloudevents.CloudEvent; -import io.cloudevents.sql.EvaluationException; -import org.antlr.v4.runtime.misc.Interval; - -import java.util.Base64; -import java.util.Objects; - -public final class CloudEventUtils { - - private CloudEventUtils() { - } - - public static boolean hasContextAttribute(CloudEvent event, String key) { - return event.getAttributeNames().contains(key) || event.getExtensionNames().contains(key); - } - - public static Object accessContextAttribute(ExceptionThrower exceptions, Interval interval, String expression, CloudEvent event, String key) { - // TODO do we have a better solution to access attributes here? - Object value; - try { - value = event.getAttribute(key); - } catch (IllegalArgumentException e) { - value = event.getExtension(key); - } - if (value == null) { - exceptions.throwException( - EvaluationException.missingAttribute(interval, expression, key) - ); - value = ""; - } else { - // Because the CESQL type system is smaller than the CE type system, - // we need to coherce some values to string - value = coherceTypes(value); - } - - return value; - } - - static Object coherceTypes(Object value) { - if (value instanceof Boolean || value instanceof String || value instanceof Integer) { - // No casting required - return value; - } - if (value instanceof byte[]) { - return Base64.getEncoder().encodeToString((byte[]) value); - } - return Objects.toString(value); - } - -} diff --git a/sql/src/main/java/io/cloudevents/sql/impl/expressions/AccessAttributeExpression.java b/sql/src/main/java/io/cloudevents/sql/impl/expressions/AccessAttributeExpression.java index d81b9625..ed496bff 100644 --- a/sql/src/main/java/io/cloudevents/sql/impl/expressions/AccessAttributeExpression.java +++ b/sql/src/main/java/io/cloudevents/sql/impl/expressions/AccessAttributeExpression.java @@ -1,23 +1,40 @@ package io.cloudevents.sql.impl.expressions; import io.cloudevents.CloudEvent; +import io.cloudevents.SpecVersion; +import io.cloudevents.sql.EvaluationException; import io.cloudevents.sql.EvaluationRuntime; -import io.cloudevents.sql.impl.CloudEventUtils; import io.cloudevents.sql.impl.ExceptionThrower; import org.antlr.v4.runtime.misc.Interval; +import java.util.Base64; +import java.util.Objects; +import java.util.function.Function; + public class AccessAttributeExpression extends BaseExpression { private final String key; + private final Function getter; public AccessAttributeExpression(Interval expressionInterval, String expressionText, String key) { super(expressionInterval, expressionText); - this.key = key.toLowerCase(); + this.key = key; + this.getter = generateGetter(key); } @Override public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThrower thrower) { - return CloudEventUtils.accessContextAttribute(thrower, expressionInterval(), expressionText(), event, key); + Object value = this.getter.apply(event); + if (value == null) { + thrower.throwException( + EvaluationException.missingAttribute(this.expressionInterval(), this.expressionText(), key) + ); + return ""; + } + + // Because the CESQL type system is smaller than the CE type system, + // we need to coherce some values to string + return coherceTypes(value); } @Override @@ -25,4 +42,19 @@ public class AccessAttributeExpression extends BaseExpression { return visitor.visitAccessAttributeExpression(this); } + private static Function generateGetter(String key) { + return SpecVersion.V1.getAllAttributes().contains(key) ? ce -> ce.getAttribute(key) : ce -> ce.getExtension(key); + } + + private static Object coherceTypes(Object value) { + if (value instanceof Boolean || value instanceof String || value instanceof Integer) { + // No casting required + return value; + } + if (value instanceof byte[]) { + return Base64.getEncoder().encodeToString((byte[]) value); + } + return Objects.toString(value); + } + } diff --git a/sql/src/main/java/io/cloudevents/sql/impl/expressions/ExistsExpression.java b/sql/src/main/java/io/cloudevents/sql/impl/expressions/ExistsExpression.java index 7c514288..648dbb80 100644 --- a/sql/src/main/java/io/cloudevents/sql/impl/expressions/ExistsExpression.java +++ b/sql/src/main/java/io/cloudevents/sql/impl/expressions/ExistsExpression.java @@ -2,7 +2,6 @@ package io.cloudevents.sql.impl.expressions; import io.cloudevents.CloudEvent; import io.cloudevents.sql.EvaluationRuntime; -import io.cloudevents.sql.impl.CloudEventUtils; import io.cloudevents.sql.impl.ExceptionThrower; import org.antlr.v4.runtime.misc.Interval; @@ -17,7 +16,7 @@ public class ExistsExpression extends BaseExpression { @Override public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThrower thrower) { - return CloudEventUtils.hasContextAttribute(event, key); + return hasContextAttribute(event, key); } @Override @@ -28,4 +27,9 @@ public class ExistsExpression extends BaseExpression { public String getKey() { return key; } + + private static boolean hasContextAttribute(CloudEvent event, String key) { + return event.getAttributeNames().contains(key) || event.getExtensionNames().contains(key); + } + }