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

JsonPathによるValueの分解 #72

Merged
merged 3 commits into from
Nov 10, 2023
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
76 changes: 76 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pulldown-cmark = "0.9.3"
regex = "1.10.1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_json_path = "0.6.4"
serde_regex = "1.1.0"
serde_yaml = "0.9.25"
tracing = "0.1.39"
Expand Down
11 changes: 5 additions & 6 deletions src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,11 @@ impl Pipeline {
if let Some(input) = store.get_combined(input) {
debug!("transform input type: {}", input.get_type_name());
let value = match with {
EnumTransformer::TemplateRenderer(template_renderer) => {
template_renderer
.transform(&input, &store)
.with_context(|| "transformer failed".to_string())?
}
};
EnumTransformer::TemplateRenderer(t) => t.transform(&input, &store),
EnumTransformer::JsonPath(t) => t.transform(&input, &store),
EnumTransformer::JsonPathAll(t) => t.transform(&input, &store),
}
.with_context(|| "transformer failed".to_string())?;
debug!("transform output type: {}", value.get_type_name());
store.set(output.to_string(), value);
} else {
Expand Down
4 changes: 3 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::TemplateRenderer;
use crate::transformer::{JsonPathQuery, JsonPathQueryAll, TemplateRenderer};

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

#[derive(Debug, Deserialize, Serialize)]
Expand Down
81 changes: 5 additions & 76 deletions src/transformer.rs
Original file line number Diff line number Diff line change
@@ -1,82 +1,11 @@
use crate::{store::Store, Value};

mod template_renderer {
use anyhow::Context;
use serde::{Deserialize, Serialize};

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

use super::Transformer;

#[derive(Debug, Deserialize, Serialize)]
pub struct TemplateRenderer {
pub template_key: String,
}

impl Transformer for TemplateRenderer {
fn transform(&self, value: &Value, store: &Store) -> anyhow::Result<Value> {
let mut engine = TemplateEngine::new();
if let Some(template_value) = store.get(&self.template_key) {
match template_value {
Value::JSON(serde_json::Value::String(string)) => {
engine
.register_template_from_string(&self.template_key, string.to_string())
.with_context(|| {
"failed to register template from string".to_string()
})?;
}
Value::Template(template) => {
engine.register_template(&self.template_key, template.clone());
}
_ => {
// TODO: すべてのanyhow::bailと.context、.with_contextに対してtracing::instruments(err)を付ける
anyhow::bail!(
"template value should be string or template, but it was {}",
template_value.get_type_name()
)
}
}
} else {
anyhow::bail!(
"no template value found for template_key {}",
self.template_key
)
}

let result_string = engine
.render(
&self.template_key,
&transform_value_for_rendering(value)
.context("failed to transform value for rendering")?,
)
.context("failed to render template")?;

Ok(Value::JSON(serde_json::Value::String(result_string)))
}
}

fn transform_value_for_rendering(value: &Value) -> anyhow::Result<serde_json::Value> {
match value {
Value::Bytes(bytes) => {
Ok(serde_json::to_value(bytes).context("failed to transform bytes to JSON")?)
}
Value::JSON(json) => match json {
serde_json::Value::Null => anyhow::bail!("failed to transform null to object"),
serde_json::Value::Bool(_)
| serde_json::Value::Number(_)
| serde_json::Value::String(_)
| serde_json::Value::Array(_) => {
let mut map = serde_json::Map::new();
map.insert(value.get_type_name().to_string(), json.clone());
Ok(serde_json::Value::Object(map))
}
serde_json::Value::Object(_) => Ok(json.clone()),
},
Value::Template(_) => anyhow::bail!("failed because template data is also template"),
}
}
}
mod json_path_query;
mod json_path_query_all;
mod template_renderer;

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

pub trait Transformer {
Expand Down
26 changes: 26 additions & 0 deletions src/transformer/json_path_query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use serde::{Deserialize, Serialize};
use serde_json_path::JsonPath;

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

use super::Transformer;

#[derive(Debug, Deserialize, Serialize)]
pub struct JsonPathQuery {
pub query: JsonPath,
}

impl Transformer for JsonPathQuery {
fn transform(&self, value: &Value, _store: &Store) -> anyhow::Result<Value> {
if let Value::JSON(json) = value {
let result = self
.query
.query(json)
.exactly_one()
.map_err(|e| anyhow::anyhow!("failed to find json path {e}"))?;
Ok(Value::JSON(result.clone()))
} else {
anyhow::bail!("value should be json, but it was {}", value.get_type_name())
}
}
}
30 changes: 30 additions & 0 deletions src/transformer/json_path_query_all.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use serde::{Deserialize, Serialize};
use serde_json_path::JsonPath;
use tracing::warn;

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

use super::Transformer;

#[derive(Debug, Deserialize, Serialize)]
pub struct JsonPathQueryAll {
pub query: JsonPath,
}

impl Transformer for JsonPathQueryAll {
#[tracing::instrument(skip(value, _store))]
fn transform(&self, value: &Value, _store: &Store) -> anyhow::Result<Value> {
if let Value::JSON(json) = value {
let result = self.query.query(json).all();
if result.is_empty() {
warn!("zero elements extracted");
}

Ok(Value::JSON(serde_json::Value::Array(
result.into_iter().map(ToOwned::to_owned).collect(),
)))
} else {
anyhow::bail!("value should be json, but it was {}", value.get_type_name())
}
}
}
72 changes: 72 additions & 0 deletions src/transformer/template_renderer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use anyhow::Context;
use serde::{Deserialize, Serialize};

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

use super::Transformer;

#[derive(Debug, Deserialize, Serialize)]
pub struct TemplateRenderer {
pub template_key: String,
}

impl Transformer for TemplateRenderer {
fn transform(&self, value: &Value, store: &Store) -> anyhow::Result<Value> {
let mut engine = TemplateEngine::new();
if let Some(template_value) = store.get(&self.template_key) {
match template_value {
Value::JSON(serde_json::Value::String(string)) => {
engine
.register_template_from_string(&self.template_key, string.to_string())
.with_context(|| "failed to register template from string".to_string())?;
}
Value::Template(template) => {
engine.register_template(&self.template_key, template.clone());
}
_ => {
// TODO: すべてのanyhow::bailと.context、.with_contextに対してtracing::instruments(err)を付ける
anyhow::bail!(
"template value should be string or template, but it was {}",
template_value.get_type_name()
)
}
}
} else {
anyhow::bail!(
"no template value found for template_key {}",
self.template_key
)
}

let result_string = engine
.render(
&self.template_key,
&transform_value_for_rendering(value)
.context("failed to transform value for rendering")?,
)
.context("failed to render template")?;

Ok(Value::JSON(serde_json::Value::String(result_string)))
}
}

fn transform_value_for_rendering(value: &Value) -> anyhow::Result<serde_json::Value> {
match value {
Value::Bytes(bytes) => {
Ok(serde_json::to_value(bytes).context("failed to transform bytes to JSON")?)
}
Value::JSON(json) => match json {
serde_json::Value::Null => anyhow::bail!("failed to transform null to object"),
serde_json::Value::Bool(_)
| serde_json::Value::Number(_)
| serde_json::Value::String(_)
| serde_json::Value::Array(_) => {
let mut map = serde_json::Map::new();
map.insert(value.get_type_name().to_string(), json.clone());
Ok(serde_json::Value::Object(map))
}
serde_json::Value::Object(_) => Ok(json.clone()),
},
Value::Template(_) => anyhow::bail!("failed because template data is also template"),
}
}
Loading