-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a sink
folder
to write event into a folder (local, S3, ...)
- Loading branch information
Showing
4 changed files
with
110 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -407,3 +407,4 @@ report/ | |
megalinter-reports/ | ||
Chart.lock | ||
.aider* | ||
proptest-regressions/ |
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,80 @@ | ||
use crate::errors::Result; | ||
use crate::Message; | ||
use opendal::{Operator, Scheme}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_with::{serde_as, DisplayFromStr}; | ||
use std::collections::HashMap; | ||
|
||
use super::Sink; | ||
|
||
#[serde_as] | ||
#[derive(Clone, Debug, Deserialize, Serialize, Default)] | ||
pub(crate) struct Config { | ||
/// Is the sink is enabled? | ||
pub(crate) enabled: bool, | ||
#[serde_as(as = "DisplayFromStr")] | ||
kind: Scheme, | ||
parameters: HashMap<String, String>, | ||
} | ||
|
||
impl TryFrom<Config> for FolderSink { | ||
type Error = crate::errors::Error; | ||
|
||
fn try_from(value: Config) -> Result<Self> { | ||
let op = Operator::via_iter(value.kind, value.parameters.clone())?; | ||
Ok(Self { op }) | ||
} | ||
} | ||
|
||
pub(crate) struct FolderSink { | ||
op: Operator, | ||
} | ||
|
||
impl Sink for FolderSink { | ||
async fn send(&self, msg: &Message) -> Result<()> { | ||
let id = msg.cdevent.id(); | ||
let mut writer = self.op.writer(&format!("{id}.json")).await?; | ||
writer.write(serde_json::to_string(&msg.cdevent)?).await?; | ||
writer.close().await?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use assert2::let_assert; | ||
use cdevents_sdk::CDEvent; | ||
use test_strategy::proptest; | ||
|
||
#[proptest(async = "tokio", cases = 10)] | ||
//TODO reuse same sink for all tests (but be sure to drop it after) | ||
async fn test_send_valid(#[any] cdevent0: CDEvent) { | ||
// TODO allow any id (not only uuid) or change the policy in cdevents | ||
let cdevent = cdevent0.with_id(uuid::Uuid::new_v4().to_string().try_into().unwrap()); | ||
let tmp_dir = tempfile::tempdir().unwrap(); | ||
let config = Config { | ||
enabled: true, | ||
kind: Scheme::Fs, | ||
parameters: HashMap::from([( | ||
"root".to_string(), | ||
tmp_dir.path().to_string_lossy().to_string(), | ||
)]), | ||
}; | ||
let sink = FolderSink::try_from(config).unwrap(); | ||
|
||
let id = cdevent.id(); | ||
let msg = crate::Message { cdevent: cdevent.clone() }; | ||
let file = tmp_dir.path().join(format!("{id}.json")); | ||
assert!(!file.exists()); | ||
let_assert!(Ok(()) = sink.send(&msg).await); | ||
assert!(file.exists()); | ||
let example_txt = std::fs::read_to_string(file).unwrap(); | ||
let example_json: serde_json::Value = | ||
serde_json::from_str(&example_txt).expect("to parse as json"); | ||
let example: CDEvent = | ||
serde_json::from_value(example_json.clone()).expect("to parse as cdevent"); | ||
assert_eq!(example, cdevent); | ||
proptest::prop_assert!(true); | ||
} | ||
} |
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