Cleanup CloudEventUtils (#398)
Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
This commit is contained in:
parent
3651cdae18
commit
c41a2c3ba7
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<CloudEvent, Object> 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<CloudEvent, Object> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue