From c3495d0a55244585da4ae19d853d8b7138d0ebd2 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Mon, 15 Feb 2021 10:49:46 +0000 Subject: [PATCH] Change ICloudEventFormatter into an abstract class Obviously the name is then inappropriate, but that will be changed in a subsequent commit. Signed-off-by: Jon Skeet --- .../AvroEventFormatter.cs | 12 +++++------ .../JsonEventFormatter.cs | 12 +++++------ .../ICloudEventFormatter.cs | 21 ++++++++++++------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/CloudNative.CloudEvents.Avro/AvroEventFormatter.cs b/src/CloudNative.CloudEvents.Avro/AvroEventFormatter.cs index 4d0ee1c..17201fc 100644 --- a/src/CloudNative.CloudEvents.Avro/AvroEventFormatter.cs +++ b/src/CloudNative.CloudEvents.Avro/AvroEventFormatter.cs @@ -40,10 +40,10 @@ namespace CloudNative.CloudEvents DecodeStructuredEvent(data, (IEnumerable)extensionAttributes); // FIXME: We shouldn't use synchronous stream methods... - public Task DecodeStructuredEventAsync(Stream data, IEnumerable extensionAttributes) => + public override Task DecodeStructuredEventAsync(Stream data, IEnumerable extensionAttributes) => Task.FromResult(DecodeStructuredEvent(data, extensionAttributes)); - public CloudEvent DecodeStructuredEvent(Stream data, IEnumerable extensionAttributes) + public override CloudEvent DecodeStructuredEvent(Stream data, IEnumerable extensionAttributes) { var decoder = new Avro.IO.BinaryDecoder(data); var rawEvent = avroReader.Read(null, decoder); @@ -53,7 +53,7 @@ namespace CloudNative.CloudEvents public CloudEvent DecodeStructuredEvent(byte[] data, params CloudEventAttribute[] extensionAttributes) => DecodeStructuredEvent(data, (IEnumerable) extensionAttributes); - public CloudEvent DecodeStructuredEvent(byte[] data, IEnumerable extensionAttributes) => + public override CloudEvent DecodeStructuredEvent(byte[] data, IEnumerable extensionAttributes) => DecodeStructuredEvent(new MemoryStream(data), extensionAttributes); public CloudEvent DecodeGenericRecord(GenericRecord record, IEnumerable extensionAttributes) @@ -106,7 +106,7 @@ namespace CloudNative.CloudEvents return cloudEvent; } - public byte[] EncodeStructuredEvent(CloudEvent cloudEvent, out ContentType contentType) + public override byte[] EncodeStructuredEvent(CloudEvent cloudEvent, out ContentType contentType) { contentType = new ContentType(CloudEvent.MediaType+AvroEventFormatter.MediaTypeSuffix); @@ -148,10 +148,10 @@ namespace CloudNative.CloudEvents } // TODO: Validate that this is correct... - public byte[] EncodeData(object value) => + public override byte[] EncodeData(object value) => throw new NotSupportedException("The Avro event formatter does not support binary content mode"); - public object DecodeData(byte[] value, string contentType) => + public override object DecodeData(byte[] value, string contentType) => throw new NotSupportedException("The Avro event formatter does not support binary content mode"); } } \ No newline at end of file diff --git a/src/CloudNative.CloudEvents.NewtonsoftJson/JsonEventFormatter.cs b/src/CloudNative.CloudEvents.NewtonsoftJson/JsonEventFormatter.cs index 0241a52..60de051 100644 --- a/src/CloudNative.CloudEvents.NewtonsoftJson/JsonEventFormatter.cs +++ b/src/CloudNative.CloudEvents.NewtonsoftJson/JsonEventFormatter.cs @@ -25,7 +25,7 @@ namespace CloudNative.CloudEvents.NewtonsoftJson public CloudEvent DecodeStructuredEvent(Stream data, params CloudEventAttribute[] extensionAttributes) => DecodeStructuredEvent(data, (IEnumerable) extensionAttributes); - public async Task DecodeStructuredEventAsync(Stream data, IEnumerable extensionAttributes) + public override async Task DecodeStructuredEventAsync(Stream data, IEnumerable extensionAttributes) { var jsonReader = new JsonTextReader(new StreamReader(data, Encoding.UTF8, true, 8192, true)) { @@ -35,7 +35,7 @@ namespace CloudNative.CloudEvents.NewtonsoftJson return DecodeJObject(jObject, extensionAttributes); } - public CloudEvent DecodeStructuredEvent(Stream data, IEnumerable extensionAttributes = null) + public override CloudEvent DecodeStructuredEvent(Stream data, IEnumerable extensionAttributes = null) { var jsonReader = new JsonTextReader(new StreamReader(data, Encoding.UTF8, true, 8192, true)) { @@ -48,7 +48,7 @@ namespace CloudNative.CloudEvents.NewtonsoftJson public CloudEvent DecodeStructuredEvent(byte[] data, params CloudEventAttribute[] extensionAttributes) => DecodeStructuredEvent(data, (IEnumerable)extensionAttributes); - public CloudEvent DecodeStructuredEvent(byte[] data, IEnumerable extensionAttributes = null) => + public override CloudEvent DecodeStructuredEvent(byte[] data, IEnumerable extensionAttributes = null) => DecodeStructuredEvent(new MemoryStream(data), extensionAttributes); // TODO: If we make this private, we'll have significantly more control over what token types we see. @@ -117,7 +117,7 @@ namespace CloudNative.CloudEvents.NewtonsoftJson return cloudEvent; } - public byte[] EncodeStructuredEvent(CloudEvent cloudEvent, out ContentType contentType) + public override byte[] EncodeStructuredEvent(CloudEvent cloudEvent, out ContentType contentType) { contentType = new ContentType("application/cloudevents+json") { @@ -157,7 +157,7 @@ namespace CloudNative.CloudEvents.NewtonsoftJson } // TODO: How should the caller know whether the result is "raw" or should be stored in data_base64? - public byte[] EncodeData(object value) + public override byte[] EncodeData(object value) { // TODO: Check this is what we want. // In particular, if this is just other text or binary data, rather than JSON, what does it @@ -173,7 +173,7 @@ namespace CloudNative.CloudEvents.NewtonsoftJson return json is null ? new byte[0] : Encoding.UTF8.GetBytes(json); } - public object DecodeData(byte[] value, string contentType) + public override object DecodeData(byte[] value, string contentType) { if (contentType == "application/json") { diff --git a/src/CloudNative.CloudEvents/ICloudEventFormatter.cs b/src/CloudNative.CloudEvents/ICloudEventFormatter.cs index 2fc28c0..82636de 100644 --- a/src/CloudNative.CloudEvents/ICloudEventFormatter.cs +++ b/src/CloudNative.CloudEvents/ICloudEventFormatter.cs @@ -2,6 +2,7 @@ // Licensed under the Apache 2.0 license. // See LICENSE file in the project root for full license information. +using System; using System.Collections.Generic; using System.IO; using System.Net.Mime; @@ -12,7 +13,7 @@ namespace CloudNative.CloudEvents /// /// Implemented by formatters /// - public interface ICloudEventFormatter + public abstract class ICloudEventFormatter { /// /// Decode a structured event from a stream @@ -20,7 +21,8 @@ namespace CloudNative.CloudEvents /// /// /// - CloudEvent DecodeStructuredEvent(Stream data, IEnumerable extensionAttributes); + public virtual CloudEvent DecodeStructuredEvent(Stream data, IEnumerable extensionAttributes) => + throw new NotImplementedException(); /// /// Decode a structured event from a stream asynchonously @@ -28,7 +30,8 @@ namespace CloudNative.CloudEvents /// /// /// - Task DecodeStructuredEventAsync(Stream data, IEnumerable extensionAttributes); + public virtual Task DecodeStructuredEventAsync(Stream data, IEnumerable extensionAttributes) => + throw new NotImplementedException(); // TODO: Remove either this one or the stream one? It seems unnecessary to have both. @@ -38,7 +41,8 @@ namespace CloudNative.CloudEvents /// /// /// - CloudEvent DecodeStructuredEvent(byte[] data, IEnumerable extensionAttributes); + public virtual CloudEvent DecodeStructuredEvent(byte[] data, IEnumerable extensionAttributes) => + throw new NotImplementedException(); /// /// Encode an structured event into a byte array @@ -46,11 +50,12 @@ namespace CloudNative.CloudEvents /// /// /// - byte[] EncodeStructuredEvent(CloudEvent cloudEvent, out ContentType contentType); - + public virtual byte[] EncodeStructuredEvent(CloudEvent cloudEvent, out ContentType contentType) => + throw new NotImplementedException(); + // TODO: Work out whether this is what we want, and whether to potentially // separate it into a separate interface. - byte[] EncodeData(object value); - object DecodeData(byte[] value, string contentType); + public virtual byte[] EncodeData(object value) => throw new NotImplementedException(); + public virtual object DecodeData(byte[] value, string contentType) => throw new NotImplementedException(); } } \ No newline at end of file