Skip to content

Commit b0f17b8

Browse files
committed
wip improve serializer and deserializer
Signed-off-by: Ning Sun <[email protected]>
1 parent 0b64945 commit b0f17b8

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
lines changed

formats/avro/src/main/java/io/cloudevents/avro/AvroDeserializer.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,63 @@
1717

1818
package io.cloudevents.avro;
1919

20+
import java.util.Map;
21+
import java.net.URI;
22+
import java.time.Instant;
23+
import java.time.OffsetDateTime;
24+
import java.time.ZoneOffset;
25+
2026
import io.cloudevents.CloudEvent;
27+
import io.cloudevents.CloudEventData;
2128
import io.cloudevents.SpecVersion;
2229
import io.cloudevents.AvroCloudEvent;
2330
import io.cloudevents.AvroCloudEventData;
2431
import io.cloudevents.core.builder.CloudEventBuilder;
32+
import io.cloudevents.core.v1.CloudEventV1;
33+
import io.cloudevents.rw.CloudEventRWException;
34+
import io.cloudevents.rw.CloudEventReader;
35+
import io.cloudevents.rw.CloudEventDataMapper;
36+
import io.cloudevents.rw.CloudEventWriter;
37+
import io.cloudevents.rw.CloudEventWriterFactory;
38+
39+
public class AvroDeserializer implements CloudEventReader {
40+
41+
private final AvroCloudEvent avroCloudEvent;
42+
43+
public AvroDeserializer(AvroCloudEvent avroCloudEvent) {
44+
this.avroCloudEvent = avroCloudEvent;
45+
}
46+
47+
@Override
48+
public <W extends CloudEventWriter<R>, R> R read(CloudEventWriterFactory<W, R> writerFactory,
49+
CloudEventDataMapper<? extends CloudEventData> mapper) throws CloudEventRWException {
50+
51+
Map<CharSequence, Object> avroCloudEventAttrs = this.avroCloudEvent.getAttribute();
52+
SpecVersion specVersion = SpecVersion.parse((String)avroCloudEventAttrs.get(CloudEventV1.SPECVERSION));
53+
final CloudEventWriter<R> writer = writerFactory.create(specVersion);
54+
55+
for (Map.Entry<CharSequence, Object> entry: avroCloudEventAttrs.entrySet()) {
56+
String key = entry.getKey().toString();
2557

26-
public class AvroDeserializer {
58+
if (key.equals(CloudEventV1.TIME)) {
59+
// OffsetDateTime
60+
Long timeAsLong = (Long) entry.getValue();
61+
Instant timeAsInstant = Instant.ofEpochMilli(timeAsLong);
62+
OffsetDateTime value = OffsetDateTime.ofInstant(timeAsInstant, ZoneOffset.UTC);
63+
writer.withContextAttribute(key, value);
2764

28-
public static CloudEvent fromAvro(AvroCloudEvent avroCloudEvent) {
65+
} else if (key.equals(CloudEventV1.DATASCHEMA)) {
66+
// URI
67+
URI value = URI.create((String) entry.getValue());
68+
writer.withContextAttribute(key, value);
69+
} else {
70+
// String
71+
writer.withContextAttribute(key, (String) entry.getValue());
72+
}
73+
}
2974

75+
// TOOD: data
76+
return writer.end();
3077
}
3178

3279
}

formats/avro/src/main/java/io/cloudevents/avro/AvroSerializer.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import io.cloudevents.CloudEvent;
2424
import io.cloudevents.CloudEventData;
25+
import io.cloudevents.core.v1.CloudEventV1;
2526
import io.cloudevents.AvroCloudEvent;
2627
import io.cloudevents.AvroCloudEventData;
2728

@@ -32,14 +33,17 @@ public static final AvroCloudEvent toAvro(CloudEvent e) {
3233

3334
Map<CharSequence, Object> attrs = new HashMap<>();
3435

35-
attrs.put("type", e.getType());
36-
attrs.put("specversion", e.getSpecVersion().toString());
37-
attrs.put("id", e.getId());
38-
attrs.put("source", e.getSource());
39-
attrs.put("time", e.getTime());
40-
attrs.put("dataschema", e.getDataSchema());
41-
attrs.put("contenttype", AvroFormat.AVRO_CONTENT_TYPE);
42-
attrs.put("datacontenttype", e.getDataContentType());
36+
attrs.put(CloudEventV1.TYPE, e.getType());
37+
attrs.put(CloudEventV1.SPECVERSION, e.getSpecVersion().toString());
38+
attrs.put(CloudEventV1.ID, e.getId());
39+
attrs.put(CloudEventV1.SOURCE, e.getSource());
40+
// convert to long
41+
attrs.put(CloudEventV1.TIME, e.getTime().toInstant().toEpochMilli());
42+
// convert
43+
attrs.put(CloudEventV1.DATASCHEMA, e.getDataSchema().toString());
44+
attrs.put(CloudEventV1.SUBJECT, e.getSubject());
45+
46+
attrs.put(CloudEventV1.DATACONTENTTYPE, e.getDataContentType());
4347

4448
avroCloudEvent.setAttribute(attrs);
4549

0 commit comments

Comments
 (0)