From a29d2c55572c4423aa1bebf3e2553d0f5941dbeb Mon Sep 17 00:00:00 2001 From: laststylebender Date: Mon, 9 Dec 2024 12:27:52 +0530 Subject: [PATCH] - revert: the trait impls --- src/core/blueprint/dynamic_value.rs | 9 ++-- src/core/serde_value_ext.rs | 64 +++++++++++++++-------------- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/core/blueprint/dynamic_value.rs b/src/core/blueprint/dynamic_value.rs index a9884cb37b..1900b73566 100644 --- a/src/core/blueprint/dynamic_value.rs +++ b/src/core/blueprint/dynamic_value.rs @@ -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)] @@ -91,7 +90,7 @@ impl DynamicValue { } } -impl JsonLike<'a>> TryFrom<&Value> for DynamicValue { +impl TryFrom<&Value> for DynamicValue { type Error = anyhow::Error; fn try_from(value: &Value) -> Result { @@ -105,19 +104,19 @@ impl JsonLike<'a>> TryFrom<&Value> for DynamicValue { Ok(DynamicValue::Object(out)) } Value::Array(arr) => { - let out: Result>, Self::Error> = + let out: Result>, 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())?)), } } } diff --git a/src/core/serde_value_ext.rs b/src/core/serde_value_ext.rs index eae673d403..37a095ac6c 100644 --- a/src/core/serde_value_ext.rs +++ b/src/core/serde_value_ext.rs @@ -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 JsonLike<'a> + DeserializeOwned + Clone> ValueExt for DynamicValue { - type Output = A; - fn render_value(&self, ctx: &impl PathString) -> Self::Output { +impl ValueExt for DynamicValue { + 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::(rendered.as_ref()) + let rendered: Cow<'a, str> = Cow::Owned(m.render(ctx)); + + serde_json::from_str::(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) } } } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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();