Skip to content

Commit ed04c9f

Browse files
author
Jon Abaunza
committed
fix: Add copyright notice, use file-scoped namespaces, avoid binary breaking change
Signed-off-by: Jon Abaunza <[email protected]>
1 parent 285904f commit ed04c9f

File tree

7 files changed

+108
-85
lines changed

7 files changed

+108
-85
lines changed

src/CloudNative.CloudEvents.Kafka/CloudNative.CloudEvents.Kafka.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFrameworks>netstandard2.0;netstandard2.1;net8.0</TargetFrameworks>
55
<Description>Kafka extensions for CloudNative.CloudEvents</Description>
66
<PackageTags>cncf;cloudnative;cloudevents;events;kafka</PackageTags>
7-
<LangVersion>9.0</LangVersion>
7+
<LangVersion>10.0</LangVersion>
88
<Nullable>enable</Nullable>
99
</PropertyGroup>
1010

src/CloudNative.CloudEvents.Kafka/KafkaExtensions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public static class KafkaExtensions
2525
internal const string KafkaContentTypeAttributeName = "content-type";
2626
private const string SpecVersionKafkaHeader = KafkaHeaderPrefix + "specversion";
2727

28+
2829
/// <summary>
2930
/// Indicates whether this message holds a single CloudEvent.
3031
/// </summary>
@@ -33,6 +34,17 @@ public static class KafkaExtensions
3334
/// </remarks>
3435
/// <param name="message">The message to check for the presence of a CloudEvent. Must not be null.</param>
3536
/// <returns>true, if the request is a CloudEvent</returns>
37+
public static bool IsCloudEvent(this Message<string?, byte[]> message) => IsCloudEvent<string?>(message);
38+
39+
/// <summary>
40+
/// Indicates whether this message holds a single CloudEvent.
41+
/// </summary>
42+
/// <remarks>
43+
/// This method returns false for batch requests, as they need to be parsed differently.
44+
/// </remarks>
45+
/// <param name="message">The message to check for the presence of a CloudEvent. Must not be null.</param>
46+
/// <typeparam name="TKey">The type of key of the Kafka message.</typeparam>
47+
/// <returns>true, if the request is a CloudEvent</returns>
3648
public static bool IsCloudEvent<TKey>(this Message<TKey, byte[]> message) =>
3749
GetHeaderValue(message, SpecVersionKafkaHeader) is object ||
3850
MimeUtilities.IsCloudEventsContentType(GetHeaderValue(message, KafkaContentTypeAttributeName));
Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
1+
// Copyright (c) Cloud Native Foundation.
2+
// Licensed under the Apache 2.0 license.
3+
// See LICENSE file in the project root for full license information.
4+
15
using System;
26

3-
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters
7+
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters;
8+
9+
/// <summary>
10+
/// Partion Key Adapter that converts to and from Guids in binary representation.
11+
/// </summary>
12+
public class BinaryGuidPartitionKeyAdapter : IPartitionKeyAdapter<byte[]?>
413
{
5-
/// <summary>
6-
/// Partion Key Adapter that converts to and from Guids in binary representation.
7-
/// </summary>
8-
public class BinaryGuidPartitionKeyAdapter : IPartitionKeyAdapter<byte[]?>
14+
/// <inheritdoc/>
15+
public bool ConvertKeyToPartitionKeyAttributeValue(byte[]? keyValue, out string? attributeValue)
916
{
10-
/// <inheritdoc/>
11-
public bool ConvertKeyToPartitionKeyAttributeValue(byte[]? keyValue, out string? attributeValue)
17+
if (keyValue == null)
1218
{
13-
if (keyValue == null)
14-
{
15-
attributeValue = null;
16-
return false;
17-
}
18-
19-
attributeValue = new Guid(keyValue).ToString();
20-
return true;
19+
attributeValue = null;
20+
return false;
2121
}
2222

23-
/// <inheritdoc/>
24-
public bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out byte[]? keyValue)
25-
{
26-
if (string.IsNullOrEmpty(attributeValue))
27-
{
28-
keyValue = default;
29-
return false;
30-
}
23+
attributeValue = new Guid(keyValue).ToString();
24+
return true;
25+
}
3126

32-
keyValue = Guid.Parse(attributeValue).ToByteArray();
33-
return true;
27+
/// <inheritdoc/>
28+
public bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out byte[]? keyValue)
29+
{
30+
if (string.IsNullOrEmpty(attributeValue))
31+
{
32+
keyValue = default;
33+
return false;
3434
}
35+
36+
keyValue = Guid.Parse(attributeValue).ToByteArray();
37+
return true;
3538
}
3639
}
Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters
1+
// Copyright (c) Cloud Native Foundation.
2+
// Licensed under the Apache 2.0 license.
3+
// See LICENSE file in the project root for full license information.
4+
5+
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters;
6+
7+
/// <summary>
8+
/// Defines the methods of the adapters responsible for transforming from cloud event
9+
/// PartitionKey Attribute to Kafka Message Key.
10+
/// </summary>
11+
/// <typeparam name="TKey">The type of Kafka Message Key.</typeparam>
12+
public interface IPartitionKeyAdapter<TKey>
213
{
314
/// <summary>
4-
/// Defines the methods of the adapters responsible for transforming from cloud event
5-
/// PartitionKey Attribute to Kafka Message Key.
15+
/// Converts a Message Key to PartionKey Attribute Value.
616
/// </summary>
7-
/// <typeparam name="TKey"></typeparam>
8-
public interface IPartitionKeyAdapter<TKey>
9-
{
10-
/// <summary>
11-
/// Converts a Message Key to PartionKey Attribute Value.
12-
/// </summary>
13-
/// <param name="keyValue">The key value to transform.</param>
14-
/// <param name="attributeValue">The transformed attribute value (output).</param>
15-
/// <returns>Whether the attribute should be set.</returns>
16-
bool ConvertKeyToPartitionKeyAttributeValue(TKey keyValue, out string? attributeValue);
17+
/// <param name="keyValue">The key value to transform.</param>
18+
/// <param name="attributeValue">The transformed attribute value (output).</param>
19+
/// <returns>Whether the attribute should be set.</returns>
20+
bool ConvertKeyToPartitionKeyAttributeValue(TKey keyValue, out string? attributeValue);
1721

18-
/// <summary>
19-
/// Converts a PartitionKey Attribute value to a Message Key.
20-
/// </summary>
21-
/// <param name="attributeValue">The attribute value to transform.</param>
22-
/// <param name="keyValue">The transformed key value (output)</param>
23-
/// <returns>Whether the key should be set.</returns>
24-
bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out TKey? keyValue);
25-
}
22+
/// <summary>
23+
/// Converts a PartitionKey Attribute value to a Message Key.
24+
/// </summary>
25+
/// <param name="attributeValue">The attribute value to transform.</param>
26+
/// <param name="keyValue">The transformed key value (output)</param>
27+
/// <returns>Whether the key should be set.</returns>
28+
bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out TKey? keyValue);
2629
}
Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters
1+
// Copyright (c) Cloud Native Foundation.
2+
// Licensed under the Apache 2.0 license.
3+
// See LICENSE file in the project root for full license information.
4+
5+
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters;
6+
7+
/// <summary>
8+
/// Partion Key Adapter that skips handling the key.
9+
/// </summary>
10+
/// <typeparam name="TKey">The type of Kafka Message Key.</typeparam>
11+
public class NullPartitionKeyAdapter<TKey> : IPartitionKeyAdapter<TKey>
212
{
3-
/// <summary>
4-
/// Partion Key Adapter that skips handling the key.
5-
/// </summary>
6-
/// <typeparam name="TKey">The type of Kafka Message Key</typeparam>
7-
public class NullPartitionKeyAdapter<TKey> : IPartitionKeyAdapter<TKey>
13+
/// <inheritdoc/>
14+
public bool ConvertKeyToPartitionKeyAttributeValue(TKey keyValue, out string? attributeValue)
815
{
9-
/// <inheritdoc/>
10-
public bool ConvertKeyToPartitionKeyAttributeValue(TKey keyValue, out string? attributeValue)
11-
{
12-
attributeValue = null;
13-
return false;
14-
}
16+
attributeValue = null;
17+
return false;
18+
}
1519

16-
/// <inheritdoc/>
17-
public bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out TKey? keyValue)
18-
{
19-
keyValue = default;
20-
return false;
21-
}
20+
/// <inheritdoc/>
21+
public bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out TKey? keyValue)
22+
{
23+
keyValue = default;
24+
return false;
2225
}
2326
}
Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters
2-
{
3-
/// <summary>
4-
/// Partion Key Adapter that skips handling the key.
5-
/// </summary>
6-
public class StringPartitionKeyAdapter : IPartitionKeyAdapter<string?>
7-
{
8-
/// <inheritdoc/>
9-
public bool ConvertKeyToPartitionKeyAttributeValue(string? keyValue, out string? attributeValue)
10-
{
11-
attributeValue = keyValue;
12-
return true;
13-
}
1+
// Copyright (c) Cloud Native Foundation.
2+
// Licensed under the Apache 2.0 license.
3+
// See LICENSE file in the project root for full license information.
144

15-
/// <inheritdoc/>
16-
public bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out string? keyValue)
17-
{
18-
keyValue = attributeValue;
19-
return true;
20-
}
5+
namespace CloudNative.CloudEvents.Kafka.PartitionKeyAdapters;
6+
7+
/// <summary>
8+
/// Partion Key Adapter that skips handling the key.
9+
/// </summary>
10+
public class StringPartitionKeyAdapter : IPartitionKeyAdapter<string?>
11+
{
12+
/// <inheritdoc/>
13+
public bool ConvertKeyToPartitionKeyAttributeValue(string? keyValue, out string? attributeValue)
14+
{
15+
attributeValue = keyValue;
16+
return true;
17+
}
18+
19+
/// <inheritdoc/>
20+
public bool ConvertPartitionKeyAttributeValueToKey(string? attributeValue, out string? keyValue)
21+
{
22+
keyValue = attributeValue;
23+
return true;
2124
}
2225
}

test/CloudNative.CloudEvents.UnitTests/Kafka/KafkaTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ public void KafkaBinaryGuidKeyedStructuredMessageTest()
140140
public void KafkaNullKeyedStructuredMessageTest()
141141
{
142142
// It will test the serialization using Confluent's Confluent.Kafka.Null type for the key.
143-
// As the default behavior without adapter is to skip the key it will work properly.
144143
var partitionKeyAdapter = new PartitionKeyAdapters.NullPartitionKeyAdapter<Confluent.Kafka.Null>();
145144
var jsonEventFormatter = new JsonEventFormatter();
146145
var cloudEvent = CreateTestCloudEvent();

0 commit comments

Comments
 (0)