2020-03-08 01:21:29 +00:00
|
|
|
use crate::args;
|
2020-06-08 02:30:40 +00:00
|
|
|
use crate::utils::{get_crate_name, get_rustdoc};
|
2020-03-08 01:21:29 +00:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use quote::quote;
|
2020-05-11 04:27:30 +00:00
|
|
|
use std::collections::HashSet;
|
2020-03-08 01:21:29 +00:00
|
|
|
use syn::{Data, DeriveInput, Error, Fields, Result, Type};
|
|
|
|
|
2020-05-10 04:41:05 +00:00
|
|
|
pub fn generate(union_args: &args::Interface, input: &DeriveInput) -> Result<TokenStream> {
|
|
|
|
let crate_name = get_crate_name(union_args.internal);
|
2020-03-08 01:21:29 +00:00
|
|
|
let ident = &input.ident;
|
|
|
|
let generics = &input.generics;
|
|
|
|
let s = match &input.data {
|
2020-05-11 04:27:30 +00:00
|
|
|
Data::Enum(s) => s,
|
|
|
|
_ => {
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
input,
|
|
|
|
"Unions can only be applied to an enum.",
|
|
|
|
))
|
|
|
|
}
|
2020-03-08 01:21:29 +00:00
|
|
|
};
|
|
|
|
let mut enum_names = Vec::new();
|
2020-05-11 04:27:30 +00:00
|
|
|
let mut enum_items = HashSet::new();
|
2020-03-08 01:21:29 +00:00
|
|
|
let mut type_into_impls = Vec::new();
|
2020-05-10 04:41:05 +00:00
|
|
|
let gql_typename = union_args.name.clone().unwrap_or_else(|| ident.to_string());
|
2020-03-19 09:20:12 +00:00
|
|
|
|
2020-05-10 04:41:05 +00:00
|
|
|
let desc = union_args
|
2020-03-08 01:21:29 +00:00
|
|
|
.desc
|
2020-05-10 04:41:05 +00:00
|
|
|
.clone()
|
|
|
|
.or_else(|| get_rustdoc(&input.attrs).ok().flatten())
|
|
|
|
.map(|s| quote! { Some(#s) })
|
2020-03-08 01:21:29 +00:00
|
|
|
.unwrap_or_else(|| quote! {None});
|
2020-05-10 04:41:05 +00:00
|
|
|
|
2020-03-08 01:21:29 +00:00
|
|
|
let mut registry_types = Vec::new();
|
|
|
|
let mut possible_types = Vec::new();
|
2020-04-02 12:37:01 +00:00
|
|
|
let mut get_introspection_typename = Vec::new();
|
2020-09-12 09:29:52 +00:00
|
|
|
let mut collect_all_fields = Vec::new();
|
2020-03-08 01:21:29 +00:00
|
|
|
|
2020-05-11 04:27:30 +00:00
|
|
|
for variant in s.variants.iter() {
|
|
|
|
let enum_name = &variant.ident;
|
|
|
|
let field = match &variant.fields {
|
|
|
|
Fields::Unnamed(fields) if fields.unnamed.len() == 1 => fields.unnamed.first().unwrap(),
|
|
|
|
Fields::Unnamed(_) => {
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
variant,
|
|
|
|
"Only single value variants are supported",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
Fields::Unit => {
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
variant,
|
|
|
|
"Empty variants are not supported",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
Fields::Named(_) => {
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
variant,
|
|
|
|
"Variants with named fields are not supported",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
2020-03-08 01:21:29 +00:00
|
|
|
if let Type::Path(p) = &field.ty {
|
2020-05-11 04:27:30 +00:00
|
|
|
// This validates that the field type wasn't already used
|
2020-05-11 13:47:24 +00:00
|
|
|
if !enum_items.insert(p) {
|
2020-05-11 04:27:30 +00:00
|
|
|
return Err(Error::new_spanned(
|
|
|
|
field,
|
|
|
|
"This type already used in another variant",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-03-08 01:21:29 +00:00
|
|
|
enum_names.push(enum_name);
|
|
|
|
type_into_impls.push(quote! {
|
2020-09-27 07:32:55 +00:00
|
|
|
#crate_name::static_assertions::assert_impl_one!(#p: #crate_name::type_mark::TypeMarkObject);
|
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2020-09-17 05:20:54 +00:00
|
|
|
impl #generics ::std::convert::From<#p> for #ident #generics {
|
2020-03-08 01:21:29 +00:00
|
|
|
fn from(obj: #p) -> Self {
|
|
|
|
#ident::#enum_name(obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
registry_types.push(quote! {
|
2020-03-19 09:20:12 +00:00
|
|
|
<#p as #crate_name::Type>::create_type_info(registry);
|
2020-03-08 01:21:29 +00:00
|
|
|
});
|
|
|
|
possible_types.push(quote! {
|
2020-03-19 09:20:12 +00:00
|
|
|
possible_types.insert(<#p as #crate_name::Type>::type_name().to_string());
|
2020-03-08 01:21:29 +00:00
|
|
|
});
|
2020-04-02 12:37:01 +00:00
|
|
|
get_introspection_typename.push(quote! {
|
|
|
|
#ident::#enum_name(obj) => <#p as #crate_name::Type>::type_name()
|
2020-09-12 09:29:52 +00:00
|
|
|
});
|
|
|
|
collect_all_fields.push(quote! {
|
|
|
|
#ident::#enum_name(obj) => obj.collect_all_fields(ctx, fields)
|
|
|
|
});
|
2020-03-08 01:21:29 +00:00
|
|
|
} else {
|
|
|
|
return Err(Error::new_spanned(field, "Invalid type"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let expanded = quote! {
|
|
|
|
#(#type_into_impls)*
|
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2020-03-19 09:20:12 +00:00
|
|
|
impl #generics #crate_name::Type for #ident #generics {
|
2020-06-24 06:04:48 +00:00
|
|
|
fn type_name() -> ::std::borrow::Cow<'static, str> {
|
|
|
|
::std::borrow::Cow::Borrowed(#gql_typename)
|
2020-03-08 01:21:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 06:04:48 +00:00
|
|
|
fn introspection_type_name(&self) -> ::std::borrow::Cow<'static, str> {
|
2020-04-02 12:37:01 +00:00
|
|
|
match self {
|
|
|
|
#(#get_introspection_typename),*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 01:21:29 +00:00
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> String {
|
|
|
|
registry.create_type::<Self, _>(|registry| {
|
|
|
|
#(#registry_types)*
|
|
|
|
|
2020-05-15 02:08:37 +00:00
|
|
|
#crate_name::registry::MetaType::Union {
|
2020-03-19 09:33:33 +00:00
|
|
|
name: #gql_typename.to_string(),
|
2020-03-08 01:21:29 +00:00
|
|
|
description: #desc,
|
2020-03-10 10:07:47 +00:00
|
|
|
possible_types: {
|
2020-05-16 02:05:48 +00:00
|
|
|
let mut possible_types = #crate_name::indexmap::IndexSet::new();
|
2020-03-10 10:07:47 +00:00
|
|
|
#(#possible_types)*
|
|
|
|
possible_types
|
|
|
|
}
|
2020-03-08 01:21:29 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2020-03-08 01:21:29 +00:00
|
|
|
#[#crate_name::async_trait::async_trait]
|
2020-09-12 09:29:52 +00:00
|
|
|
impl #generics #crate_name::resolver_utils::ObjectType for #ident #generics {
|
2020-04-26 07:12:18 +00:00
|
|
|
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
2020-04-02 02:21:04 +00:00
|
|
|
Err(#crate_name::QueryError::FieldNotFound {
|
2020-09-12 16:42:15 +00:00
|
|
|
field_name: ctx.item.node.name.to_string(),
|
2020-03-08 01:21:29 +00:00
|
|
|
object: #gql_typename.to_string(),
|
2020-09-12 16:42:15 +00:00
|
|
|
}.into_error(ctx.item.pos))
|
2020-03-08 01:21:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 09:29:52 +00:00
|
|
|
fn collect_all_fields<'a>(&'a self, ctx: &#crate_name::ContextSelectionSet<'a>, fields: &mut #crate_name::resolver_utils::Fields<'a>) -> #crate_name::Result<()> {
|
|
|
|
match self {
|
|
|
|
#(#collect_all_fields),*
|
|
|
|
}
|
2020-03-08 01:21:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-19 09:20:12 +00:00
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2020-03-19 09:20:12 +00:00
|
|
|
#[#crate_name::async_trait::async_trait]
|
|
|
|
impl #generics #crate_name::OutputValueType for #ident #generics {
|
2020-09-06 05:38:31 +00:00
|
|
|
async fn resolve(&self, ctx: &#crate_name::ContextSelectionSet<'_>, _field: &#crate_name::Positioned<#crate_name::parser::types::Field>) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
2020-09-12 09:29:52 +00:00
|
|
|
#crate_name::resolver_utils::resolve_object(ctx, self).await
|
2020-03-19 09:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-27 07:32:55 +00:00
|
|
|
|
|
|
|
impl #generics #crate_name::type_mark::TypeMarkEnum for #ident #generics {}
|
2020-03-08 01:21:29 +00:00
|
|
|
};
|
|
|
|
Ok(expanded.into())
|
|
|
|
}
|