fix: Add copyright notice, use file-scoped namespaces, avoid binary breaking change

Signed-off-by: Jon Abaunza <j.abaunza@lantek.com>
This commit is contained in:
Jon Abaunza 2024-09-26 13:22:35 +02:00
parent 285904f31c
commit ed04c9f983
7 changed files with 110 additions and 87 deletions

View File

@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net8.0</TargetFrameworks> <TargetFrameworks>netstandard2.0;netstandard2.1;net8.0</TargetFrameworks>
<Description>Kafka extensions for CloudNative.CloudEvents</Description> <Description>Kafka extensions for CloudNative.CloudEvents</Description>
<PackageTags>cncf;cloudnative;cloudevents;events;kafka</PackageTags> <PackageTags>cncf;cloudnative;cloudevents;events;kafka</PackageTags>
<LangVersion>9.0</LangVersion> <LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>

View File

@ -25,6 +25,7 @@ namespace CloudNative.CloudEvents.Kafka
internal const string KafkaContentTypeAttributeName = "content-type"; internal const string KafkaContentTypeAttributeName = "content-type";
private const string SpecVersionKafkaHeader = KafkaHeaderPrefix + "specversion"; private const string SpecVersionKafkaHeader = KafkaHeaderPrefix + "specversion";
/// <summary> /// <summary>
/// Indicates whether this message holds a single CloudEvent. /// Indicates whether this message holds a single CloudEvent.
/// </summary> /// </summary>
@ -33,6 +34,17 @@ namespace CloudNative.CloudEvents.Kafka
/// </remarks> /// </remarks>
/// <param name="message">The message to check for the presence of a CloudEvent. Must not be null.</param> /// <param name="message">The message to check for the presence of a CloudEvent. Must not be null.</param>
/// <returns>true, if the request is a CloudEvent</returns> /// <returns>true, if the request is a CloudEvent</returns>
public static bool IsCloudEvent(this Message<string?, byte[]> message) => IsCloudEvent<string?>(message);
/// <summary>
/// Indicates whether this message holds a single CloudEvent.
/// </summary>
/// <remarks>
/// This method returns false for batch requests, as they need to be parsed differently.
/// </remarks>
/// <param name="message">The message to check for the presence of a CloudEvent. Must not be null.</param>
/// <typeparam name="TKey">The type of key of the Kafka message.</typeparam>
/// <returns>true, if the request is a CloudEvent</returns>
public static bool IsCloudEvent<TKey>(this Message<TKey, byte[]> message) => public static bool IsCloudEvent<TKey>(this Message<TKey, byte[]> message) =>
GetHeaderValue(message, SpecVersionKafkaHeader) is object || GetHeaderValue(message, SpecVersionKafkaHeader) is object ||
MimeUtilities.IsCloudEventsContentType(GetHeaderValue(message, KafkaContentTypeAttributeName)); MimeUtilities.IsCloudEventsContentType(GetHeaderValue(message, KafkaContentTypeAttributeName));

View File

@ -1,12 +1,16 @@
// Copyright (c) Cloud Native Foundation.
// Licensed under the Apache 2.0 license.
// See LICENSE file in the project root for full license information.
using System; using System;
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters;
/// <summary>
/// Partion Key Adapter that converts to and from Guids in binary representation.
/// </summary>
public class BinaryGuidPartitionKeyAdapter : IPartitionKeyAdapter<byte[]?>
{ {
/// <summary>
/// Partion Key Adapter that converts to and from Guids in binary representation.
/// </summary>
public class BinaryGuidPartitionKeyAdapter : IPartitionKeyAdapter<byte[]?>
{
/// <inheritdoc/> /// <inheritdoc/>
public bool ConvertKeyToPartitionKeyAttributeValue(byte[]? keyValue, out string? attributeValue) public bool ConvertKeyToPartitionKeyAttributeValue(byte[]? keyValue, out string? attributeValue)
{ {
@ -32,5 +36,4 @@ namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters
keyValue = Guid.Parse(attributeValue).ToByteArray(); keyValue = Guid.Parse(attributeValue).ToByteArray();
return true; return true;
} }
}
} }

View File

@ -1,12 +1,16 @@
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters // Copyright (c) Cloud Native Foundation.
// Licensed under the Apache 2.0 license.
// See LICENSE file in the project root for full license information.
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters;
/// <summary>
/// Defines the methods of the adapters responsible for transforming from cloud event
/// PartitionKey Attribute to Kafka Message Key.
/// </summary>
/// <typeparam name="TKey">The type of Kafka Message Key.</typeparam>
public interface IPartitionKeyAdapter<TKey>
{ {
/// <summary>
/// Defines the methods of the adapters responsible for transforming from cloud event
/// PartitionKey Attribute to Kafka Message Key.
/// </summary>
/// <typeparam name="TKey"></typeparam>
public interface IPartitionKeyAdapter<TKey>
{
/// <summary> /// <summary>
/// Converts a Message Key to PartionKey Attribute Value. /// Converts a Message Key to PartionKey Attribute Value.
/// </summary> /// </summary>
@ -22,5 +26,4 @@ namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters
/// <param name="keyValue">The transformed key value (output)</param> /// <param name="keyValue">The transformed key value (output)</param>
/// <returns>Whether the key should be set.</returns> /// <returns>Whether the key should be set.</returns>
bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out TKey? keyValue); bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out TKey? keyValue);
}
} }

View File

@ -1,11 +1,15 @@
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters // Copyright (c) Cloud Native Foundation.
// Licensed under the Apache 2.0 license.
// See LICENSE file in the project root for full license information.
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters;
/// <summary>
/// Partion Key Adapter that skips handling the key.
/// </summary>
/// <typeparam name="TKey">The type of Kafka Message Key.</typeparam>
public class NullPartitionKeyAdapter<TKey> : IPartitionKeyAdapter<TKey>
{ {
/// <summary>
/// Partion Key Adapter that skips handling the key.
/// </summary>
/// <typeparam name="TKey">The type of Kafka Message Key</typeparam>
public class NullPartitionKeyAdapter<TKey> : IPartitionKeyAdapter<TKey>
{
/// <inheritdoc/> /// <inheritdoc/>
public bool ConvertKeyToPartitionKeyAttributeValue(TKey keyValue, out string? attributeValue) public bool ConvertKeyToPartitionKeyAttributeValue(TKey keyValue, out string? attributeValue)
{ {
@ -19,5 +23,4 @@ namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters
keyValue = default; keyValue = default;
return false; return false;
} }
}
} }

View File

@ -1,10 +1,14 @@
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters // Copyright (c) Cloud Native Foundation.
// Licensed under the Apache 2.0 license.
// See LICENSE file in the project root for full license information.
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters;
/// <summary>
/// Partion Key Adapter that skips handling the key.
/// </summary>
public class StringPartitionKeyAdapter : IPartitionKeyAdapter<string?>
{ {
/// <summary>
/// Partion Key Adapter that skips handling the key.
/// </summary>
public class StringPartitionKeyAdapter : IPartitionKeyAdapter<string?>
{
/// <inheritdoc/> /// <inheritdoc/>
public bool ConvertKeyToPartitionKeyAttributeValue(string? keyValue, out string? attributeValue) public bool ConvertKeyToPartitionKeyAttributeValue(string? keyValue, out string? attributeValue)
{ {
@ -18,5 +22,4 @@ namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters
keyValue = attributeValue; keyValue = attributeValue;
return true; return true;
} }
}
} }

View File

@ -140,7 +140,6 @@ namespace CloudNative.CloudEvents.Kafka.UnitTests
public void KafkaNullKeyedStructuredMessageTest() public void KafkaNullKeyedStructuredMessageTest()
{ {
// It will test the serialization using Confluent's Confluent.Kafka.Null type for the key. // It will test the serialization using Confluent's Confluent.Kafka.Null type for the key.
// As the default behavior without adapter is to skip the key it will work properly.
var partitionKeyAdapter = new PartitionKeyAdapters.NullPartitionKeyAdapter<Confluent.Kafka.Null>(); var partitionKeyAdapter = new PartitionKeyAdapters.NullPartitionKeyAdapter<Confluent.Kafka.Null>();
var jsonEventFormatter = new JsonEventFormatter(); var jsonEventFormatter = new JsonEventFormatter();
var cloudEvent = CreateTestCloudEvent(); var cloudEvent = CreateTestCloudEvent();