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>
<Description>Kafka extensions for CloudNative.CloudEvents</Description>
<PackageTags>cncf;cloudnative;cloudevents;events;kafka</PackageTags>
<LangVersion>9.0</LangVersion>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

View File

@ -25,6 +25,7 @@ namespace CloudNative.CloudEvents.Kafka
internal const string KafkaContentTypeAttributeName = "content-type";
private const string SpecVersionKafkaHeader = KafkaHeaderPrefix + "specversion";
/// <summary>
/// Indicates whether this message holds a single CloudEvent.
/// </summary>
@ -33,6 +34,17 @@ namespace CloudNative.CloudEvents.Kafka
/// </remarks>
/// <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>
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) =>
GetHeaderValue(message, SpecVersionKafkaHeader) is object ||
MimeUtilities.IsCloudEventsContentType(GetHeaderValue(message, KafkaContentTypeAttributeName));

View File

@ -1,36 +1,39 @@
// 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;
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/>
public bool ConvertKeyToPartitionKeyAttributeValue(byte[]? keyValue, out string? attributeValue)
{
/// <inheritdoc/>
public bool ConvertKeyToPartitionKeyAttributeValue(byte[]? keyValue, out string? attributeValue)
if (keyValue == null)
{
if (keyValue == null)
{
attributeValue = null;
return false;
}
attributeValue = new Guid(keyValue).ToString();
return true;
attributeValue = null;
return false;
}
/// <inheritdoc/>
public bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out byte[]? keyValue)
{
if (string.IsNullOrEmpty(attributeValue))
{
keyValue = default;
return false;
}
attributeValue = new Guid(keyValue).ToString();
return true;
}
keyValue = Guid.Parse(attributeValue).ToByteArray();
return true;
/// <inheritdoc/>
public bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out byte[]? keyValue)
{
if (string.IsNullOrEmpty(attributeValue))
{
keyValue = default;
return false;
}
keyValue = Guid.Parse(attributeValue).ToByteArray();
return true;
}
}

View File

@ -1,26 +1,29 @@
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.
/// Converts a Message Key to PartionKey Attribute Value.
/// </summary>
/// <typeparam name="TKey"></typeparam>
public interface IPartitionKeyAdapter<TKey>
{
/// <summary>
/// Converts a Message Key to PartionKey Attribute Value.
/// </summary>
/// <param name="keyValue">The key value to transform.</param>
/// <param name="attributeValue">The transformed attribute value (output).</param>
/// <returns>Whether the attribute should be set.</returns>
bool ConvertKeyToPartitionKeyAttributeValue(TKey keyValue, out string? attributeValue);
/// <param name="keyValue">The key value to transform.</param>
/// <param name="attributeValue">The transformed attribute value (output).</param>
/// <returns>Whether the attribute should be set.</returns>
bool ConvertKeyToPartitionKeyAttributeValue(TKey keyValue, out string? attributeValue);
/// <summary>
/// Converts a PartitionKey Attribute value to a Message Key.
/// </summary>
/// <param name="attributeValue">The attribute value to transform.</param>
/// <param name="keyValue">The transformed key value (output)</param>
/// <returns>Whether the key should be set.</returns>
bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out TKey? keyValue);
}
/// <summary>
/// Converts a PartitionKey Attribute value to a Message Key.
/// </summary>
/// <param name="attributeValue">The attribute value to transform.</param>
/// <param name="keyValue">The transformed key value (output)</param>
/// <returns>Whether the key should be set.</returns>
bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out TKey? keyValue);
}

View File

@ -1,23 +1,26 @@
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>
{
/// <inheritdoc/>
public bool ConvertKeyToPartitionKeyAttributeValue(TKey keyValue, out string? attributeValue)
{
attributeValue = null;
return false;
}
// Copyright (c) Cloud Native Foundation.
// Licensed under the Apache 2.0 license.
// See LICENSE file in the project root for full license information.
/// <inheritdoc/>
public bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out TKey? keyValue)
{
keyValue = default;
return false;
}
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>
{
/// <inheritdoc/>
public bool ConvertKeyToPartitionKeyAttributeValue(TKey keyValue, out string? attributeValue)
{
attributeValue = null;
return false;
}
/// <inheritdoc/>
public bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out TKey? keyValue)
{
keyValue = default;
return false;
}
}

View File

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

View File

@ -140,7 +140,6 @@ namespace CloudNative.CloudEvents.Kafka.UnitTests
public void KafkaNullKeyedStructuredMessageTest()
{
// 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 jsonEventFormatter = new JsonEventFormatter();
var cloudEvent = CreateTestCloudEvent();