-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a deserialization target that retains the ValidatedMessage metad…
…ata (#45) Currently the consume method on the MessageStream drops all of the ValidatedMessage metadata, this PR adds a wrapper type that enables clients to opt into deserializing into a wrapper type that includes the ValidatedMessage metadata fields alongside the target decoded message
- Loading branch information
Showing
5 changed files
with
144 additions
and
86 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use bytes::Bytes; | ||
use std::{borrow::Cow, time::SystemTime}; | ||
use uuid::Uuid; | ||
|
||
use crate::Headers; | ||
|
||
/// A validated message. | ||
/// | ||
/// These are created by validators after encoding a user message, or when pulling messages from | ||
/// the message service. | ||
#[derive(Debug, Clone)] | ||
// derive Eq only in tests so that users can't foot-shoot an expensive == over data | ||
#[cfg_attr(test, derive(PartialEq, Eq))] | ||
pub struct ValidatedMessage<M> { | ||
/// Unique message identifier. | ||
pub(crate) id: Uuid, | ||
/// The timestamp when message was created in the publishing service. | ||
pub(crate) timestamp: SystemTime, | ||
/// URI of the schema validating this message. | ||
/// | ||
/// E.g. `https://hedwig.domain.xyz/schemas#/schemas/user.created/1.0` | ||
pub(crate) schema: Cow<'static, str>, | ||
/// Custom message headers. | ||
/// | ||
/// This may be used to track request_id, for example. | ||
pub(crate) headers: Headers, | ||
/// The message data. | ||
pub(crate) data: M, | ||
} | ||
|
||
impl ValidatedMessage<Bytes> { | ||
/// Create a new validated message | ||
pub fn new<S, D>(id: Uuid, timestamp: SystemTime, schema: S, headers: Headers, data: D) -> Self | ||
where | ||
S: Into<Cow<'static, str>>, | ||
D: Into<Bytes>, | ||
{ | ||
Self { | ||
id, | ||
timestamp, | ||
schema: schema.into(), | ||
headers, | ||
data: data.into(), | ||
} | ||
} | ||
} | ||
|
||
impl<M> ValidatedMessage<M> { | ||
/// Unique message identifier. | ||
pub fn uuid(&self) -> &Uuid { | ||
&self.id | ||
} | ||
|
||
/// The timestamp when message was created in the publishing service. | ||
pub fn timestamp(&self) -> &SystemTime { | ||
&self.timestamp | ||
} | ||
|
||
/// URI of the schema validating this message. | ||
/// | ||
/// E.g. `https://hedwig.domain.xyz/schemas#/schemas/user.created/1.0` | ||
pub fn schema(&self) -> &str { | ||
&self.schema | ||
} | ||
|
||
/// Custom message headers. | ||
/// | ||
/// This may be used to track request_id, for example. | ||
pub fn headers(&self) -> &Headers { | ||
&self.headers | ||
} | ||
|
||
/// Mutable access to the message headers | ||
pub fn headers_mut(&mut self) -> &mut Headers { | ||
&mut self.headers | ||
} | ||
|
||
/// The message data. | ||
pub fn data(&self) -> &M { | ||
&self.data | ||
} | ||
|
||
/// Destructure this message into just the contained data | ||
pub fn into_data(self) -> M { | ||
self.data | ||
} | ||
} |
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