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:
parent
285904f31c
commit
ed04c9f983
|
@ -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>
|
||||||
|
|
||||||
|
|
|
@ -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));
|
||||||
|
|
|
@ -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;
|
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>
|
/// <inheritdoc/>
|
||||||
/// Partion Key Adapter that converts to and from Guids in binary representation.
|
public bool ConvertKeyToPartitionKeyAttributeValue(byte[]? keyValue, out string? attributeValue)
|
||||||
/// </summary>
|
|
||||||
public class BinaryGuidPartitionKeyAdapter : IPartitionKeyAdapter<byte[]?>
|
|
||||||
{
|
{
|
||||||
/// <inheritdoc/>
|
if (keyValue == null)
|
||||||
public bool ConvertKeyToPartitionKeyAttributeValue(byte[]? keyValue, out string? attributeValue)
|
|
||||||
{
|
{
|
||||||
if (keyValue == null)
|
attributeValue = null;
|
||||||
{
|
return false;
|
||||||
attributeValue = null;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
attributeValue = new Guid(keyValue).ToString();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
attributeValue = new Guid(keyValue).ToString();
|
||||||
public bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out byte[]? keyValue)
|
return true;
|
||||||
{
|
}
|
||||||
if (string.IsNullOrEmpty(attributeValue))
|
|
||||||
{
|
|
||||||
keyValue = default;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
keyValue = Guid.Parse(attributeValue).ToByteArray();
|
/// <inheritdoc/>
|
||||||
return true;
|
public bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out byte[]? keyValue)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(attributeValue))
|
||||||
|
{
|
||||||
|
keyValue = default;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
keyValue = Guid.Parse(attributeValue).ToByteArray();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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>
|
/// <summary>
|
||||||
/// Defines the methods of the adapters responsible for transforming from cloud event
|
/// Converts a Message Key to PartionKey Attribute Value.
|
||||||
/// PartitionKey Attribute to Kafka Message Key.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TKey"></typeparam>
|
/// <param name="keyValue">The key value to transform.</param>
|
||||||
public interface IPartitionKeyAdapter<TKey>
|
/// <param name="attributeValue">The transformed attribute value (output).</param>
|
||||||
{
|
/// <returns>Whether the attribute should be set.</returns>
|
||||||
/// <summary>
|
bool ConvertKeyToPartitionKeyAttributeValue(TKey keyValue, out string? attributeValue);
|
||||||
/// 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);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Converts a PartitionKey Attribute value to a Message Key.
|
/// Converts a PartitionKey Attribute value to a Message Key.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="attributeValue">The attribute value to transform.</param>
|
/// <param name="attributeValue">The attribute value to transform.</param>
|
||||||
/// <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);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,26 @@
|
||||||
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters
|
// Copyright (c) Cloud Native Foundation.
|
||||||
{
|
// Licensed under the Apache 2.0 license.
|
||||||
/// <summary>
|
// See LICENSE file in the project root for full license information.
|
||||||
/// 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/>
|
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters;
|
||||||
public bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out TKey? keyValue)
|
|
||||||
{
|
/// <summary>
|
||||||
keyValue = default;
|
/// Partion Key Adapter that skips handling the key.
|
||||||
return false;
|
/// </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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,25 @@
|
||||||
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters
|
// Copyright (c) Cloud Native Foundation.
|
||||||
{
|
// Licensed under the Apache 2.0 license.
|
||||||
/// <summary>
|
// See LICENSE file in the project root for full license information.
|
||||||
/// 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/>
|
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters;
|
||||||
public bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out string? keyValue)
|
|
||||||
{
|
/// <summary>
|
||||||
keyValue = attributeValue;
|
/// Partion Key Adapter that skips handling the key.
|
||||||
return true;
|
/// </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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
|
Loading…
Reference in New Issue