Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
52 changes: 0 additions & 52 deletions sql/src/main/java/io/cloudevents/sql/impl/CloudEventUtils.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,60 @@
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
public <T> T visit(ExpressionInternalVisitor<T> visitor) {
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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;

Expand All @@ -17,7 +16,7 @@ public ExistsExpression(Interval expressionInterval, String expressionText, Stri

@Override
public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThrower thrower) {
return CloudEventUtils.hasContextAttribute(event, key);
return hasContextAttribute(event, key);
}

@Override
Expand All @@ -28,4 +27,9 @@ public <T> T visit(ExpressionInternalVisitor<T> visitor) {
public String getKey() {
return key;
}

private static boolean hasContextAttribute(CloudEvent event, String key) {
return event.getAttributeNames().contains(key) || event.getExtensionNames().contains(key);
}

}