From 07d0fa6807619a01698b4ecb3c28798363cab0b4 Mon Sep 17 00:00:00 2001 From: Alex Collins Date: Tue, 6 Jun 2023 08:02:26 -0700 Subject: [PATCH] fix: avroturbo --- formats/{avro => avro-turbo}/README.md | 0 formats/{avro => avro-turbo}/pom.xml | 12 +- .../src/main/avro/cloudevents.avsc | 0 .../avroturbo/AvroTurboFormat.java | 127 ++++++++++++++++++ .../io.cloudevents.core.format.EventFormat | 1 + .../cloudevents/avro/AvroTurboFormatTest.java | 76 +++++++++++ .../java/io/cloudevents/avro/AvroFormat.java | 127 ------------------ .../io.cloudevents.core.format.EventFormat | 1 - .../io/cloudevents/avro/AvroFormatTest.java | 78 ----------- pom.xml | 2 +- 10 files changed, 214 insertions(+), 210 deletions(-) rename formats/{avro => avro-turbo}/README.md (100%) rename formats/{avro => avro-turbo}/pom.xml (91%) rename formats/{avro => avro-turbo}/src/main/avro/cloudevents.avsc (100%) create mode 100644 formats/avro-turbo/src/main/java/io/cloudevents/avroturbo/AvroTurboFormat.java create mode 100644 formats/avro-turbo/src/main/resources/META-INF/services/io.cloudevents.core.format.EventFormat create mode 100644 formats/avro-turbo/src/test/java/io/cloudevents/avro/AvroTurboFormatTest.java delete mode 100644 formats/avro/src/main/java/io/cloudevents/avro/AvroFormat.java delete mode 100644 formats/avro/src/main/resources/META-INF/services/io.cloudevents.core.format.EventFormat delete mode 100644 formats/avro/src/test/java/io/cloudevents/avro/AvroFormatTest.java diff --git a/formats/avro/README.md b/formats/avro-turbo/README.md similarity index 100% rename from formats/avro/README.md rename to formats/avro-turbo/README.md diff --git a/formats/avro/pom.xml b/formats/avro-turbo/pom.xml similarity index 91% rename from formats/avro/pom.xml rename to formats/avro-turbo/pom.xml index 7db6ef1df..8d7560c1b 100644 --- a/formats/avro/pom.xml +++ b/formats/avro-turbo/pom.xml @@ -23,12 +23,18 @@ io.cloudevents cloudevents-parent - 2.5.0-SNAPSHOT + 3.0.0-SNAPSHOT ../../pom.xml - cloudevents-avro - CloudEvents - Avro + cloudevents-avro-turbo + CloudEvents - Avro Turbo + + + + 2.13.3 + io.cloudevents.formats.avroturbo + diff --git a/formats/avro/src/main/avro/cloudevents.avsc b/formats/avro-turbo/src/main/avro/cloudevents.avsc similarity index 100% rename from formats/avro/src/main/avro/cloudevents.avsc rename to formats/avro-turbo/src/main/avro/cloudevents.avsc diff --git a/formats/avro-turbo/src/main/java/io/cloudevents/avroturbo/AvroTurboFormat.java b/formats/avro-turbo/src/main/java/io/cloudevents/avroturbo/AvroTurboFormat.java new file mode 100644 index 000000000..2ae8ef9dd --- /dev/null +++ b/formats/avro-turbo/src/main/java/io/cloudevents/avroturbo/AvroTurboFormat.java @@ -0,0 +1,127 @@ +/* + * Copyright 2018-Present The CloudEvents Authors + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.avroturbo; + + +import io.cloudevents.CloudEvent; +import io.cloudevents.CloudEventData; +import io.cloudevents.core.builder.CloudEventBuilder; +import io.cloudevents.core.data.BytesCloudEventData; +import io.cloudevents.core.format.EventDeserializationException; +import io.cloudevents.core.format.EventFormat; +import io.cloudevents.core.format.EventSerializationException; +import io.cloudevents.rw.CloudEventDataMapper; +import io.cloudevents.v1.avro.CloudEvent.Builder; + +import java.net.URI; +import java.nio.ByteBuffer; +import java.time.ZoneOffset; +import java.util.HashMap; +import java.util.Map; + +/** + * An implementation of {@link EventFormat} for the Avro format. + * This format is resolvable with {@link io.cloudevents.core.provider.EventFormatProvider} using the content type {@link #AVRO_CONTENT_TYPE}. + * It only supports data that is bytes. + */ +public class AvroTurboFormat implements EventFormat { + + public static final String AVRO_CONTENT_TYPE = "application/cloudevents+avroturbo"; + + @Override + public byte[] serialize(CloudEvent from) throws EventSerializationException { + try { + Builder to = io.cloudevents.v1.avro.CloudEvent.newBuilder(); + + // extensions + Map attribute = new HashMap<>(); + for (String name : from.getExtensionNames()) { + Object value = from.getExtension(name); + attribute.put(name, value instanceof byte[] ? ByteBuffer.wrap((byte[]) value) : value); + } + + to.setSource(from.getSource().toString()) + .setType(from.getType()) + .setId(from.getId()) + .setSubject(from.getSubject()) + .setDatacontenttype(from.getDataContentType()) + .setAttribute(attribute); + + if (from.getTime() != null) + to.setTime(from.getTime().toInstant()); + if (from.getDataSchema() != null) + to.setDataschema(from.getDataSchema().toString()); + + CloudEventData data = from.getData(); + if (data != null) + to.setData(ByteBuffer.wrap(data.toBytes())); + return to.build().toByteBuffer().array(); + } catch (Exception e) { + throw new EventSerializationException(e); + } + } + + @Override + public CloudEvent deserialize(byte[] bytes, CloudEventDataMapper mapper) throws EventDeserializationException { + try { + io.cloudevents.v1.avro.CloudEvent from = io.cloudevents.v1.avro.CloudEvent.fromByteBuffer(ByteBuffer.wrap(bytes)); + CloudEventBuilder to = CloudEventBuilder.v1() + .withSource(URI.create(from.getSource())) + .withType(from.getType()) + .withId(from.getType()) + .withSubject(from.getSubject()) + .withDataContentType(from.getDatacontenttype()); + + if (from.getTime() != null) + to.withTime(from.getTime().atOffset(ZoneOffset.UTC)); + if (from.getDataschema() != null) + to.withDataSchema(URI.create(from.getDataschema())); + + // extensions + for (Map.Entry entry : from.getAttribute().entrySet()) { + String name = entry.getKey(); + Object value = entry.getValue(); + // Avro supports boolean, int, string, bytes + if (value instanceof Boolean) + to.withExtension(name, (boolean) value); + else if (value instanceof Integer) + to.withExtension(name, (int) value); + else if (value instanceof String) + to.withExtension(name, (String) value); + else if (value instanceof ByteBuffer) + to.withExtension(name, ((ByteBuffer) value).array()); + else + // this cannot happen, if ever seen, must be bug in this library + throw new AssertionError(String.format("invalid extension %s unsupported type %s", name, value.getClass())); + } + + if (from.getData() == null) + return to.end(); + else { + CloudEventData data = BytesCloudEventData.wrap(from.getData().array()); + return to.end(mapper.map(data)); + } + } catch (Exception e) { + throw new EventDeserializationException(e); + } + } + + @Override + public String serializedContentType() { + return AVRO_CONTENT_TYPE; + } +} diff --git a/formats/avro-turbo/src/main/resources/META-INF/services/io.cloudevents.core.format.EventFormat b/formats/avro-turbo/src/main/resources/META-INF/services/io.cloudevents.core.format.EventFormat new file mode 100644 index 000000000..4274487db --- /dev/null +++ b/formats/avro-turbo/src/main/resources/META-INF/services/io.cloudevents.core.format.EventFormat @@ -0,0 +1 @@ +io.cloudevents.avroturbo.AvroTurboFormat diff --git a/formats/avro-turbo/src/test/java/io/cloudevents/avro/AvroTurboFormatTest.java b/formats/avro-turbo/src/test/java/io/cloudevents/avro/AvroTurboFormatTest.java new file mode 100644 index 000000000..48d2057e7 --- /dev/null +++ b/formats/avro-turbo/src/test/java/io/cloudevents/avro/AvroTurboFormatTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 2018-Present The CloudEvents Authors + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.avro; + +import io.cloudevents.CloudEvent; +import io.cloudevents.avroturbo.AvroTurboFormat; +import io.cloudevents.core.builder.CloudEventBuilder; +import io.cloudevents.core.data.BytesCloudEventData; +import io.cloudevents.core.format.EventFormat; +import io.cloudevents.core.provider.EventFormatProvider; +import org.junit.jupiter.api.Test; + +import java.net.URI; +import java.time.Instant; +import java.time.ZoneOffset; +import java.util.Collections; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +class AvroTurboFormatTest { + + private final EventFormat format = EventFormatProvider.getInstance().resolveFormat(AvroTurboFormat.AVRO_CONTENT_TYPE); + + // TODO - add test cases for + // - null data + // - non-bytes data + // - extension that is bytes + // - invalid extension type + @Test + void format() { + assertNotNull(format); + assertEquals(Collections.singleton("application/cloudevents+avroturbo"), format.deserializableContentTypes()); + + CloudEvent event = CloudEventBuilder.v1() + // mandatory + .withId("") + .withSource(URI.create("")) + .withType("") + // optional + .withTime(Instant.EPOCH.atOffset(ZoneOffset.UTC)) + .withSubject("") + .withDataSchema(URI.create("")) + // extension + // support boolean, int, string, bytes + .withExtension("boolean", false) + .withExtension("int", 0) + .withExtension("string", "") + // omitting bytes, because it is not supported be CloudEvent.equals + .withData("", BytesCloudEventData.wrap(new byte[0])) + .build(); + + byte[] serialized = format.serialize(event); + + assertNotNull(serialized); + + CloudEvent deserialized = format.deserialize(serialized); + + assertEquals(event, deserialized); + + } +} diff --git a/formats/avro/src/main/java/io/cloudevents/avro/AvroFormat.java b/formats/avro/src/main/java/io/cloudevents/avro/AvroFormat.java deleted file mode 100644 index f910f4156..000000000 --- a/formats/avro/src/main/java/io/cloudevents/avro/AvroFormat.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright 2018-Present The CloudEvents Authors - *

- * 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 - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * 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.avro; - - -import io.cloudevents.CloudEvent; -import io.cloudevents.CloudEventData; -import io.cloudevents.core.builder.CloudEventBuilder; -import io.cloudevents.core.data.BytesCloudEventData; -import io.cloudevents.core.format.EventDeserializationException; -import io.cloudevents.core.format.EventFormat; -import io.cloudevents.core.format.EventSerializationException; -import io.cloudevents.rw.CloudEventDataMapper; -import io.cloudevents.v1.avro.CloudEvent.Builder; - -import java.net.URI; -import java.nio.ByteBuffer; -import java.time.ZoneOffset; -import java.util.HashMap; -import java.util.Map; - -/** - * An implementation of {@link EventFormat} for the Avro format. - * This format is resolvable with {@link io.cloudevents.core.provider.EventFormatProvider} using the content type {@link #AVRO_CONTENT_TYPE}. - * It only supports data that is bytes. - */ -public class AvroFormat implements EventFormat { - - public static final String AVRO_CONTENT_TYPE = "application/cloudevents+avro"; - - @Override - public byte[] serialize(CloudEvent from) throws EventSerializationException { - try { - Builder to = io.cloudevents.v1.avro.CloudEvent.newBuilder(); - - // extensions - Map attribute = new HashMap<>(); - for (String name : from.getExtensionNames()) { - Object value = from.getExtension(name); - attribute.put(name, value instanceof byte[] ? ByteBuffer.wrap((byte[]) value) : value); - } - - to.setSource(from.getSource().toString()) - .setType(from.getType()) - .setId(from.getId()) - .setSubject(from.getSubject()) - .setDatacontenttype(from.getDataContentType()) - .setAttribute(attribute); - - if (from.getTime() != null) - to.setTime(from.getTime().toInstant()); - if (from.getDataSchema() != null) - to.setDataschema(from.getDataSchema().toString()); - - CloudEventData data = from.getData(); - if (data != null) - to.setData(ByteBuffer.wrap(data.toBytes())); - return to.build().toByteBuffer().array(); - } catch (Exception e) { - throw new EventSerializationException(e); - } - } - - @Override - public CloudEvent deserialize(byte[] bytes, CloudEventDataMapper mapper) throws EventDeserializationException { - try { - io.cloudevents.v1.avro.CloudEvent from = io.cloudevents.v1.avro.CloudEvent.fromByteBuffer(ByteBuffer.wrap(bytes)); - CloudEventBuilder to = CloudEventBuilder.v1() - .withSource(URI.create(from.getSource())) - .withType(from.getType()) - .withId(from.getType()) - .withSubject(from.getSubject()) - .withDataContentType(from.getDatacontenttype()); - - if (from.getTime() != null) - to.withTime(from.getTime().atOffset(ZoneOffset.UTC)); - if (from.getDataschema() != null) - to.withDataSchema(URI.create(from.getDataschema())); - - // extensions - for (Map.Entry entry : from.getAttribute().entrySet()) { - String name = entry.getKey(); - Object value = entry.getValue(); - // Avro supports boolean, int, string, bytes - if (value instanceof Boolean) - to.withExtension(name, (boolean) value); - else if (value instanceof Integer) - to.withExtension(name, (int) value); - else if (value instanceof String) - to.withExtension(name, (String) value); - else if (value instanceof ByteBuffer) - to.withExtension(name, ((ByteBuffer) value).array()); - else - // this cannot happen, if ever seen, must be bug in this library - throw new AssertionError(String.format("invalid extension %s unsupported type %s", name, value.getClass())); - } - - if (from.getData() == null) - return to.end(); - else { - CloudEventData data = BytesCloudEventData.wrap(from.getData().array()); - return to.end(mapper.map(data)); - } - } catch (Exception e) { - throw new EventDeserializationException(e); - } - } - - @Override - public String serializedContentType() { - return AVRO_CONTENT_TYPE; - } -} diff --git a/formats/avro/src/main/resources/META-INF/services/io.cloudevents.core.format.EventFormat b/formats/avro/src/main/resources/META-INF/services/io.cloudevents.core.format.EventFormat deleted file mode 100644 index 31cb85d6d..000000000 --- a/formats/avro/src/main/resources/META-INF/services/io.cloudevents.core.format.EventFormat +++ /dev/null @@ -1 +0,0 @@ -io.cloudevents.avro.AvroFormat diff --git a/formats/avro/src/test/java/io/cloudevents/avro/AvroFormatTest.java b/formats/avro/src/test/java/io/cloudevents/avro/AvroFormatTest.java deleted file mode 100644 index 5dd5804d5..000000000 --- a/formats/avro/src/test/java/io/cloudevents/avro/AvroFormatTest.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2018-Present The CloudEvents Authors - *

- * 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 - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * 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.avro; - -import io.cloudevents.CloudEvent; -import io.cloudevents.core.builder.CloudEventBuilder; -import io.cloudevents.core.data.BytesCloudEventData; -import io.cloudevents.core.data.PojoCloudEventData; -import io.cloudevents.core.format.EventFormat; -import io.cloudevents.core.provider.EventFormatProvider; -import org.junit.jupiter.api.Test; - -import java.net.URI; -import java.time.Instant; -import java.time.OffsetDateTime; -import java.time.ZoneOffset; -import java.util.Collections; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -class AvroFormatTest { - - private final EventFormat format = EventFormatProvider.getInstance().resolveFormat(AvroFormat.AVRO_CONTENT_TYPE); - - // TODO - add test cases for - // - null data - // - non-bytes data - // - extension that is bytes - // - invalid extension type - @Test - void format() { - assertNotNull(format); - assertEquals(Collections.singleton("application/cloudevents+avro"), format.deserializableContentTypes()); - - CloudEvent event = CloudEventBuilder.v1() - // mandatory - .withId("") - .withSource(URI.create("")) - .withType("") - // optional - .withTime(Instant.EPOCH.atOffset(ZoneOffset.UTC)) - .withSubject("") - .withDataSchema(URI.create("")) - // extension - // support boolean, int, string, bytes - .withExtension("boolean", false) - .withExtension("int", 0) - .withExtension("string", "") - // omitting bytes, because it is not supported be CloudEvent.equals - .withData("", BytesCloudEventData.wrap(new byte[0])) - .build(); - - byte[] serialized = format.serialize(event); - - assertNotNull(serialized); - - CloudEvent deserialized = format.deserialize(serialized); - - assertEquals(event, deserialized); - - } -} diff --git a/pom.xml b/pom.xml index 457c9d95a..82c5fed7d 100644 --- a/pom.xml +++ b/pom.xml @@ -68,7 +68,7 @@ api core - formats/avro + formats/avro-turbo formats/json-jackson formats/protobuf formats/xml