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
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,36 @@
*
*/

package io.cloudevents.core.impl;
package io.cloudevents.core;

import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData;
import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.core.impl.CloudEventReaderAdapter;
import io.cloudevents.lang.Nullable;
import io.cloudevents.rw.CloudEventContextReader;
import io.cloudevents.rw.CloudEventDataMapper;
import io.cloudevents.rw.CloudEventRWException;
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 {

private CloudEventUtils() {}

/**
* 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.
* <p>
* It's safe to use the returned {@link CloudEventReader} multiple times.
*
* @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) {
return (CloudEventReader) event;
} else {
Expand All @@ -62,6 +69,27 @@ public static CloudEventContextReader toContextReader(CloudEvent event) {
}
}

/**
* 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class CloudEventReaderAdapter implements CloudEventReader, CloudEventCont

private final CloudEvent event;

CloudEventReaderAdapter(CloudEvent event) {
public CloudEventReaderAdapter(CloudEvent event) {
this.event = event;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData;
import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.core.CloudEventUtils;
import io.cloudevents.lang.Nullable;
import io.cloudevents.rw.*;

Expand Down Expand Up @@ -102,7 +102,7 @@ default CloudEvent toEvent() throws CloudEventRWException, IllegalStateException
default CloudEvent toEvent(@Nullable CloudEventDataMapper<? extends CloudEventData> mapper) throws CloudEventRWException, IllegalStateException {
switch (getEncoding()) {
case BINARY:
return this.read(CloudEventBuilder::fromSpecVersion, mapper);
return CloudEventUtils.toEvent(this, mapper);
case STRUCTURED:
return this.read((format, value) -> format.deserialize(value, mapper));
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
package io.cloudevents.core.message;

import io.cloudevents.CloudEvent;
import io.cloudevents.core.CloudEventUtils;
import io.cloudevents.core.format.EventFormat;
import io.cloudevents.core.impl.CloudEventUtils;
import io.cloudevents.core.message.impl.GenericStructuredMessageReader;
import io.cloudevents.rw.CloudEventWriter;
import io.cloudevents.rw.CloudEventWriterFactory;
Expand Down Expand Up @@ -68,7 +68,7 @@ default R writeStructured(CloudEvent event, EventFormat format) {
* @return return value at the end of the write process.
*/
default R writeBinary(CloudEvent event) {
return CloudEventUtils.toVisitable(event).read(this);
return CloudEventUtils.toReader(event).read(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package io.cloudevents.core.v03;

import io.cloudevents.SpecVersion;
import io.cloudevents.core.CloudEventUtils;
import io.cloudevents.core.impl.BaseCloudEventBuilder;
import io.cloudevents.core.impl.CloudEventUtils;
import io.cloudevents.rw.CloudEventRWException;
import io.cloudevents.types.Time;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import io.cloudevents.CloudEvent;
import io.cloudevents.SpecVersion;
import io.cloudevents.core.CloudEventUtils;
import io.cloudevents.core.impl.BaseCloudEventBuilder;
import io.cloudevents.core.impl.CloudEventUtils;
import io.cloudevents.rw.CloudEventRWException;
import io.cloudevents.types.Time;

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

import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData;
import io.cloudevents.SpecVersion;
import io.cloudevents.core.CloudEventUtils;
import io.cloudevents.core.data.BytesCloudEventData;
import io.cloudevents.core.impl.CloudEventUtils;
import io.cloudevents.core.message.MessageReader;
import io.cloudevents.core.message.impl.BaseBinaryMessageReader;
import io.cloudevents.rw.*;
Expand Down Expand Up @@ -57,7 +57,7 @@ public MockBinaryMessageWriter() {
public MockBinaryMessageWriter(CloudEvent event) {
this();
CloudEventUtils
.toVisitable(event)
.toReader(event)
.read(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import io.cloudevents.CloudEvent;
import io.cloudevents.CloudEventData;
import io.cloudevents.core.impl.CloudEventUtils;
import io.cloudevents.core.CloudEventUtils;
import io.cloudevents.rw.CloudEventAttributesWriter;
import io.cloudevents.rw.CloudEventContextReader;
import io.cloudevents.rw.CloudEventExtensionsWriter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import io.cloudevents.CloudEvent;
import io.cloudevents.core.CloudEventUtils;
import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.core.impl.CloudEventUtils;
import io.cloudevents.core.test.Data;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package io.cloudevents.kafka;

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.MessageReader;
import io.cloudevents.core.mock.MockBinaryMessageWriter;
Expand All @@ -41,7 +41,7 @@ public void serializerShouldWork() {
Headers headers = new RecordHeaders();

MockBinaryMessageWriter inMessage = new MockBinaryMessageWriter();
CloudEventUtils.toVisitable(event).read(inMessage);
CloudEventUtils.toReader(event).read(inMessage);

byte[] payload = serializer.serialize(topic, headers, inMessage);

Expand Down