Skip to content

Commit

Permalink
Merge pull request #73 from yuma140902/text-to-template
Browse files Browse the repository at this point in the history
add TextAsTemplate transformer
  • Loading branch information
yuma140902 authored Nov 10, 2023
2 parents f1f895c + 4bae25b commit a8352ae
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ impl Pipeline {
EnumTransformer::TemplateRenderer(t) => t.transform(&input, &store),
EnumTransformer::JsonPath(t) => t.transform(&input, &store),
EnumTransformer::JsonPathAll(t) => t.transform(&input, &store),
EnumTransformer::TextAsTemplate(t) => t.transform(&input, &store),
}
.with_context(|| "transformer failed".to_string())?;
debug!("transform output type: {}", value.get_type_name());
Expand Down
3 changes: 2 additions & 1 deletion src/pipeline/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, path::PathBuf};

use serde::{Deserialize, Serialize};

use crate::transformer::{JsonPathQuery, JsonPathQueryAll, TemplateRenderer};
use crate::transformer::{JsonPathQuery, JsonPathQueryAll, TemplateRenderer, TextAsTemplate};

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
Expand Down Expand Up @@ -35,6 +35,7 @@ pub enum EnumTransformer {
TemplateRenderer(TemplateRenderer),
JsonPath(JsonPathQuery),
JsonPathAll(JsonPathQueryAll),
TextAsTemplate(TextAsTemplate),
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down
2 changes: 2 additions & 0 deletions src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use crate::{store::Store, Value};
mod json_path_query;
mod json_path_query_all;
mod template_renderer;
mod text_as_template;

pub use json_path_query::*;
pub use json_path_query_all::*;
pub use template_renderer::*;
pub use text_as_template::*;

pub trait Transformer {
fn transform(&self, value: &Value, store: &Store) -> anyhow::Result<Value>;
Expand Down
25 changes: 25 additions & 0 deletions src/transformer/text_as_template.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use anyhow::Context;
use handlebars::Template;
use serde::{Deserialize, Serialize};

use crate::{store::Store, Value};

use super::Transformer;

#[derive(Debug, Deserialize, Serialize)]
pub struct TextAsTemplate;

impl Transformer for TextAsTemplate {
fn transform(&self, value: &Value, _store: &Store) -> anyhow::Result<Value> {
if let Value::JSON(serde_json::Value::String(string)) = value {
Ok(Value::Template(Template::compile(string).with_context(
|| format!("failed to compile template {}", string),
)?))
} else {
anyhow::bail!(
"value should be string, but it was {}",
value.get_type_name()
)
}
}
}

0 comments on commit a8352ae

Please sign in to comment.