Skip to content

Commit

Permalink
addressing some review comments for avro format
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Nov 10, 2021
1 parent 8505d78 commit 123bf68
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 20 deletions.
5 changes: 3 additions & 2 deletions formats/avro/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

<properties>
<avro.version>1.9.2</avro.version>
<build.helper.version>3.2.0</build.helper.version>
<module-name>io.cloudevents.formats.avro</module-name>
</properties>

Expand All @@ -41,7 +42,7 @@
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>1.10.2</version>
<version>${avro.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
Expand All @@ -59,7 +60,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<version>${build.helper.version}</version>
<executions>
<execution>
<id>add-source</id>
Expand Down
3 changes: 1 addition & 2 deletions formats/avro/src/main/avro/spec.avsc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"namespace":"io.cloudevents",
"namespace":"io.cloudevents.avro",
"type":"record",
"name":"AvroCloudEvent",
"version":"1.0",
Expand Down Expand Up @@ -61,4 +61,3 @@
}
]
}

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

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import io.cloudevents.AvroCloudEventData;
import io.cloudevents.CloudEventData;
import io.cloudevents.core.format.EventDeserializationException;

/**
* Encode JSON style cloudevent data into Avro format.
*
*/
public class AvroCloudEventDataWrapper implements CloudEventData {

private AvroCloudEventData avroCloudEventData;
private final AvroCloudEventData avroCloudEventData;

/**
* Wraps a JSON object-like data structure.
Expand All @@ -42,13 +42,11 @@ public AvroCloudEventDataWrapper(Map<String, Object> data) {

@Override
public byte[] toBytes() {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
try (ByteArrayOutputStream bytes = new ByteArrayOutputStream()) {
AvroCloudEventData.getEncoder().encode(this.avroCloudEventData, bytes);
} catch (IOException ignore) {
// ignored
return bytes.toByteArray();
} catch (IOException e) {
throw new EventDeserializationException(e);
}

return bytes.toByteArray();
}
}
10 changes: 3 additions & 7 deletions formats/avro/src/main/java/io/cloudevents/avro/AvroFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,19 @@ public class AvroFormat implements EventFormat {
@Override
public byte[] serialize(CloudEvent event) throws EventSerializationException {
AvroCloudEvent avroCloudEvent = AvroSerializer.toAvro(event);
ByteArrayOutputStream output = new ByteArrayOutputStream();

try {
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
AvroCloudEvent.getEncoder().encode(avroCloudEvent, output);
return output.toByteArray();
} catch (IOException e) {
throw new EventSerializationException(e);
}

return output.toByteArray();
}

@Override
public CloudEvent deserialize(byte[] bytes, CloudEventDataMapper<? extends CloudEventData> mapper)
throws EventDeserializationException {
ByteArrayInputStream input = new ByteArrayInputStream(bytes);

try {
try (ByteArrayInputStream input = new ByteArrayInputStream(bytes)) {
AvroCloudEvent avroCloudEvent = AvroCloudEvent.getDecoder().decode(input);

return new AvroDeserializer(avroCloudEvent).read(CloudEventBuilder::fromSpecVersion, mapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

class AvroSerializer {

public static final AvroCloudEvent toAvro(CloudEvent e) {
static final AvroCloudEvent toAvro(CloudEvent e) {
AvroCloudEvent avroCloudEvent = new AvroCloudEvent();

Map<String, Object> attrs = new HashMap<>();
Expand Down

0 comments on commit 123bf68

Please sign in to comment.