Skip to content

Reworked a bit the Extension interface #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 1, 2020
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
33 changes: 30 additions & 3 deletions api/src/main/java/io/cloudevents/Extension.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,39 @@

package io.cloudevents;

import java.util.Map;
import io.cloudevents.lang.Nullable;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Set;

/**
* Materialized CloudEvent Extension interface to read/write the extension attributes key/values.
*/
@ParametersAreNonnullByDefault
public interface Extension {

void readFromEvent(CloudEvent event);
/**
* Fill this materialized extension with values from a {@link CloudEventExtensions} implementation
*
* @param extensions
*/
void readFrom(CloudEventExtensions extensions);

/**
* Get the attribute of extension named {@code key}
*
* @param key the name of the extension attribute
* @return the extension value
* @throws IllegalArgumentException if the key is unknown to this extension
*/
@Nullable
Object getValue(String key) throws IllegalArgumentException;

Map<String, Object> asMap();
/**
* Get the set of possible extension attribute keys
*
* @return set of possible extension attribute keys
*/
Set<String> getKeys();

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@

package io.cloudevents.core.extensions;

import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventExtensions;
import io.cloudevents.Extension;
import io.cloudevents.core.extensions.impl.ExtensionUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.HashSet;
import java.util.Set;

public final class DistributedTracingExtension implements Extension {

public static final String TRACEPARENT = "traceparent";
public static final String TRACESTATE = "tracestate";
private static final Set<String> KEY_SET = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(TRACEPARENT, TRACESTATE)));

private String traceparent;
private String tracestate;
Expand All @@ -49,23 +52,31 @@ public void setTracestate(String tracestate) {
}

@Override
public void readFromEvent(CloudEvent event) {
Object tp = event.getExtension(TRACEPARENT);
public void readFrom(CloudEventExtensions extensions) {
Object tp = extensions.getExtension(TRACEPARENT);
if (tp != null) {
this.traceparent = tp.toString();
}
Object ts = event.getExtension(TRACESTATE);
Object ts = extensions.getExtension(TRACESTATE);
if (ts != null) {
this.tracestate = ts.toString();
}
}

@Override
public Map<String, Object> asMap() {
HashMap<String, Object> map = new HashMap<>();
map.put(TRACEPARENT, this.traceparent);
map.put(TRACESTATE, this.tracestate);
return Collections.unmodifiableMap(map);
public Object getValue(String key) {
switch (key) {
case TRACEPARENT:
return this.traceparent;
case TRACESTATE:
return this.tracestate;
}
throw ExtensionUtils.generateInvalidKeyException(this.getClass().getSimpleName(), key);
}

@Override
public Set<String> getKeys() {
return KEY_SET;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public <T extends Extension> T parseExtension(Class<T> extensionClass, CloudEven
Supplier<Extension> factory = extensionFactories.get(extensionClass);
if (factory != null) {
Extension ext = factory.get();
ext.readFromEvent(event);
ext.readFrom(event);
return (T) ext;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2018-Present The CloudEvents Authors
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package io.cloudevents.core.extensions.impl;

public final class ExtensionUtils {

private ExtensionUtils() {
}

public static IllegalArgumentException generateInvalidKeyException(String extensionName, String key) {
return new IllegalArgumentException(extensionName + " doesn't expect the attribute key \"" + key + "\"");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ public SELF withExtension(String key, boolean value) {
}

public SELF withExtension(Extension extension) {
this.extensions.putAll(extension.asMap());
for (String key : extension.getKeys()) {
Object value = extension.getValue(key);
if (value != null) {
this.extensions.put(key, value);
}
}
return self;
}

Expand Down