Merge pull request #768 from BratSinot/hashbrown

Add hashbrown HashMap/HashSet implementations for Input/Output type.
This commit is contained in:
Sunli 2021-12-30 10:05:28 +08:00 committed by GitHub
commit 39bb6110e2
7 changed files with 185 additions and 0 deletions

View File

@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [3.0.19] 2021-12-28
- Add `InputType` / `OutputType` support for `hashbrown` crate.
## [3.0.18] 2021-12-26
- Federation's `_Entity` should not be sent if empty as it's in conflict with [GraphQL Union type validation](https://spec.graphql.org/draft/#sec-Unions.Type-Validation) [#765](https://github.com/async-graphql/async-graphql/pull/765).

View File

@ -52,6 +52,7 @@ num-traits = "0.2.14"
bson = { version = "2.0.0", optional = true, features = ["chrono-0_4"] }
chrono = { version = "0.4.19", optional = true }
chrono-tz = { version = "0.5.3", optional = true }
hashbrown = { version = "0.11.2", optional = true }
iso8601-duration = { version = "0.1.0", optional = true }
log = { version = "0.4.14", optional = true }
secrecy = { version = "0.7.0", optional = true }

View File

@ -74,6 +74,7 @@
//! - `decimal`: Integrate with the [`rust_decimal` crate](https://crates.io/crates/rust_decimal).
//! - `cbor`: Support for [serde_cbor](https://crates.io/crates/serde_cbor).
//! - `smol_str`: Integrate with the [`smol_str` crate](https://crates.io/crates/smol_str).
//! - `hashbrown`: Integrate with the [`hashbrown` crate](https://github.com/rust-lang/hashbrown).
//!
//! ## Integrations
//!

View File

@ -0,0 +1,100 @@
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 std::collections::HashMap as StdHashMap;
use crate::registry::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> {
<StdHashMap<K, V> as InputType>::type_name()
}
fn create_type_info(registry: &mut Registry) -> String {
<StdHashMap<K, V> as InputType>::create_type_info(registry)
}
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> {
<StdHashMap<K, V> as OutputType>::type_name()
}
fn create_type_info(registry: &mut Registry) -> String {
<StdHashMap<K, V> as OutputType>::create_type_info(registry)
}
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))
}
}

View File

@ -1,2 +1,4 @@
mod btreemap;
#[cfg(feature = "hashbrown")]
mod hashbrown_hashmap;
mod hashmap;

View File

@ -0,0 +1,75 @@
use std::borrow::Cow;
use std::cmp::Eq;
use std::collections::HashSet as StdHashSet;
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> {
<StdHashSet<T> as InputType>::type_name()
}
fn qualified_type_name() -> String {
<StdHashSet<T> as InputType>::qualified_type_name()
}
fn create_type_info(registry: &mut registry::Registry) -> String {
<StdHashSet<T> as InputType>::create_type_info(registry)
}
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> {
<StdHashSet<T> as OutputType>::type_name()
}
fn qualified_type_name() -> String {
<StdHashSet<T> as OutputType>::qualified_type_name()
}
fn create_type_info(registry: &mut registry::Registry) -> String {
<StdHashSet<T> as OutputType>::create_type_info(registry)
}
async fn resolve(
&self,
ctx: &ContextSelectionSet<'_>,
field: &Positioned<Field>,
) -> ServerResult<Value> {
resolve_list(ctx, field, self, Some(self.len())).await
}
}

View File

@ -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;