Skip to content

Commit

Permalink
- revert: the trait impls
Browse files Browse the repository at this point in the history
  • Loading branch information
laststylebender14 committed Dec 9, 2024
1 parent e0c8e11 commit a29d2c5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
9 changes: 4 additions & 5 deletions src/core/blueprint/dynamic_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use async_graphql_value::{ConstValue, Name};
use indexmap::IndexMap;
use serde_json::Value;

use crate::core::json::JsonLike;
use crate::core::mustache::Mustache;

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -91,7 +90,7 @@ impl<A> DynamicValue<A> {
}
}

impl<A: for<'a> JsonLike<'a>> TryFrom<&Value> for DynamicValue<A> {
impl TryFrom<&Value> for DynamicValue<ConstValue> {
type Error = anyhow::Error;

fn try_from(value: &Value) -> Result<Self, Self::Error> {
Expand All @@ -105,19 +104,19 @@ impl<A: for<'a> JsonLike<'a>> TryFrom<&Value> for DynamicValue<A> {
Ok(DynamicValue::Object(out))
}
Value::Array(arr) => {
let out: Result<Vec<DynamicValue<A>>, Self::Error> =
let out: Result<Vec<DynamicValue<ConstValue>>, Self::Error> =
arr.iter().map(DynamicValue::try_from).collect();
Ok(DynamicValue::Array(out?))
}
Value::String(s) => {
let m = Mustache::parse(s.as_str());
if m.is_const() {
Ok(DynamicValue::Value(A::clone_from(value)))
Ok(DynamicValue::Value(ConstValue::from_json(value.clone())?))
} else {
Ok(DynamicValue::Mustache(m))
}
}
_ => Ok(DynamicValue::Value(A::clone_from(value))),
_ => Ok(DynamicValue::Value(ConstValue::from_json(value.clone())?)),
}
}
}
Expand Down
64 changes: 33 additions & 31 deletions src/core/serde_value_ext.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
use std::borrow::Cow;

use serde::de::DeserializeOwned;
use async_graphql::{Name, Value as GraphQLValue};
use indexmap::IndexMap;

use crate::core::blueprint::DynamicValue;
use crate::core::json::{JsonLike, JsonObjectLike};
use crate::core::path::PathString;

pub trait ValueExt {
type Output;
fn render_value(&self, ctx: &impl PathString) -> Self::Output;
fn render_value(&self, ctx: &impl PathString) -> GraphQLValue;
}

impl<A: for<'a> JsonLike<'a> + DeserializeOwned + Clone> ValueExt for DynamicValue<A> {
type Output = A;
fn render_value(&self, ctx: &impl PathString) -> Self::Output {
impl ValueExt for DynamicValue<async_graphql::Value> {
fn render_value<'a>(&self, ctx: &'a impl PathString) -> GraphQLValue {
match self {
DynamicValue::Value(value) => value.to_owned(),
DynamicValue::Mustache(m) => {
let rendered = m.render(ctx);
serde_json::from_str::<A>(rendered.as_ref())
let rendered: Cow<'a, str> = Cow::Owned(m.render(ctx));

serde_json::from_str::<GraphQLValue>(rendered.as_ref())
// parsing can fail when Mustache::render returns bare string and since
// that string is not wrapped with quotes serde_json will fail to parse it
// but, we can just use that string as is
.unwrap_or_else(|_| JsonLike::string(Cow::Owned(rendered)))
.unwrap_or_else(|_| GraphQLValue::String(rendered.into_owned()))
}
DynamicValue::Value(v) => v.clone(),
DynamicValue::Object(obj) => {
let mut storage = A::JsonObject::with_capacity(obj.len());
for (key, value) in obj.iter() {
let key = key.as_str();
let value = value.render_value(ctx);
storage.insert_key(key, value);
}

A::object(storage)
let out: IndexMap<_, _> = obj
.iter()
.map(|(k, v)| {
let key = Cow::Borrowed(k.as_str());
let val = v.render_value(ctx);

(Name::new(key), val)
})
.collect();

GraphQLValue::Object(out)
}
DynamicValue::Array(arr) => {
let out: Vec<_> = arr.iter().map(|v| v.render_value(ctx)).collect();
A::array(out)
GraphQLValue::List(out)
}
}
}
Expand All @@ -54,7 +56,7 @@ mod tests {
let value = json!({"a": "{{foo}}"});
let value = DynamicValue::try_from(&value).unwrap();
let ctx = json!({"foo": {"bar": "baz"}});
let result: async_graphql::Value = value.render_value(&ctx);
let result = value.render_value(&ctx);
let expected = async_graphql::Value::from_json(json!({"a": {"bar": "baz"}})).unwrap();
assert_eq!(result, expected);
}
Expand All @@ -64,7 +66,7 @@ mod tests {
let value = json!({"a": "{{foo.bar.baz}}"});
let value = DynamicValue::try_from(&value).unwrap();
let ctx = json!({"foo": {"bar": {"baz": 1}}});
let result: async_graphql::Value = value.render_value(&ctx);
let result = value.render_value(&ctx);
let expected = async_graphql::Value::from_json(json!({"a": 1})).unwrap();
assert_eq!(result, expected);
}
Expand All @@ -74,7 +76,7 @@ mod tests {
let value = json!({"a": "{{foo.bar.baz}}"});
let value = DynamicValue::try_from(&value).unwrap();
let ctx = json!({"foo": {"bar": {"baz": "foo"}}});
let result: async_graphql::Value = value.render_value(&ctx);
let result = value.render_value(&ctx);
let expected = async_graphql::Value::from_json(json!({"a": "foo"})).unwrap();
assert_eq!(result, expected);
}
Expand All @@ -84,7 +86,7 @@ mod tests {
let value = json!("{{foo.bar.baz}}");
let value = DynamicValue::try_from(&value).unwrap();
let ctx = json!({"foo": {"bar": {"baz": null}}});
let result: async_graphql::Value = value.render_value(&ctx);
let result = value.render_value(&ctx);
let expected = async_graphql::Value::from_json(json!(null)).unwrap();
assert_eq!(result, expected);
}
Expand All @@ -94,7 +96,7 @@ mod tests {
let value = json!({"a": "{{foo.bar.baz}}"});
let value = DynamicValue::try_from(&value).unwrap();
let ctx = json!({"foo": {"bar": {"baz": true}}});
let result: async_graphql::Value = value.render_value(&ctx);
let result = value.render_value(&ctx);
let expected = async_graphql::Value::from_json(json!({"a": true})).unwrap();
assert_eq!(result, expected);
}
Expand All @@ -104,7 +106,7 @@ mod tests {
let value = json!({"a": "{{foo.bar.baz}}"});
let value = DynamicValue::try_from(&value).unwrap();
let ctx = json!({"foo": {"bar": {"baz": 1.1}}});
let result: async_graphql::Value = value.render_value(&ctx);
let result = value.render_value(&ctx);
let expected = async_graphql::Value::from_json(json!({"a": 1.1})).unwrap();
assert_eq!(result, expected);
}
Expand All @@ -114,7 +116,7 @@ mod tests {
let value = json!({"a": "{{foo.bar.baz}}"});
let value = DynamicValue::try_from(&value).unwrap();
let ctx = json!({"foo": {"bar": {"baz": [1,2,3]}}});
let result: async_graphql::Value = value.render_value(&ctx);
let result = value.render_value(&ctx);
let expected = async_graphql::Value::from_json(json!({"a": [1, 2, 3]})).unwrap();
assert_eq!(result, expected);
}
Expand All @@ -124,7 +126,7 @@ mod tests {
let value = json!({"a": ["{{foo.bar.baz}}", "{{foo.bar.qux}}"]});
let value = DynamicValue::try_from(&value).unwrap();
let ctx = json!({"foo": {"bar": {"baz": 1, "qux": 2}}});
let result: async_graphql::Value = value.render_value(&ctx);
let result = value.render_value(&ctx);
let expected = async_graphql::Value::from_json(json!({"a": [1, 2]})).unwrap();
assert_eq!(result, expected);
}
Expand All @@ -134,7 +136,7 @@ mod tests {
let value = json!("{{foo}}");
let value = DynamicValue::try_from(&value).unwrap();
let ctx = json!({"foo": "bar"});
let result: async_graphql::Value = value.render_value(&ctx);
let result = value.render_value(&ctx);
let expected = async_graphql::Value::String("bar".to_owned());
assert_eq!(result, expected);
}
Expand All @@ -144,7 +146,7 @@ mod tests {
let value = json!([{"a": "{{foo.bar.baz}}"}, {"a": "{{foo.bar.qux}}"}]);
let value = DynamicValue::try_from(&value).unwrap();
let ctx = json!({"foo": {"bar": {"baz": 1, "qux": 2}}});
let result: async_graphql::Value = value.render_value(&ctx);
let result = value.render_value(&ctx);
let expected = async_graphql::Value::from_json(json!([{"a": 1}, {"a":2}])).unwrap();
assert_eq!(result, expected);
}
Expand All @@ -154,7 +156,7 @@ mod tests {
let value = json!([{"a": [{"aa": "{{foo.bar.baz}}"}]}, {"a": [{"aa": "{{foo.bar.qux}}"}]}]);
let value = DynamicValue::try_from(&value).unwrap();
let ctx = json!({"foo": {"bar": {"baz": 1, "qux": 2}}});
let result: async_graphql::Value = value.render_value(&ctx);
let result = value.render_value(&ctx);
let expected =
async_graphql::Value::from_json(json!([{"a": [{"aa": 1}]}, {"a":[{"aa": 2}]}]))
.unwrap();
Expand Down

0 comments on commit a29d2c5

Please sign in to comment.