Skip to content

Commit

Permalink
[ISSUE cloudevents#560]Optimize BaseCloudEvent#readExtensions method
Browse files Browse the repository at this point in the history
Signed-off-by: mxsm <ljbmxsm@gmail.com>
  • Loading branch information
mxsm committed Apr 29, 2023
1 parent 4ebeab0 commit d174d8d
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions core/src/main/java/io/cloudevents/core/impl/BaseCloudEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,14 @@ public <T extends CloudEventWriter<V>, V> V read(CloudEventWriterFactory<T, V> w
}

protected void readExtensions(CloudEventContextWriter writer) throws CloudEventRWException {
// TODO to be improved
for (Map.Entry<String, Object> entry : this.extensions.entrySet()) {
if (entry.getValue() instanceof String) {
writer.withContextAttribute(entry.getKey(), (String) entry.getValue());
} else if (entry.getValue() instanceof Number) {
writer.withContextAttribute(entry.getKey(), (Number) entry.getValue());
} else if (entry.getValue() instanceof Boolean) {
writer.withContextAttribute(entry.getKey(), (Boolean) entry.getValue());
} else if (entry.getValue() instanceof URI) {
writer.withContextAttribute(entry.getKey(), (URI) entry.getValue());
} else if (entry.getValue() instanceof OffsetDateTime) {
writer.withContextAttribute(entry.getKey(), (OffsetDateTime) entry.getValue());
} else if (entry.getValue() instanceof byte[]) {
writer.withContextAttribute(entry.getKey(), (byte[]) entry.getValue());
Object value = entry.getValue();
if (value instanceof String || value instanceof Number || value instanceof Boolean || value instanceof URI) {
writer.withContextAttribute(entry.getKey(), value.toString());
} else if (value instanceof OffsetDateTime) {
writer.withContextAttribute(entry.getKey(), (OffsetDateTime) value);
} else if (value instanceof byte[]) {
writer.withContextAttribute(entry.getKey(), (byte[]) value);
} else {
// This should never happen because we build that map only through our builders
throw new IllegalStateException("Illegal value inside extensions map: " + entry);
Expand Down

0 comments on commit d174d8d

Please sign in to comment.