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
4 changes: 2 additions & 2 deletions api/src/main/java/io/cloudevents/rw/CloudEventDataMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
@FunctionalInterface
@ParametersAreNonnullByDefault
public interface CloudEventDataMapper {
public interface CloudEventDataMapper<R extends CloudEventData> {

/**
* Map {@code data} to another {@link CloudEventData} instance.
Expand All @@ -35,6 +35,6 @@ public interface CloudEventDataMapper {
* @return The new data
* @throws CloudEventRWException is anything goes wrong while mapping the input data
*/
CloudEventData map(CloudEventData data) throws CloudEventRWException;
R map(CloudEventData data) throws CloudEventRWException;

}
15 changes: 15 additions & 0 deletions core/src/main/java/io/cloudevents/core/impl/CloudEventUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
package io.cloudevents.core.impl;

import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData;
import io.cloudevents.lang.Nullable;
import io.cloudevents.rw.CloudEventDataMapper;
import io.cloudevents.rw.CloudEventReader;

public final class CloudEventUtils {
Expand All @@ -39,4 +42,16 @@ public static CloudEventReader toVisitable(CloudEvent event) {
}
}

/**
* Get the data contained in {@code event} and map it using the provided mapper.
*/
@Nullable
public static <R extends CloudEventData> R mapData(CloudEvent event, CloudEventDataMapper<R> mapper) {
CloudEventData data = event.getData();
if (data == null) {
return null;
}
return mapper.map(data);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.cloudevents.core.message;

import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData;
import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.lang.Nullable;
import io.cloudevents.rw.*;
Expand Down Expand Up @@ -116,7 +117,7 @@ default CloudEvent toEvent() throws CloudEventRWException, IllegalStateException
* @throws CloudEventRWException if something went wrong during the visit.
* @throws IllegalStateException if the message has an unknown encoding.
*/
default CloudEvent toEvent(@Nullable CloudEventDataMapper mapper) throws CloudEventRWException, IllegalStateException {
default CloudEvent toEvent(@Nullable CloudEventDataMapper<? extends CloudEventData> mapper) throws CloudEventRWException, IllegalStateException {
switch (getEncoding()) {
case BINARY:
return this.read(CloudEventBuilder::fromSpecVersion, mapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.cloudevents.core.message;

import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData;
import io.cloudevents.core.format.EventFormat;
import io.cloudevents.core.message.impl.GenericStructuredMessageReader;
import io.cloudevents.lang.Nullable;
Expand All @@ -44,7 +45,7 @@ default CloudEvent toEvent() throws CloudEventRWException, IllegalStateException
return this.read(EventFormat::deserialize);
}

default CloudEvent toEvent(@Nullable CloudEventDataMapper mapper) throws CloudEventRWException, IllegalStateException {
default CloudEvent toEvent(@Nullable CloudEventDataMapper<? extends CloudEventData> mapper) throws CloudEventRWException, IllegalStateException {
return this.read((format, value) -> format.deserialize(value, mapper));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.cloudevents.core.impl;

import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData;
import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.core.mock.MyCloudEventData;
import io.cloudevents.rw.CloudEventDataMapper;
import org.junit.jupiter.api.Test;

import java.net.URI;
import java.util.concurrent.atomic.AtomicInteger;

import static org.assertj.core.api.Assertions.assertThat;

class CloudEventUtilsTest {

@Test
void mapDataWithNullData() {
AtomicInteger i = new AtomicInteger();
CloudEventDataMapper<CloudEventData> mapper = data -> {
i.incrementAndGet();
return data;
};

CloudEvent cloudEvent = CloudEventBuilder.v1()
.withId("aaa")
.withSource(URI.create("localhost"))
.withType("bbb")
.build();

assertThat(CloudEventUtils.mapData(cloudEvent, mapper))
.isNull();
assertThat(i)
.hasValue(0);
}

@Test
void mapData() {
AtomicInteger i = new AtomicInteger();
CloudEventDataMapper<CloudEventData> mapper = data -> {
i.incrementAndGet();
return data;
};

MyCloudEventData data = new MyCloudEventData(10);

CloudEvent cloudEvent = CloudEventBuilder.v1()
.withId("aaa")
.withSource(URI.create("localhost"))
.withType("bbb")
.withData(data)
.build();

assertThat(CloudEventUtils.mapData(cloudEvent, mapper))
.isEqualTo(data);
assertThat(i)
.hasValue(1);
}
}