Skip to content
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

feat: develop values template render #9

Merged
merged 3 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
**Code**: [![codecov](https://codecov.io/gh/zefiroproj/zefiro/graph/badge.svg?token=5DgmM1KzuQ)](https://codecov.io/gh/zefiroproj/zefiro)
[![codecov](https://codecov.io/gh/zefiroproj/zefiro/graph/badge.svg?token=5DgmM1KzuQ)](https://codecov.io/gh/zefiroproj/zefiro)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fzefiroproj%2Fzefiro.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fzefiroproj%2Fzefiro?ref=badge_shield)

# Zefiro - fast, scalable and simple engine to manage workflows

⚠️ This project is a Work In Progress, and is very far from being suitable for use ⚠️

## License
## License Compliance
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fzefiroproj%2Fzefiro.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fzefiroproj%2Fzefiro?ref=badge_large)
1 change: 1 addition & 0 deletions zefiro-core/zefiro-cwl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ serde_json = "1.0.135"
serde_with = "3.12.0"
serde_yaml = "0.9.34"
sha1 = "0.10.6"
tera = "1.20.0"

[dev-dependencies]
rstest = "0.24.0"
Expand Down
1 change: 0 additions & 1 deletion zefiro-core/zefiro-cwl/src/exprs/mod.rs

This file was deleted.

1 change: 1 addition & 0 deletions zefiro-core/zefiro-cwl/src/js/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod execute;
6 changes: 4 additions & 2 deletions zefiro-core/zefiro-cwl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
pub mod exprs;
pub mod js;
#[doc = include_str!("../README.md")]
pub mod schema;
pub mod template;
pub mod values;

pub use crate::exprs::exec::JsExecutor;
pub use crate::js::execute::JsExecutor;
pub use crate::schema::document::CwlSchema;
pub use crate::template::render::TemplateRender;
pub use crate::values::document::CwlValues;
1 change: 1 addition & 0 deletions zefiro-core/zefiro-cwl/src/template/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod render;
82 changes: 82 additions & 0 deletions zefiro-core/zefiro-cwl/src/template/render.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use anyhow::Error;
use serde_json::Value;
use tera::{Context, Tera};

pub struct TemplateRender {
content: Value,
tera: Tera,
}

impl TemplateRender {
pub fn new(content: Value, template: &str) -> Result<Self, Error> {
let mut tera = Tera::default();
tera.add_raw_template("template", template)?;
Ok(Self { content, tera })
}

pub fn render(&self) -> Result<String, Error> {
let mut context = Context::new();
let object = self.content.as_object().ok_or_else(|| {
anyhow::anyhow!("Content must be a JSON object, got: {}", self.content)
})?;
for (key, value) in object {
context.insert(key, value);
}
let result = self.tera.render("template", &context)?;

Ok(result)
}
}

#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
use serde_json::json;

#[rstest]
#[case(
json!({"inputLocation": "s3://bucket"}),
r#"
input_file:
class: location
location: {{ inputLocation }}/dir/input.txt
"#,
r#"
input_file:
class: location
location: s3://bucket/dir/input.txt
"#,
)]
#[case(
json!({
"inputLocation": "s3://bucket",
"suffixes": [1, 2, 3]
}),
r#"
input_file:
{% for suffix in suffixes %}
- class: File
location: {{ inputLocation }}/dir/input-{{ suffix }}.txt
{% endfor %}
"#,
r#"
input_file:

- class: File
location: s3://bucket/dir/input-1.txt

- class: File
location: s3://bucket/dir/input-2.txt

- class: File
location: s3://bucket/dir/input-3.txt

"#,
)]
fn test_render(#[case] content: Value, #[case] template: &str, #[case] expected: &str) {
let template_render = TemplateRender::new(content.clone(), template).unwrap();
let rendered = template_render.render().unwrap();
assert_eq!(rendered, expected);
}
}
Loading