Skip to content

Commit

Permalink
feat(header): Implement find method on headers (#350)
Browse files Browse the repository at this point in the history
* feat(header): Implement find method on headers

Add the ability in the Headers struct of types to be able to find
a key and if found return the corresponding value.

* Cargo check

* Update rust-arroyo/src/backends/kafka/types.rs

Co-authored-by: Arpad Borsos <arpad.borsos@sentry.io>

* Update rust-arroyo/src/backends/kafka/types.rs

Co-authored-by: Arpad Borsos <arpad.borsos@sentry.io>

* Fix tests

* Use different way to assert in test

---------

Co-authored-by: Arpad Borsos <arpad.borsos@sentry.io>
  • Loading branch information
nikhars and Swatinem committed Apr 9, 2024
1 parent 26b8fc1 commit 5d67ae2
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions rust-arroyo/src/backends/kafka/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::types::TopicOrPartition;
use rdkafka::message::Headers as _;
use rdkafka::message::{BorrowedHeaders, Header, OwnedHeaders};
use rdkafka::producer::BaseRecord;

Expand All @@ -22,6 +23,13 @@ impl Headers {
});
Self { headers }
}

pub fn get(&self, key: &str) -> Option<&[u8]> {
self.headers
.iter()
.find(|header| header.key == key)
.and_then(|header| header.value)
}
}

impl Default for Headers {
Expand Down Expand Up @@ -143,4 +151,16 @@ mod tests {
assert_eq!(base_record.key, Some(&b"key".to_vec()));
assert_eq!(base_record.payload, Some(&b"message".to_vec()));
}

#[test]
fn test_headers() {
let mut headers = self::Headers::new();
headers = headers.insert("key1", Some(b"value1".to_vec()));
headers = headers.insert("key2", Some(b"value2".to_vec()));
headers = headers.insert("key3", Some(b"value3".to_vec()));

assert_eq!(headers.get("key1"), Some(b"value1").map(|v| v.as_ref()));
assert_eq!(headers.get("key2"), Some(b"value2").map(|v| v.as_ref()));
assert_eq!(headers.get("key10"), None);
}
}

0 comments on commit 5d67ae2

Please sign in to comment.