Moved CloudEventUtils from impl to io.cloudevents.core (#261)

Renamed CloudEventUtils#toVisitable to CloudEventUtils#toReader
Added CloudEventUtils#toEvent

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
This commit is contained in:
Francesco Guardiani 2020-11-13 10:00:49 +01:00 committed by GitHub
parent 7696ffe4ec
commit 34408236db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 45 additions and 17 deletions

View File

@ -15,29 +15,36 @@
* *
*/ */
package io.cloudevents.core.impl; package io.cloudevents.core;
import io.cloudevents.CloudEvent; import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData; import io.cloudevents.CloudEventData;
import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.core.impl.CloudEventReaderAdapter;
import io.cloudevents.lang.Nullable; import io.cloudevents.lang.Nullable;
import io.cloudevents.rw.CloudEventContextReader; import io.cloudevents.rw.CloudEventContextReader;
import io.cloudevents.rw.CloudEventDataMapper; import io.cloudevents.rw.CloudEventDataMapper;
import io.cloudevents.rw.CloudEventRWException;
import io.cloudevents.rw.CloudEventReader; import io.cloudevents.rw.CloudEventReader;
/**
* This class contains a set of utility methods to deal with conversions of io.cloudevents related interfaces
*/
public final class CloudEventUtils { public final class CloudEventUtils {
private CloudEventUtils() {} private CloudEventUtils() {}
/** /**
* Convert a {@link CloudEvent} to a {@link CloudEventReader}. This method provides a default implementation * Convert a {@link CloudEvent} to a {@link CloudEventReader}. This method provides a default implementation
* for CloudEvent that doesn't implement {@link CloudEventReader}
* for CloudEvent that doesn't implement CloudEventVisitable. * for CloudEvent that doesn't implement CloudEventVisitable.
* <p> * <p>
* It's safe to use the returned {@link CloudEventReader} multiple times. * It's safe to use the returned {@link CloudEventReader} multiple times.
* *
* @param event the event to convert * @param event the event to convert
* @return the visitable implementation * @return the reader implementation
*/ */
public static CloudEventReader toVisitable(CloudEvent event) { public static CloudEventReader toReader(CloudEvent event) {
if (event instanceof CloudEventReader) { if (event instanceof CloudEventReader) {
return (CloudEventReader) event; return (CloudEventReader) event;
} else { } else {
@ -62,6 +69,27 @@ public final class CloudEventUtils {
} }
} }
/**
* Convert a {@link CloudEventReader} to a {@link CloudEvent}.
*
* @param reader the reader where to read the message from
* @return the reader implementation
*/
public static CloudEvent toEvent(CloudEventReader reader) throws CloudEventRWException {
return toEvent(reader, null);
}
/**
* Convert a {@link CloudEventReader} to a {@link CloudEvent} mapping the data with the provided {@code mapper}.
*
* @param reader the reader where to read the message from
* @param mapper the mapper to use when reading the data
* @return the reader implementation
*/
public static CloudEvent toEvent(CloudEventReader reader, @Nullable CloudEventDataMapper<?> mapper) throws CloudEventRWException {
return reader.read(CloudEventBuilder::fromSpecVersion, mapper);
}
/** /**
* Get the data contained in {@code event} and map it using the provided mapper. * Get the data contained in {@code event} and map it using the provided mapper.
*/ */

View File

@ -24,7 +24,7 @@ public class CloudEventReaderAdapter implements CloudEventReader, CloudEventCont
private final CloudEvent event; private final CloudEvent event;
CloudEventReaderAdapter(CloudEvent event) { public CloudEventReaderAdapter(CloudEvent event) {
this.event = event; this.event = event;
} }

View File

@ -19,7 +19,7 @@ package io.cloudevents.core.message;
import io.cloudevents.CloudEvent; import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData; import io.cloudevents.CloudEventData;
import io.cloudevents.core.builder.CloudEventBuilder; import io.cloudevents.core.CloudEventUtils;
import io.cloudevents.lang.Nullable; import io.cloudevents.lang.Nullable;
import io.cloudevents.rw.*; import io.cloudevents.rw.*;
@ -102,7 +102,7 @@ public interface MessageReader extends StructuredMessageReader, CloudEventReader
default CloudEvent toEvent(@Nullable CloudEventDataMapper<? extends CloudEventData> mapper) throws CloudEventRWException, IllegalStateException { default CloudEvent toEvent(@Nullable CloudEventDataMapper<? extends CloudEventData> mapper) throws CloudEventRWException, IllegalStateException {
switch (getEncoding()) { switch (getEncoding()) {
case BINARY: case BINARY:
return this.read(CloudEventBuilder::fromSpecVersion, mapper); return CloudEventUtils.toEvent(this, mapper);
case STRUCTURED: case STRUCTURED:
return this.read((format, value) -> format.deserialize(value, mapper)); return this.read((format, value) -> format.deserialize(value, mapper));
default: default:

View File

@ -18,8 +18,8 @@
package io.cloudevents.core.message; package io.cloudevents.core.message;
import io.cloudevents.CloudEvent; import io.cloudevents.CloudEvent;
import io.cloudevents.core.CloudEventUtils;
import io.cloudevents.core.format.EventFormat; import io.cloudevents.core.format.EventFormat;
import io.cloudevents.core.impl.CloudEventUtils;
import io.cloudevents.core.message.impl.GenericStructuredMessageReader; import io.cloudevents.core.message.impl.GenericStructuredMessageReader;
import io.cloudevents.rw.CloudEventWriter; import io.cloudevents.rw.CloudEventWriter;
import io.cloudevents.rw.CloudEventWriterFactory; import io.cloudevents.rw.CloudEventWriterFactory;
@ -68,7 +68,7 @@ public interface MessageWriter<CEV extends CloudEventWriter<R>, R> extends Cloud
* @return return value at the end of the write process. * @return return value at the end of the write process.
*/ */
default R writeBinary(CloudEvent event) { default R writeBinary(CloudEvent event) {
return CloudEventUtils.toVisitable(event).read(this); return CloudEventUtils.toReader(event).read(this);
} }
} }

View File

@ -17,8 +17,8 @@
package io.cloudevents.core.v03; package io.cloudevents.core.v03;
import io.cloudevents.SpecVersion; import io.cloudevents.SpecVersion;
import io.cloudevents.core.CloudEventUtils;
import io.cloudevents.core.impl.BaseCloudEventBuilder; import io.cloudevents.core.impl.BaseCloudEventBuilder;
import io.cloudevents.core.impl.CloudEventUtils;
import io.cloudevents.rw.CloudEventRWException; import io.cloudevents.rw.CloudEventRWException;
import io.cloudevents.types.Time; import io.cloudevents.types.Time;

View File

@ -19,8 +19,8 @@ package io.cloudevents.core.v1;
import io.cloudevents.CloudEvent; import io.cloudevents.CloudEvent;
import io.cloudevents.SpecVersion; import io.cloudevents.SpecVersion;
import io.cloudevents.core.CloudEventUtils;
import io.cloudevents.core.impl.BaseCloudEventBuilder; import io.cloudevents.core.impl.BaseCloudEventBuilder;
import io.cloudevents.core.impl.CloudEventUtils;
import io.cloudevents.rw.CloudEventRWException; import io.cloudevents.rw.CloudEventRWException;
import io.cloudevents.types.Time; import io.cloudevents.types.Time;

View File

@ -1,4 +1,4 @@
package io.cloudevents.core.impl; package io.cloudevents.core;
import io.cloudevents.CloudEvent; import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData; import io.cloudevents.CloudEventData;

View File

@ -20,8 +20,8 @@ package io.cloudevents.core.mock;
import io.cloudevents.CloudEvent; import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData; import io.cloudevents.CloudEventData;
import io.cloudevents.SpecVersion; import io.cloudevents.SpecVersion;
import io.cloudevents.core.CloudEventUtils;
import io.cloudevents.core.data.BytesCloudEventData; import io.cloudevents.core.data.BytesCloudEventData;
import io.cloudevents.core.impl.CloudEventUtils;
import io.cloudevents.core.message.MessageReader; import io.cloudevents.core.message.MessageReader;
import io.cloudevents.core.message.impl.BaseBinaryMessageReader; import io.cloudevents.core.message.impl.BaseBinaryMessageReader;
import io.cloudevents.rw.*; import io.cloudevents.rw.*;
@ -57,7 +57,7 @@ public class MockBinaryMessageWriter extends BaseBinaryMessageReader implements
public MockBinaryMessageWriter(CloudEvent event) { public MockBinaryMessageWriter(CloudEvent event) {
this(); this();
CloudEventUtils CloudEventUtils
.toVisitable(event) .toReader(event)
.read(this); .read(this);
} }

View File

@ -22,7 +22,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer; import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import io.cloudevents.CloudEvent; import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData; import io.cloudevents.CloudEventData;
import io.cloudevents.core.impl.CloudEventUtils; import io.cloudevents.core.CloudEventUtils;
import io.cloudevents.rw.CloudEventAttributesWriter; import io.cloudevents.rw.CloudEventAttributesWriter;
import io.cloudevents.rw.CloudEventContextReader; import io.cloudevents.rw.CloudEventContextReader;
import io.cloudevents.rw.CloudEventExtensionsWriter; import io.cloudevents.rw.CloudEventExtensionsWriter;

View File

@ -5,8 +5,8 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import io.cloudevents.CloudEvent; import io.cloudevents.CloudEvent;
import io.cloudevents.core.CloudEventUtils;
import io.cloudevents.core.builder.CloudEventBuilder; import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.core.impl.CloudEventUtils;
import io.cloudevents.core.test.Data; import io.cloudevents.core.test.Data;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;

View File

@ -18,7 +18,7 @@
package io.cloudevents.kafka; package io.cloudevents.kafka;
import io.cloudevents.CloudEvent; import io.cloudevents.CloudEvent;
import io.cloudevents.core.impl.CloudEventUtils; import io.cloudevents.core.CloudEventUtils;
import io.cloudevents.core.message.Encoding; import io.cloudevents.core.message.Encoding;
import io.cloudevents.core.message.MessageReader; import io.cloudevents.core.message.MessageReader;
import io.cloudevents.core.mock.MockBinaryMessageWriter; import io.cloudevents.core.mock.MockBinaryMessageWriter;
@ -41,7 +41,7 @@ public class CloudEventMessageSerializerTest {
Headers headers = new RecordHeaders(); Headers headers = new RecordHeaders();
MockBinaryMessageWriter inMessage = new MockBinaryMessageWriter(); MockBinaryMessageWriter inMessage = new MockBinaryMessageWriter();
CloudEventUtils.toVisitable(event).read(inMessage); CloudEventUtils.toReader(event).read(inMessage);
byte[] payload = serializer.serialize(topic, headers, inMessage); byte[] payload = serializer.serialize(topic, headers, inMessage);