From 1eb224b6c72f3a616474a5978a3dfced982c7690 Mon Sep 17 00:00:00 2001 From: Kiryl Mialeshka <8974488+meskill@users.noreply.github.com> Date: Tue, 3 Sep 2024 16:18:49 +0200 Subject: [PATCH] refactor: simplify json_like trait (#2780) --- src/core/ir/discriminator.rs | 1 - src/core/jit/common/jp.rs | 9 +-------- src/core/jit/exec.rs | 3 +-- src/core/jit/synth/synth.rs | 1 - src/core/json/borrow.rs | 12 ++++++------ src/core/json/graphql.rs | 12 ++++++------ src/core/json/json_like.rs | 27 ++++++--------------------- src/core/json/serde.rs | 12 ++++++------ src/core/scalar.rs | 8 ++++---- 9 files changed, 30 insertions(+), 55 deletions(-) diff --git a/src/core/ir/discriminator.rs b/src/core/ir/discriminator.rs index ee8f08057c..127cc59414 100644 --- a/src/core/ir/discriminator.rs +++ b/src/core/ir/discriminator.rs @@ -23,7 +23,6 @@ const TYPENAME_FIELD: &str = "__typename"; impl<'json, T> TypedValue<'json> for T where T: JsonLike<'json>, - T::JsonObject<'json>: JsonObjectLike<'json, Value = T>, { type Error = anyhow::Error; diff --git a/src/core/jit/common/jp.rs b/src/core/jit/common/jp.rs index 521ecdfc8c..815d3a5311 100644 --- a/src/core/jit/common/jp.rs +++ b/src/core/jit/common/jp.rs @@ -83,14 +83,7 @@ impl<'a, Value: JsonLike<'a> + Deserialize<'a> + Clone + 'a> TestData { } } -impl< - 'a, - Value: Deserialize<'a> - + Clone - + 'a - + JsonLike<'a, JsonObject<'a>: JsonObjectLike<'a, Value = Value>>, - > JP -{ +impl<'a, Value: Deserialize<'a> + Clone + 'a + JsonLike<'a>> JP { const CONFIG: &'static str = include_str!("../fixtures/jsonplaceholder-mutation.graphql"); fn plan(query: &str, variables: &Variables) -> OperationPlan { diff --git a/src/core/jit/exec.rs b/src/core/jit/exec.rs index ee9a890bb2..f8d0da9331 100644 --- a/src/core/jit/exec.rs +++ b/src/core/jit/exec.rs @@ -25,8 +25,7 @@ pub struct Executor { impl Executor where - Output: - for<'b> JsonLike<'b, JsonObject<'b>: JsonObjectLike<'b, Value = Output>> + Debug + Clone, + Output: for<'b> JsonLike<'b> + Debug + Clone, Input: Clone + Debug, Exec: IRExecutor, { diff --git a/src/core/jit/synth/synth.rs b/src/core/jit/synth/synth.rs index 446fc8f00a..b5a9d4c7ad 100644 --- a/src/core/jit/synth/synth.rs +++ b/src/core/jit/synth/synth.rs @@ -26,7 +26,6 @@ impl Synth { impl<'a, Value> Synth where Value: JsonLike<'a> + Clone + std::fmt::Debug, - Value::JsonObject<'a>: JsonObjectLike<'a, Value = Value>, { #[inline(always)] fn include(&self, field: &Field) -> bool { diff --git a/src/core/json/borrow.rs b/src/core/json/borrow.rs index 59fd376d7c..1a38196ca8 100644 --- a/src/core/json/borrow.rs +++ b/src/core/json/borrow.rs @@ -12,7 +12,7 @@ impl<'ctx> JsonObjectLike<'ctx> for ObjectAsVec<'ctx> { ObjectAsVec::default() } - fn get_key(&'ctx self, key: &str) -> Option<&Value> { + fn get_key(&self, key: &str) -> Option<&Self::Value> { self.get(key) } @@ -22,13 +22,13 @@ impl<'ctx> JsonObjectLike<'ctx> for ObjectAsVec<'ctx> { } impl<'ctx> JsonLike<'ctx> for Value<'ctx> { - type JsonObject<'obj> = ObjectAsVec<'obj>; + type JsonObject = ObjectAsVec<'ctx>; fn null() -> Self { Value::Null } - fn object(obj: Self::JsonObject<'ctx>) -> Self { + fn object(obj: Self::JsonObject) -> Self { Value::Object(obj) } @@ -54,18 +54,18 @@ impl<'ctx> JsonLike<'ctx> for Value<'ctx> { } } - fn as_object(&self) -> Option<&Self::JsonObject<'_>> { + fn as_object(&self) -> Option<&Self::JsonObject> { self.as_object() } - fn as_object_mut(&mut self) -> Option<&mut Self::JsonObject<'ctx>> { + fn as_object_mut(&mut self) -> Option<&mut Self::JsonObject> { match self { Value::Object(obj) => Some(obj), _ => None, } } - fn into_object(self) -> Option> { + fn into_object(self) -> Option { match self { Value::Object(obj) => Some(obj), _ => None, diff --git a/src/core/json/graphql.rs b/src/core/json/graphql.rs index a41609a80a..aa049a8ed8 100644 --- a/src/core/json/graphql.rs +++ b/src/core/json/graphql.rs @@ -14,7 +14,7 @@ impl<'obj, Value: JsonLike<'obj> + Clone> JsonObjectLike<'obj> for IndexMap Option<&Self::Value> { + fn get_key(&self, key: &str) -> Option<&Self::Value> { self.get(key) } @@ -24,7 +24,7 @@ impl<'obj, Value: JsonLike<'obj> + Clone> JsonObjectLike<'obj> for IndexMap JsonLike<'json> for ConstValue { - type JsonObject<'obj> = IndexMap; + type JsonObject = IndexMap; fn as_array(&self) -> Option<&Vec> { match self { @@ -110,28 +110,28 @@ impl<'json> JsonLike<'json> for ConstValue { Default::default() } - fn as_object(&self) -> Option<&Self::JsonObject<'_>> { + fn as_object(&self) -> Option<&Self::JsonObject> { match self { ConstValue::Object(map) => Some(map), _ => None, } } - fn as_object_mut(&mut self) -> Option<&mut Self::JsonObject<'_>> { + fn as_object_mut(&mut self) -> Option<&mut Self::JsonObject> { match self { ConstValue::Object(map) => Some(map), _ => None, } } - fn into_object(self) -> Option> { + fn into_object(self) -> Option { match self { ConstValue::Object(map) => Some(map), _ => None, } } - fn object(obj: Self::JsonObject<'json>) -> Self { + fn object(obj: Self::JsonObject) -> Self { ConstValue::Object(obj) } diff --git a/src/core/json/json_like.rs b/src/core/json/json_like.rs index 2b10d39eb0..6452036e4a 100644 --- a/src/core/json/json_like.rs +++ b/src/core/json/json_like.rs @@ -6,35 +6,20 @@ impl JsonLikeOwned for T where T: for<'json> JsonLike<'json> {} /// A trait for objects that can be used as JSON values pub trait JsonLike<'json>: Sized { - type JsonObject<'obj>: JsonObjectLike< - 'obj, - // generally we want to specify `Self` instead of generic here - // and `Self` is used anyway through JsonObjectLike for - // current implementations. - // But `Self` means the very specific type with some specific lifetime - // which doesn't work in case we want to return self type but with different - // lifetime. Currently, it affects only `as_object` fn because `serde_json_borrow` - // returns smaller lifetime for Value in its `as_object` fn that either forces to - // use `&'json self` in the fn (that leads to error "variable does not live long enough") - // or generic like this. - // TODO: perhaps it could be fixed on `serde_json_borrow` side if we return `Value<'ctx>` - // instead of `Value<'_>` in its functions like `as_object`. In that case we can specify - // `Self` here and simplify usages of this trait - Value: JsonLike<'obj>, - >; + type JsonObject: JsonObjectLike<'json, Value = Self>; // Constructors fn null() -> Self; - fn object(obj: Self::JsonObject<'json>) -> Self; + fn object(obj: Self::JsonObject) -> Self; fn array(arr: Vec) -> Self; fn string(s: Cow<'json, str>) -> Self; // Operators fn as_array(&self) -> Option<&Vec>; fn into_array(self) -> Option>; - fn as_object(&self) -> Option<&Self::JsonObject<'_>>; - fn as_object_mut(&mut self) -> Option<&mut Self::JsonObject<'json>>; - fn into_object(self) -> Option>; + fn as_object(&self) -> Option<&Self::JsonObject>; + fn as_object_mut(&mut self) -> Option<&mut Self::JsonObject>; + fn into_object(self) -> Option; fn as_str(&self) -> Option<&str>; fn as_i64(&self) -> Option; fn as_u64(&self) -> Option; @@ -50,7 +35,7 @@ pub trait JsonLike<'json>: Sized { pub trait JsonObjectLike<'obj>: Sized { type Value; fn new() -> Self; - fn get_key(&'obj self, key: &str) -> Option<&Self::Value>; + fn get_key(&self, key: &str) -> Option<&Self::Value>; fn insert_key(&mut self, key: &'obj str, value: Self::Value); } diff --git a/src/core/json/serde.rs b/src/core/json/serde.rs index 8ee36499c8..4f6fad6609 100644 --- a/src/core/json/serde.rs +++ b/src/core/json/serde.rs @@ -10,7 +10,7 @@ impl<'obj> JsonObjectLike<'obj> for serde_json::Map { serde_json::Map::new() } - fn get_key(&'obj self, key: &str) -> Option<&serde_json::Value> { + fn get_key(&self, key: &str) -> Option<&serde_json::Value> { self.get(key) } @@ -20,7 +20,7 @@ impl<'obj> JsonObjectLike<'obj> for serde_json::Map { } impl<'json> JsonLike<'json> for serde_json::Value { - type JsonObject<'obj> = serde_json::Map; + type JsonObject = serde_json::Map; fn as_array(&self) -> Option<&Vec> { self.as_array() @@ -89,15 +89,15 @@ impl<'json> JsonLike<'json> for serde_json::Value { Self::Null } - fn as_object(&self) -> Option<&Self::JsonObject<'_>> { + fn as_object(&self) -> Option<&Self::JsonObject> { self.as_object() } - fn as_object_mut(&mut self) -> Option<&mut Self::JsonObject<'_>> { + fn as_object_mut(&mut self) -> Option<&mut Self::JsonObject> { self.as_object_mut() } - fn into_object(self) -> Option> { + fn into_object(self) -> Option { if let Self::Object(obj) = self { Some(obj) } else { @@ -105,7 +105,7 @@ impl<'json> JsonLike<'json> for serde_json::Value { } } - fn object(obj: Self::JsonObject<'json>) -> Self { + fn object(obj: Self::JsonObject) -> Self { serde_json::Value::Object(obj) } diff --git a/src/core/scalar.rs b/src/core/scalar.rs index 1cffeffb39..44e7a4b35a 100644 --- a/src/core/scalar.rs +++ b/src/core/scalar.rs @@ -75,14 +75,14 @@ pub enum Scalar { Bytes, } -fn eval_str<'a, Value: JsonLike<'a> + 'a, F: Fn(&str) -> bool>(val: &'a Value, fxn: F) -> bool { +fn eval_str<'a, Value: JsonLike<'a>, F: Fn(&str) -> bool>(val: &'a Value, fxn: F) -> bool { val.as_str().map_or(false, fxn) } fn eval_signed< 'a, Num, - Value: JsonLike<'a> + 'a, + Value: JsonLike<'a>, F: Fn(i64) -> Result, >( val: &'a Value, @@ -94,7 +94,7 @@ fn eval_signed< fn eval_unsigned< 'a, Num, - Value: JsonLike<'a> + 'a, + Value: JsonLike<'a>, F: Fn(u64) -> Result, >( val: &'a Value, @@ -114,7 +114,7 @@ impl Scalar { } } - pub fn validate<'a, Value: JsonLike<'a> + 'a>(&self, value: &'a Value) -> bool { + pub fn validate<'a, Value: JsonLike<'a>>(&self, value: &'a Value) -> bool { match self { Scalar::JSON => true, Scalar::Empty => true,