-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add copyright notice, use file-scoped namespaces, avoid binary b…
…reaking change Signed-off-by: Jon Abaunza <j.abaunza@lantek.com>
- Loading branch information
Jon Abaunza
committed
Sep 26, 2024
1 parent
285904f
commit f1855f3
Showing
6 changed files
with
108 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 28 additions & 25 deletions
53
src/CloudNative.CloudEvents.Kafka/PartitionKeyAdapters/BinaryGuidPartitionKeyAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
45 changes: 24 additions & 21 deletions
45
src/CloudNative.CloudEvents.Kafka/PartitionKeyAdapters/IPartitionKeyAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"></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); | ||
} |
39 changes: 21 additions & 18 deletions
39
src/CloudNative.CloudEvents.Kafka/PartitionKeyAdapters/NullPartitionKeyAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,26 @@ | ||
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/> | ||
public bool ConvertKeyToPartitionKeyAttributeValue(TKey keyValue, out string? attributeValue) | ||
{ | ||
/// <inheritdoc/> | ||
public bool ConvertKeyToPartitionKeyAttributeValue(TKey keyValue, out string? attributeValue) | ||
{ | ||
attributeValue = null; | ||
return false; | ||
} | ||
attributeValue = null; | ||
return false; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out TKey? keyValue) | ||
{ | ||
keyValue = default; | ||
return false; | ||
} | ||
/// <inheritdoc/> | ||
public bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out TKey? keyValue) | ||
{ | ||
keyValue = default; | ||
return false; | ||
} | ||
} |
41 changes: 22 additions & 19 deletions
41
src/CloudNative.CloudEvents.Kafka/PartitionKeyAdapters/StringPartitionKeyAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |