diff --git a/CHANGELOG.md b/CHANGELOG.md index e9ab3789..b1372807 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/Cargo.toml b/Cargo.toml index 08a5fa19..2ee53d6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/lib.rs b/src/lib.rs index c64c099e..c032f59e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 //! diff --git a/src/types/external/json_object/hashbrown_hashmap.rs b/src/types/external/json_object/hashbrown_hashmap.rs new file mode 100644 index 00000000..cf7ce7e0 --- /dev/null +++ b/src/types/external/json_object/hashbrown_hashmap.rs @@ -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 InputType for HashMap +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> { + as InputType>::type_name() + } + + fn create_type_info(registry: &mut Registry) -> String { + as InputType>::create_type_info(registry) + } + + fn parse(value: Option) -> InputValueResult { + 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::::custom(format!("object key: {}", err)) + })?, + from_value(value).map_err(|err| format!("object value: {}", err))?, + )) + }) + .collect::>() + .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 OutputType for HashMap +where + K: ToString + Eq + Hash + Send + Sync, + V: Serialize + Send + Sync, +{ + fn type_name() -> Cow<'static, str> { + as OutputType>::type_name() + } + + fn create_type_info(registry: &mut Registry) -> String { + as OutputType>::create_type_info(registry) + } + + async fn resolve( + &self, + _ctx: &ContextSelectionSet<'_>, + _field: &Positioned, + ) -> ServerResult { + 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)) + } +} diff --git a/src/types/external/json_object/mod.rs b/src/types/external/json_object/mod.rs index e579be5c..1a750794 100644 --- a/src/types/external/json_object/mod.rs +++ b/src/types/external/json_object/mod.rs @@ -1,2 +1,4 @@ mod btreemap; +#[cfg(feature = "hashbrown")] +mod hashbrown_hashmap; mod hashmap; diff --git a/src/types/external/list/hashbrown_hash_set.rs b/src/types/external/list/hashbrown_hash_set.rs new file mode 100644 index 00000000..22d89b00 --- /dev/null +++ b/src/types/external/list/hashbrown_hash_set.rs @@ -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 InputType for HashSet { + type RawValueType = Self; + + fn type_name() -> Cow<'static, str> { + as InputType>::type_name() + } + + fn qualified_type_name() -> String { + as InputType>::qualified_type_name() + } + + fn create_type_info(registry: &mut registry::Registry) -> String { + as InputType>::create_type_info(registry) + } + + fn parse(value: Option) -> InputValueResult { + match value.unwrap_or_default() { + Value::List(values) => values + .into_iter() + .map(|value| InputType::parse(Some(value))) + .collect::>() + .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 OutputType for HashSet { + fn type_name() -> Cow<'static, str> { + as OutputType>::type_name() + } + + fn qualified_type_name() -> String { + as OutputType>::qualified_type_name() + } + + fn create_type_info(registry: &mut registry::Registry) -> String { + as OutputType>::create_type_info(registry) + } + + async fn resolve( + &self, + ctx: &ContextSelectionSet<'_>, + field: &Positioned, + ) -> ServerResult { + resolve_list(ctx, field, self, Some(self.len())).await + } +} diff --git a/src/types/external/list/mod.rs b/src/types/external/list/mod.rs index ad015fe3..d80bb0c6 100644 --- a/src/types/external/list/mod.rs +++ b/src/types/external/list/mod.rs @@ -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;