Skip to content

Commit c41a2c3

Browse files
Cleanup CloudEventUtils (#398)
Signed-off-by: Francesco Guardiani <[email protected]>
1 parent 3651cda commit c41a2c3

File tree

3 files changed

+41
-57
lines changed

3 files changed

+41
-57
lines changed

sql/src/main/java/io/cloudevents/sql/impl/CloudEventUtils.java

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,60 @@
11
package io.cloudevents.sql.impl.expressions;
22

33
import io.cloudevents.CloudEvent;
4+
import io.cloudevents.SpecVersion;
5+
import io.cloudevents.sql.EvaluationException;
46
import io.cloudevents.sql.EvaluationRuntime;
5-
import io.cloudevents.sql.impl.CloudEventUtils;
67
import io.cloudevents.sql.impl.ExceptionThrower;
78
import org.antlr.v4.runtime.misc.Interval;
89

10+
import java.util.Base64;
11+
import java.util.Objects;
12+
import java.util.function.Function;
13+
914
public class AccessAttributeExpression extends BaseExpression {
1015

1116
private final String key;
17+
private final Function<CloudEvent, Object> getter;
1218

1319
public AccessAttributeExpression(Interval expressionInterval, String expressionText, String key) {
1420
super(expressionInterval, expressionText);
15-
this.key = key.toLowerCase();
21+
this.key = key;
22+
this.getter = generateGetter(key);
1623
}
1724

1825
@Override
1926
public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThrower thrower) {
20-
return CloudEventUtils.accessContextAttribute(thrower, expressionInterval(), expressionText(), event, key);
27+
Object value = this.getter.apply(event);
28+
if (value == null) {
29+
thrower.throwException(
30+
EvaluationException.missingAttribute(this.expressionInterval(), this.expressionText(), key)
31+
);
32+
return "";
33+
}
34+
35+
// Because the CESQL type system is smaller than the CE type system,
36+
// we need to coherce some values to string
37+
return coherceTypes(value);
2138
}
2239

2340
@Override
2441
public <T> T visit(ExpressionInternalVisitor<T> visitor) {
2542
return visitor.visitAccessAttributeExpression(this);
2643
}
2744

45+
private static Function<CloudEvent, Object> generateGetter(String key) {
46+
return SpecVersion.V1.getAllAttributes().contains(key) ? ce -> ce.getAttribute(key) : ce -> ce.getExtension(key);
47+
}
48+
49+
private static Object coherceTypes(Object value) {
50+
if (value instanceof Boolean || value instanceof String || value instanceof Integer) {
51+
// No casting required
52+
return value;
53+
}
54+
if (value instanceof byte[]) {
55+
return Base64.getEncoder().encodeToString((byte[]) value);
56+
}
57+
return Objects.toString(value);
58+
}
59+
2860
}

sql/src/main/java/io/cloudevents/sql/impl/expressions/ExistsExpression.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import io.cloudevents.CloudEvent;
44
import io.cloudevents.sql.EvaluationRuntime;
5-
import io.cloudevents.sql.impl.CloudEventUtils;
65
import io.cloudevents.sql.impl.ExceptionThrower;
76
import org.antlr.v4.runtime.misc.Interval;
87

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

1817
@Override
1918
public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThrower thrower) {
20-
return CloudEventUtils.hasContextAttribute(event, key);
19+
return hasContextAttribute(event, key);
2120
}
2221

2322
@Override
@@ -28,4 +27,9 @@ public <T> T visit(ExpressionInternalVisitor<T> visitor) {
2827
public String getKey() {
2928
return key;
3029
}
30+
31+
private static boolean hasContextAttribute(CloudEvent event, String key) {
32+
return event.getAttributeNames().contains(key) || event.getExtensionNames().contains(key);
33+
}
34+
3135
}

0 commit comments

Comments
 (0)