Remove the disgusting functions json_value_to_gql_value and gql_value_to_json_value.😂
This commit is contained in:
parent
7f7e708614
commit
0d540465a5
|
@ -16,3 +16,4 @@ categories = ["network-programming", "asynchronous"]
|
|||
pest = "2.1.3"
|
||||
pest_derive = "2.1.0"
|
||||
thiserror = "1.0.11"
|
||||
serde_json = "1.0.48"
|
||||
|
|
|
@ -115,3 +115,45 @@ impl fmt::Display for Value {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Value> for serde_json::Value {
|
||||
fn from(value: Value) -> Self {
|
||||
match value {
|
||||
Value::Null => serde_json::Value::Null,
|
||||
Value::Variable(name) => name.into(),
|
||||
Value::Int(n) => n.into(),
|
||||
Value::Float(n) => n.into(),
|
||||
Value::String(s) => s.into(),
|
||||
Value::Boolean(v) => v.into(),
|
||||
Value::Enum(e) => e.into(),
|
||||
Value::List(values) => values
|
||||
.into_iter()
|
||||
.map(Into::into)
|
||||
.collect::<Vec<serde_json::Value>>()
|
||||
.into(),
|
||||
Value::Object(obj) => serde_json::Value::Object(
|
||||
obj.into_iter()
|
||||
.map(|(name, value)| (name, value.into()))
|
||||
.collect(),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::Value> for Value {
|
||||
fn from(value: serde_json::Value) -> Self {
|
||||
match value {
|
||||
serde_json::Value::Null => Value::Null,
|
||||
serde_json::Value::Bool(n) => Value::Boolean(n),
|
||||
serde_json::Value::Number(n) if n.is_f64() => Value::Float(n.as_f64().unwrap()),
|
||||
serde_json::Value::Number(n) => Value::Int(n.as_i64().unwrap()),
|
||||
serde_json::Value::String(s) => Value::String(s),
|
||||
serde_json::Value::Array(ls) => Value::List(ls.into_iter().map(Into::into).collect()),
|
||||
serde_json::Value::Object(obj) => Value::Object(
|
||||
obj.into_iter()
|
||||
.map(|(name, value)| (name, value.into()))
|
||||
.collect(),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ impl DerefMut for Variables {
|
|||
impl Variables {
|
||||
/// Parse variables from JSON object.
|
||||
pub fn parse_from_json(value: serde_json::Value) -> Result<Self> {
|
||||
if let Value::Object(obj) = json_value_to_gql_value(value) {
|
||||
if let Value::Object(obj) = value.into() {
|
||||
Ok(Variables(Value::Object(obj)))
|
||||
} else {
|
||||
Ok(Default::default())
|
||||
|
@ -109,24 +109,6 @@ fn file_string(filename: &str, content_type: Option<&str>, path: &Path) -> Strin
|
|||
}
|
||||
}
|
||||
|
||||
fn json_value_to_gql_value(value: serde_json::Value) -> Value {
|
||||
match value {
|
||||
serde_json::Value::Null => Value::Null,
|
||||
serde_json::Value::Bool(n) => Value::Boolean(n),
|
||||
serde_json::Value::Number(n) if n.is_f64() => Value::Float(n.as_f64().unwrap()),
|
||||
serde_json::Value::Number(n) => Value::Int(n.as_i64().unwrap()),
|
||||
serde_json::Value::String(s) => Value::String(s),
|
||||
serde_json::Value::Array(ls) => {
|
||||
Value::List(ls.into_iter().map(json_value_to_gql_value).collect())
|
||||
}
|
||||
serde_json::Value::Object(obj) => Value::Object(
|
||||
obj.into_iter()
|
||||
.map(|(name, value)| (name, json_value_to_gql_value(value)))
|
||||
.collect(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
/// Schema/Context data
|
||||
pub struct Data(FnvHashMap<TypeId, Box<dyn Any + Sync + Send>>);
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use crate::{Result, ScalarType, Value};
|
||||
use async_graphql_derive::Scalar;
|
||||
use itertools::Itertools;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
/// Any scalar
|
||||
|
@ -28,7 +27,7 @@ impl ScalarType for Any {
|
|||
}
|
||||
|
||||
fn to_json(&self) -> Result<serde_json::Value> {
|
||||
Ok(gql_value_to_json_value(self.0.clone()))
|
||||
Ok(self.0.clone().into())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,28 +38,6 @@ impl Any {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn gql_value_to_json_value(value: Value) -> serde_json::Value {
|
||||
match value {
|
||||
Value::Null => serde_json::Value::Null,
|
||||
Value::Variable(name) => name.into(),
|
||||
Value::Int(n) => n.into(),
|
||||
Value::Float(n) => n.into(),
|
||||
Value::String(s) => s.into(),
|
||||
Value::Boolean(v) => v.into(),
|
||||
Value::Enum(e) => e.into(),
|
||||
Value::List(values) => values
|
||||
.into_iter()
|
||||
.map(gql_value_to_json_value)
|
||||
.collect_vec()
|
||||
.into(),
|
||||
Value::Object(obj) => serde_json::Value::Object(
|
||||
obj.into_iter()
|
||||
.map(|(k, v)| (k, gql_value_to_json_value(v)))
|
||||
.collect(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for Any
|
||||
where
|
||||
T: Into<Value>,
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
use crate::scalars::any::gql_value_to_json_value;
|
||||
use crate::{Result, ScalarType, Value};
|
||||
use async_graphql_derive::Scalar;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
@ -30,9 +29,7 @@ impl<T: DeserializeOwned + Serialize + Send + Sync> ScalarType for Json<T> {
|
|||
}
|
||||
|
||||
fn parse(value: &Value) -> Option<Self> {
|
||||
serde_json::from_value(gql_value_to_json_value(value.clone()))
|
||||
.map(Json)
|
||||
.ok()
|
||||
serde_json::from_value(value.clone().into()).map(Json).ok()
|
||||
}
|
||||
|
||||
fn to_json(&self) -> Result<serde_json::Value> {
|
||||
|
|
Loading…
Reference in New Issue
Block a user