Add hashbrown HashMap/HashSet implementations for Input/Output type.
This commit is contained in:
parent
f3d6f9319c
commit
807c9d142b
|
@ -33,6 +33,7 @@ async-stream = "0.3.0"
|
|||
async-trait = "0.1.48"
|
||||
fnv = "1.0.7"
|
||||
futures-util = { version = "0.3.0", default-features = false, features = ["io", "sink"] }
|
||||
hashbrown = { version="0.11.2", optional = true }
|
||||
indexmap = "1.6.2"
|
||||
once_cell = "1.7.2"
|
||||
pin-project-lite = "0.2.6"
|
||||
|
|
111
src/types/external/json_object/hashbrown_hashmap.rs
vendored
Normal file
111
src/types/external/json_object/hashbrown_hashmap.rs
vendored
Normal file
|
@ -0,0 +1,111 @@
|
|||
use std::borrow::Cow;
|
||||
use std::fmt::Display;
|
||||
use std::hash::Hash;
|
||||
use std::str::FromStr;
|
||||
|
||||
use async_graphql_parser::types::Field;
|
||||
use async_graphql_parser::Positioned;
|
||||
use async_graphql_value::{from_value, to_value};
|
||||
use hashbrown::HashMap;
|
||||
use indexmap::IndexMap;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::registry::{MetaType, Registry};
|
||||
use crate::{
|
||||
ContextSelectionSet, InputType, InputValueError, InputValueResult, Name, OutputType,
|
||||
ServerResult, Value,
|
||||
};
|
||||
|
||||
impl<K, V> InputType for HashMap<K, V>
|
||||
where
|
||||
K: ToString + FromStr + Eq + Hash + Send + Sync,
|
||||
K::Err: Display,
|
||||
V: Serialize + DeserializeOwned + Send + Sync,
|
||||
{
|
||||
type RawValueType = Self;
|
||||
|
||||
fn type_name() -> Cow<'static, str> {
|
||||
Cow::Borrowed("JSONObject")
|
||||
}
|
||||
|
||||
fn create_type_info(registry: &mut Registry) -> String {
|
||||
registry.create_input_type::<Self, _>(|_| MetaType::Scalar {
|
||||
name: <Self as InputType>::type_name().to_string(),
|
||||
description: Some("A scalar that can represent any JSON Object value."),
|
||||
is_valid: |_| true,
|
||||
visible: None,
|
||||
specified_by_url: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn parse(value: Option<Value>) -> InputValueResult<Self> {
|
||||
let value = value.unwrap_or_default();
|
||||
match value {
|
||||
Value::Object(map) => map
|
||||
.into_iter()
|
||||
.map(|(name, value)| {
|
||||
Ok((
|
||||
K::from_str(&name).map_err(|err| {
|
||||
InputValueError::<Self>::custom(format!("object key: {}", err))
|
||||
})?,
|
||||
from_value(value).map_err(|err| format!("object value: {}", err))?,
|
||||
))
|
||||
})
|
||||
.collect::<Result<_, _>>()
|
||||
.map_err(InputValueError::propagate),
|
||||
_ => Err(InputValueError::expected_type(value)),
|
||||
}
|
||||
}
|
||||
|
||||
fn to_value(&self) -> Value {
|
||||
let mut map = IndexMap::new();
|
||||
for (name, value) in self {
|
||||
map.insert(
|
||||
Name::new(name.to_string()),
|
||||
to_value(value).unwrap_or_default(),
|
||||
);
|
||||
}
|
||||
Value::Object(map)
|
||||
}
|
||||
|
||||
fn as_raw_value(&self) -> Option<&Self::RawValueType> {
|
||||
Some(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<K, V> OutputType for HashMap<K, V>
|
||||
where
|
||||
K: ToString + Eq + Hash + Send + Sync,
|
||||
V: Serialize + Send + Sync,
|
||||
{
|
||||
fn type_name() -> Cow<'static, str> {
|
||||
Cow::Borrowed("JSONObject")
|
||||
}
|
||||
|
||||
fn create_type_info(registry: &mut Registry) -> String {
|
||||
registry.create_output_type::<Self, _>(|_| MetaType::Scalar {
|
||||
name: <Self as OutputType>::type_name().to_string(),
|
||||
description: Some("A scalar that can represent any JSON Object value."),
|
||||
is_valid: |_| true,
|
||||
visible: None,
|
||||
specified_by_url: None,
|
||||
})
|
||||
}
|
||||
|
||||
async fn resolve(
|
||||
&self,
|
||||
_ctx: &ContextSelectionSet<'_>,
|
||||
_field: &Positioned<Field>,
|
||||
) -> ServerResult<Value> {
|
||||
let mut map = IndexMap::new();
|
||||
for (name, value) in self {
|
||||
map.insert(
|
||||
Name::new(name.to_string()),
|
||||
to_value(value).unwrap_or_default(),
|
||||
);
|
||||
}
|
||||
Ok(Value::Object(map))
|
||||
}
|
||||
}
|
2
src/types/external/json_object/mod.rs
vendored
2
src/types/external/json_object/mod.rs
vendored
|
@ -1,2 +1,4 @@
|
|||
mod btreemap;
|
||||
#[cfg(feature = "hashbrown")]
|
||||
mod hashbrown_hashmap;
|
||||
mod hashmap;
|
||||
|
|
76
src/types/external/list/hashbrown_hash_set.rs
vendored
Normal file
76
src/types/external/list/hashbrown_hash_set.rs
vendored
Normal file
|
@ -0,0 +1,76 @@
|
|||
use std::borrow::Cow;
|
||||
use std::cmp::Eq;
|
||||
use std::hash::Hash;
|
||||
|
||||
use hashbrown::HashSet;
|
||||
|
||||
use crate::parser::types::Field;
|
||||
use crate::resolver_utils::resolve_list;
|
||||
use crate::{
|
||||
registry, ContextSelectionSet, InputType, InputValueError, InputValueResult, OutputType,
|
||||
Positioned, Result, ServerResult, Value,
|
||||
};
|
||||
|
||||
impl<T: InputType + Hash + Eq> InputType for HashSet<T> {
|
||||
type RawValueType = Self;
|
||||
|
||||
fn type_name() -> Cow<'static, str> {
|
||||
Cow::Owned(format!("[{}]", T::qualified_type_name()))
|
||||
}
|
||||
|
||||
fn qualified_type_name() -> String {
|
||||
format!("[{}]!", T::qualified_type_name())
|
||||
}
|
||||
|
||||
fn create_type_info(registry: &mut registry::Registry) -> String {
|
||||
T::create_type_info(registry);
|
||||
Self::qualified_type_name()
|
||||
}
|
||||
|
||||
fn parse(value: Option<Value>) -> InputValueResult<Self> {
|
||||
match value.unwrap_or_default() {
|
||||
Value::List(values) => values
|
||||
.into_iter()
|
||||
.map(|value| InputType::parse(Some(value)))
|
||||
.collect::<Result<_, _>>()
|
||||
.map_err(InputValueError::propagate),
|
||||
value => Ok({
|
||||
let mut result = Self::default();
|
||||
result.insert(InputType::parse(Some(value)).map_err(InputValueError::propagate)?);
|
||||
result
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn to_value(&self) -> Value {
|
||||
Value::List(self.iter().map(InputType::to_value).collect())
|
||||
}
|
||||
|
||||
fn as_raw_value(&self) -> Option<&Self::RawValueType> {
|
||||
Some(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<T: OutputType + Hash + Eq> OutputType for HashSet<T> {
|
||||
fn type_name() -> Cow<'static, str> {
|
||||
Cow::Owned(format!("[{}]", T::qualified_type_name()))
|
||||
}
|
||||
|
||||
fn qualified_type_name() -> String {
|
||||
format!("[{}]!", T::qualified_type_name())
|
||||
}
|
||||
|
||||
fn create_type_info(registry: &mut registry::Registry) -> String {
|
||||
T::create_type_info(registry);
|
||||
Self::qualified_type_name()
|
||||
}
|
||||
|
||||
async fn resolve(
|
||||
&self,
|
||||
ctx: &ContextSelectionSet<'_>,
|
||||
field: &Positioned<Field>,
|
||||
) -> ServerResult<Value> {
|
||||
resolve_list(ctx, field, self, Some(self.len())).await
|
||||
}
|
||||
}
|
2
src/types/external/list/mod.rs
vendored
2
src/types/external/list/mod.rs
vendored
|
@ -1,6 +1,8 @@
|
|||
mod array;
|
||||
mod btree_set;
|
||||
mod hash_set;
|
||||
#[cfg(feature = "hashbrown")]
|
||||
mod hashbrown_hash_set;
|
||||
mod linked_list;
|
||||
mod slice;
|
||||
mod vec;
|
||||
|
|
Loading…
Reference in New Issue
Block a user