- 
                Notifications
    
You must be signed in to change notification settings  - Fork 87
 
feat: provide support for Kafka message keys different than "string?" #314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            Abuntxa
  wants to merge
  2
  commits into
  cloudevents:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
Abuntxa:support-kafka-key-types
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
          
            4 changes: 2 additions & 2 deletions
          
          4 
        
  src/CloudNative.CloudEvents.Kafka/CloudNative.CloudEvents.Kafka.csproj
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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 hidden or 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
    
  
  
    
              
        
          
          
            39 changes: 39 additions & 0 deletions
          
          39 
        
  src/CloudNative.CloudEvents.Kafka/PartitionKeyAdapters/BinaryGuidPartitionKeyAdapter.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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 | 
|---|---|---|
| @@ -0,0 +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; | ||
| 
     | 
||
| /// <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) | ||
| { | ||
| if (keyValue == null) | ||
| { | ||
| attributeValue = null; | ||
| return false; | ||
| } | ||
| 
     | 
||
| attributeValue = new Guid(keyValue).ToString(); | ||
| 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; | ||
| } | ||
| } | 
        
          
          
            29 changes: 29 additions & 0 deletions
          
          29 
        
  src/CloudNative.CloudEvents.Kafka/PartitionKeyAdapters/IPartitionKeyAdapter.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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 | 
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // 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> | ||
| /// 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> | ||
| /// 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); | ||
| } | 
        
          
          
            26 changes: 26 additions & 0 deletions
          
          26 
        
  src/CloudNative.CloudEvents.Kafka/PartitionKeyAdapters/NullPartitionKeyAdapter.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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 | 
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // 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> | ||
| { | ||
| /// <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; | ||
| } | ||
| } | 
        
          
          
            25 changes: 25 additions & 0 deletions
          
          25 
        
  src/CloudNative.CloudEvents.Kafka/PartitionKeyAdapters/StringPartitionKeyAdapter.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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 | 
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // 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?> | ||
| { | ||
| /// <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; | ||
| } | ||
| } | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.