From 4d37f4625db3285fc4d921e9eff7a4b67aa6f28d Mon Sep 17 00:00:00 2001 From: BratSinot Date: Tue, 28 Dec 2021 12:11:40 +0200 Subject: [PATCH 1/5] Add hashbrown HashMap/HashSet implementations for Input/Output type. --- Cargo.toml | 1 + .../external/json_object/hashbrown_hashmap.rs | 111 ++++++++++++++++++ src/types/external/json_object/mod.rs | 2 + src/types/external/list/hashbrown_hash_set.rs | 76 ++++++++++++ src/types/external/list/mod.rs | 2 + 5 files changed, 192 insertions(+) create mode 100644 src/types/external/json_object/hashbrown_hashmap.rs create mode 100644 src/types/external/list/hashbrown_hash_set.rs diff --git a/Cargo.toml b/Cargo.toml index 08a5fa19..9c2b54e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" 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..d853adf6 --- /dev/null +++ b/src/types/external/json_object/hashbrown_hashmap.rs @@ -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 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> { + Cow::Borrowed("JSONObject") + } + + fn create_type_info(registry: &mut Registry) -> String { + registry.create_input_type::(|_| MetaType::Scalar { + name: ::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) -> 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> { + Cow::Borrowed("JSONObject") + } + + fn create_type_info(registry: &mut Registry) -> String { + registry.create_output_type::(|_| MetaType::Scalar { + name: ::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, + ) -> 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..98dfde2b --- /dev/null +++ b/src/types/external/list/hashbrown_hash_set.rs @@ -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 InputType for HashSet { + 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) -> 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> { + 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, + ) -> 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; From 1ab7dd27e54af8a844b9c6ce259598a55540afe6 Mon Sep 17 00:00:00 2001 From: BratSinot Date: Tue, 28 Dec 2021 12:25:30 +0200 Subject: [PATCH 2/5] Remove some copy-paste. --- .../external/json_object/hashbrown_hashmap.rs | 21 +++++-------------- src/types/external/list/hashbrown_hash_set.rs | 15 +++++++------ 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/types/external/json_object/hashbrown_hashmap.rs b/src/types/external/json_object/hashbrown_hashmap.rs index d853adf6..c805c823 100644 --- a/src/types/external/json_object/hashbrown_hashmap.rs +++ b/src/types/external/json_object/hashbrown_hashmap.rs @@ -7,6 +7,7 @@ use async_graphql_parser::types::Field; use async_graphql_parser::Positioned; use async_graphql_value::{from_value, to_value}; use hashbrown::HashMap; +use std::collections::HashMap as StdHashMap; use indexmap::IndexMap; use serde::de::DeserializeOwned; use serde::Serialize; @@ -26,17 +27,11 @@ impl InputType for HashMap type RawValueType = Self; fn type_name() -> Cow<'static, str> { - Cow::Borrowed("JSONObject") + as InputType>::type_name() } fn create_type_info(registry: &mut Registry) -> String { - registry.create_input_type::(|_| MetaType::Scalar { - name: ::type_name().to_string(), - description: Some("A scalar that can represent any JSON Object value."), - is_valid: |_| true, - visible: None, - specified_by_url: None, - }) + as InputType>::create_type_info(registry) } fn parse(value: Option) -> InputValueResult { @@ -81,17 +76,11 @@ impl OutputType for HashMap V: Serialize + Send + Sync, { fn type_name() -> Cow<'static, str> { - Cow::Borrowed("JSONObject") + as OutputType>::type_name() } fn create_type_info(registry: &mut Registry) -> String { - registry.create_output_type::(|_| MetaType::Scalar { - name: ::type_name().to_string(), - description: Some("A scalar that can represent any JSON Object value."), - is_valid: |_| true, - visible: None, - specified_by_url: None, - }) + as OutputType>::create_type_info(registry) } async fn resolve( diff --git a/src/types/external/list/hashbrown_hash_set.rs b/src/types/external/list/hashbrown_hash_set.rs index 98dfde2b..22d89b00 100644 --- a/src/types/external/list/hashbrown_hash_set.rs +++ b/src/types/external/list/hashbrown_hash_set.rs @@ -1,5 +1,6 @@ use std::borrow::Cow; use std::cmp::Eq; +use std::collections::HashSet as StdHashSet; use std::hash::Hash; use hashbrown::HashSet; @@ -15,16 +16,15 @@ impl InputType for HashSet { type RawValueType = Self; fn type_name() -> Cow<'static, str> { - Cow::Owned(format!("[{}]", T::qualified_type_name())) + as InputType>::type_name() } fn qualified_type_name() -> String { - format!("[{}]!", T::qualified_type_name()) + as InputType>::qualified_type_name() } fn create_type_info(registry: &mut registry::Registry) -> String { - T::create_type_info(registry); - Self::qualified_type_name() + as InputType>::create_type_info(registry) } fn parse(value: Option) -> InputValueResult { @@ -54,16 +54,15 @@ impl InputType for HashSet { #[async_trait::async_trait] impl OutputType for HashSet { fn type_name() -> Cow<'static, str> { - Cow::Owned(format!("[{}]", T::qualified_type_name())) + as OutputType>::type_name() } fn qualified_type_name() -> String { - format!("[{}]!", T::qualified_type_name()) + as OutputType>::qualified_type_name() } fn create_type_info(registry: &mut registry::Registry) -> String { - T::create_type_info(registry); - Self::qualified_type_name() + as OutputType>::create_type_info(registry) } async fn resolve( From 97c56f1d7644ceab8e18eb21f899885d54ef2011 Mon Sep 17 00:00:00 2001 From: BratSinot Date: Tue, 28 Dec 2021 12:30:11 +0200 Subject: [PATCH 3/5] Fix typo. --- src/types/external/json_object/hashbrown_hashmap.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/external/json_object/hashbrown_hashmap.rs b/src/types/external/json_object/hashbrown_hashmap.rs index c805c823..8fedd540 100644 --- a/src/types/external/json_object/hashbrown_hashmap.rs +++ b/src/types/external/json_object/hashbrown_hashmap.rs @@ -12,7 +12,7 @@ use indexmap::IndexMap; use serde::de::DeserializeOwned; use serde::Serialize; -use crate::registry::{MetaType, Registry}; +use crate::registry::{Registry}; use crate::{ ContextSelectionSet, InputType, InputValueError, InputValueResult, Name, OutputType, ServerResult, Value, From f5593a27e1c579eff0b429cce4b535b19d56fcba Mon Sep 17 00:00:00 2001 From: BratSinot Date: Tue, 28 Dec 2021 13:05:32 +0200 Subject: [PATCH 4/5] Update CHANGELOG. --- CHANGELOG.md | 4 ++++ Cargo.toml | 2 +- src/lib.rs | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) 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 9c2b54e3..2ee53d6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,6 @@ 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" @@ -53,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 //! From c5d50e789f9ab6a179dc45a630a93f1998762d84 Mon Sep 17 00:00:00 2001 From: BratSinot Date: Tue, 28 Dec 2021 13:06:57 +0200 Subject: [PATCH 5/5] rustfmt --- .../external/json_object/hashbrown_hashmap.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/types/external/json_object/hashbrown_hashmap.rs b/src/types/external/json_object/hashbrown_hashmap.rs index 8fedd540..cf7ce7e0 100644 --- a/src/types/external/json_object/hashbrown_hashmap.rs +++ b/src/types/external/json_object/hashbrown_hashmap.rs @@ -7,22 +7,22 @@ use async_graphql_parser::types::Field; use async_graphql_parser::Positioned; use async_graphql_value::{from_value, to_value}; use hashbrown::HashMap; -use std::collections::HashMap as StdHashMap; use indexmap::IndexMap; use serde::de::DeserializeOwned; use serde::Serialize; +use std::collections::HashMap as StdHashMap; -use crate::registry::{Registry}; +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, +where + K: ToString + FromStr + Eq + Hash + Send + Sync, + K::Err: Display, + V: Serialize + DeserializeOwned + Send + Sync, { type RawValueType = Self; @@ -71,9 +71,9 @@ impl InputType for HashMap #[async_trait::async_trait] impl OutputType for HashMap - where - K: ToString + Eq + Hash + Send + Sync, - V: Serialize + Send + Sync, +where + K: ToString + Eq + Hash + Send + Sync, + V: Serialize + Send + Sync, { fn type_name() -> Cow<'static, str> { as OutputType>::type_name()