async-graphql/src/types/list.rs

98 lines
2.6 KiB
Rust
Raw Normal View History

2020-03-24 10:54:22 +00:00
use crate::{
registry, ContextSelectionSet, InputValueType, JsonWriter, OutputValueType, Result, Type, Value,
};
2020-03-02 00:24:49 +00:00
use std::borrow::Cow;
2020-03-19 09:20:12 +00:00
impl<T: Type> Type for Vec<T> {
2020-03-02 00:24:49 +00:00
fn type_name() -> Cow<'static, str> {
2020-03-05 06:23:55 +00:00
Cow::Owned(format!("[{}]", T::qualified_type_name()))
}
fn qualified_type_name() -> String {
2020-03-05 06:23:55 +00:00
format!("[{}]!", T::qualified_type_name())
2020-03-02 00:24:49 +00:00
}
2020-03-03 03:48:00 +00:00
2020-03-03 11:15:18 +00:00
fn create_type_info(registry: &mut registry::Registry) -> String {
2020-03-05 13:34:31 +00:00
T::create_type_info(registry);
Self::qualified_type_name()
2020-03-03 03:48:00 +00:00
}
2020-03-02 00:24:49 +00:00
}
2020-03-19 09:20:12 +00:00
impl<T: InputValueType> InputValueType for Vec<T> {
2020-03-04 02:38:07 +00:00
fn parse(value: &Value) -> Option<Self> {
2020-03-02 00:24:49 +00:00
match value {
Value::List(values) => {
let mut result = Vec::new();
for value in values {
2020-03-19 09:20:12 +00:00
result.push(InputValueType::parse(value)?);
2020-03-02 00:24:49 +00:00
}
2020-03-03 11:15:18 +00:00
Some(result)
2020-03-02 00:24:49 +00:00
}
2020-03-03 11:15:18 +00:00
_ => None,
2020-03-02 00:24:49 +00:00
}
}
}
2020-03-21 01:32:13 +00:00
#[allow(clippy::ptr_arg)]
2020-03-02 00:24:49 +00:00
#[async_trait::async_trait]
2020-03-19 09:20:12 +00:00
impl<T: OutputValueType + Send + Sync> OutputValueType for Vec<T> {
2020-03-24 10:54:22 +00:00
async fn resolve(
value: &Self,
ctx: &ContextSelectionSet<'_>,
w: &mut JsonWriter,
) -> Result<()> {
w.begin_array();
2020-03-06 15:58:43 +00:00
for item in value {
2020-03-24 10:54:22 +00:00
w.begin_array_value();
OutputValueType::resolve(item, &ctx, w).await?;
w.end_array_value();
2020-03-02 00:24:49 +00:00
}
2020-03-24 10:54:22 +00:00
w.end_array();
Ok(())
2020-03-02 00:24:49 +00:00
}
}
2020-03-19 09:20:12 +00:00
impl<T: Type> Type for &[T] {
2020-03-02 00:24:49 +00:00
fn type_name() -> Cow<'static, str> {
2020-03-03 11:15:18 +00:00
Cow::Owned(format!("[{}]", T::type_name()))
2020-03-02 00:24:49 +00:00
}
2020-03-03 03:48:00 +00:00
2020-03-03 11:15:18 +00:00
fn create_type_info(registry: &mut registry::Registry) -> String {
2020-03-03 03:48:00 +00:00
T::create_type_info(registry)
}
2020-03-02 00:24:49 +00:00
}
#[async_trait::async_trait]
2020-03-19 09:20:12 +00:00
impl<T: OutputValueType + Send + Sync> OutputValueType for &[T] {
2020-03-24 10:54:22 +00:00
async fn resolve(
value: &Self,
ctx: &ContextSelectionSet<'_>,
w: &mut JsonWriter,
) -> Result<()> {
w.begin_array();
2020-03-06 15:58:43 +00:00
for item in value.iter() {
2020-03-24 10:54:22 +00:00
w.begin_array_value();
OutputValueType::resolve(item, &ctx, w).await?;
w.end_array_value();
2020-03-05 13:34:31 +00:00
}
2020-03-24 10:54:22 +00:00
w.end_array();
Ok(())
2020-03-05 13:34:31 +00:00
}
}
2020-03-05 06:23:55 +00:00
#[cfg(test)]
mod tests {
2020-03-19 09:20:12 +00:00
use crate::Type;
2020-03-05 06:23:55 +00:00
#[test]
fn test_list_type() {
assert_eq!(Vec::<i32>::type_name(), "[Int!]");
assert_eq!(Vec::<Option<i32>>::type_name(), "[Int]");
assert_eq!(Option::<Vec::<Option<i32>>>::type_name(), "[Int]");
assert_eq!(Vec::<i32>::qualified_type_name(), "[Int!]!");
assert_eq!(Vec::<Option<i32>>::qualified_type_name(), "[Int]!");
assert_eq!(Option::<Vec::<Option<i32>>>::qualified_type_name(), "[Int]");
}
}