2022-04-19 04:25:11 +00:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
use darling::ast::{Data, Style};
|
2020-03-08 01:21:29 +00:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use quote::quote;
|
2022-04-19 04:25:11 +00:00
|
|
|
use syn::{visit_mut::VisitMut, Error, Type};
|
2020-03-08 01:21:29 +00:00
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
use crate::{
|
|
|
|
args::{self, RenameTarget},
|
|
|
|
utils::{get_crate_name, get_rustdoc, visible_fn, GeneratorResult, RemoveLifetime},
|
|
|
|
};
|
2020-10-16 02:45:27 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
pub fn generate(union_args: &args::Union) -> GeneratorResult<TokenStream> {
|
2020-05-10 04:41:05 +00:00
|
|
|
let crate_name = get_crate_name(union_args.internal);
|
2020-09-28 09:44:00 +00:00
|
|
|
let ident = &union_args.ident;
|
2021-01-14 03:27:15 +00:00
|
|
|
let (impl_generics, ty_generics, where_clause) = union_args.generics.split_for_impl();
|
2020-09-28 09:44:00 +00:00
|
|
|
let s = match &union_args.data {
|
2020-05-11 04:27:30 +00:00
|
|
|
Data::Enum(s) => s,
|
|
|
|
_ => {
|
2020-09-28 09:44:00 +00:00
|
|
|
return Err(Error::new_spanned(&ident, "Union can only be applied to an enum.").into())
|
2020-05-11 04:27:30 +00:00
|
|
|
}
|
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-10-14 09:08:57 +00:00
|
|
|
let gql_typename = union_args
|
|
|
|
.name
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| RenameTarget::Type.rename(ident.to_string()));
|
2020-03-19 09:20:12 +00:00
|
|
|
|
2022-08-18 09:40:04 +00:00
|
|
|
let inaccessible = union_args.inaccessible;
|
2022-08-22 09:44:02 +00:00
|
|
|
let tags = &union_args.tags;
|
2020-09-28 09:44:00 +00:00
|
|
|
let desc = get_rustdoc(&union_args.attrs)?
|
2020-10-16 10:37:59 +00:00
|
|
|
.map(|s| quote! { ::std::option::Option::Some(#s) })
|
|
|
|
.unwrap_or_else(|| quote! {::std::option::Option::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-09-28 09:44:00 +00:00
|
|
|
for variant in s {
|
2020-05-11 04:27:30 +00:00
|
|
|
let enum_name = &variant.ident;
|
2020-09-28 09:44:00 +00:00
|
|
|
let ty = match variant.fields.style {
|
|
|
|
Style::Tuple if variant.fields.fields.len() == 1 => &variant.fields.fields[0],
|
|
|
|
Style::Tuple => {
|
2020-05-11 04:27:30 +00:00
|
|
|
return Err(Error::new_spanned(
|
2020-09-28 09:44:00 +00:00
|
|
|
enum_name,
|
2020-05-11 04:27:30 +00:00
|
|
|
"Only single value variants are supported",
|
2020-09-28 09:44:00 +00:00
|
|
|
)
|
|
|
|
.into())
|
2020-05-11 04:27:30 +00:00
|
|
|
}
|
2020-09-28 09:44:00 +00:00
|
|
|
Style::Unit => {
|
|
|
|
return Err(
|
|
|
|
Error::new_spanned(enum_name, "Empty variants are not supported").into(),
|
|
|
|
)
|
2020-05-11 04:27:30 +00:00
|
|
|
}
|
2020-09-28 09:44:00 +00:00
|
|
|
Style::Struct => {
|
2020-05-11 04:27:30 +00:00
|
|
|
return Err(Error::new_spanned(
|
2020-09-28 09:44:00 +00:00
|
|
|
enum_name,
|
2020-05-11 04:27:30 +00:00
|
|
|
"Variants with named fields are not supported",
|
2020-09-28 09:44:00 +00:00
|
|
|
)
|
|
|
|
.into())
|
2020-05-11 04:27:30 +00:00
|
|
|
}
|
|
|
|
};
|
2020-09-28 03:13:46 +00:00
|
|
|
|
2022-04-06 13:50:07 +00:00
|
|
|
let mut ty = ty;
|
|
|
|
while let Type::Group(group) = ty {
|
|
|
|
ty = &*group.elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
if matches!(ty, Type::Path(_) | Type::Macro(_)) {
|
2020-05-11 04:27:30 +00:00
|
|
|
// This validates that the field type wasn't already used
|
2022-04-06 13:50:07 +00:00
|
|
|
if !enum_items.insert(ty) {
|
2022-05-14 08:43:28 +00:00
|
|
|
return Err(Error::new_spanned(
|
|
|
|
&ty,
|
|
|
|
"This type is already used in another variant",
|
|
|
|
)
|
|
|
|
.into());
|
2020-05-11 04:27:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 01:21:29 +00:00
|
|
|
enum_names.push(enum_name);
|
2020-09-27 07:32:55 +00:00
|
|
|
|
2022-04-06 13:50:07 +00:00
|
|
|
let mut assert_ty = ty.clone();
|
|
|
|
RemoveLifetime.visit_type_mut(&mut assert_ty);
|
2020-10-14 22:27:31 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
if !variant.flatten {
|
2020-09-28 03:13:46 +00:00
|
|
|
type_into_impls.push(quote! {
|
2020-10-14 22:27:31 +00:00
|
|
|
#crate_name::static_assertions::assert_impl_one!(#assert_ty: #crate_name::ObjectType);
|
2020-09-28 03:13:46 +00:00
|
|
|
|
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2022-04-06 13:50:07 +00:00
|
|
|
impl #impl_generics ::std::convert::From<#ty> for #ident #ty_generics #where_clause {
|
|
|
|
fn from(obj: #ty) -> Self {
|
2020-09-28 03:13:46 +00:00
|
|
|
#ident::#enum_name(obj)
|
|
|
|
}
|
2020-03-08 01:21:29 +00:00
|
|
|
}
|
2020-09-28 03:13:46 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
type_into_impls.push(quote! {
|
2020-10-14 22:27:31 +00:00
|
|
|
#crate_name::static_assertions::assert_impl_one!(#assert_ty: #crate_name::UnionType);
|
2020-09-28 03:13:46 +00:00
|
|
|
|
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2022-04-06 13:50:07 +00:00
|
|
|
impl #impl_generics ::std::convert::From<#ty> for #ident #ty_generics #where_clause {
|
|
|
|
fn from(obj: #ty) -> Self {
|
2020-09-28 03:13:46 +00:00
|
|
|
#ident::#enum_name(obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
if !variant.flatten {
|
2020-10-20 03:49:31 +00:00
|
|
|
registry_types.push(quote! {
|
2022-04-06 13:50:07 +00:00
|
|
|
<#ty as #crate_name::OutputType>::create_type_info(registry);
|
2020-10-20 03:49:31 +00:00
|
|
|
});
|
2020-09-28 03:13:46 +00:00
|
|
|
possible_types.push(quote! {
|
2022-04-06 13:50:07 +00:00
|
|
|
possible_types.insert(<#ty as #crate_name::OutputType>::type_name().into_owned());
|
2020-09-28 03:13:46 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
possible_types.push(quote! {
|
2020-10-20 03:49:31 +00:00
|
|
|
if let #crate_name::registry::MetaType::Union { possible_types: possible_types2, .. } =
|
2022-04-06 13:50:07 +00:00
|
|
|
registry.create_fake_output_type::<#ty>() {
|
2020-10-20 03:49:31 +00:00
|
|
|
possible_types.extend(possible_types2);
|
2020-09-28 03:13:46 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
if !variant.flatten {
|
2020-09-28 03:13:46 +00:00
|
|
|
get_introspection_typename.push(quote! {
|
2022-04-06 13:50:07 +00:00
|
|
|
#ident::#enum_name(obj) => <#ty as #crate_name::OutputType>::type_name()
|
2020-09-28 03:13:46 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
get_introspection_typename.push(quote! {
|
2022-04-06 13:50:07 +00:00
|
|
|
#ident::#enum_name(obj) => <#ty as #crate_name::OutputType>::introspection_type_name(obj)
|
2020-09-28 03:13:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-09-28 09:44:00 +00:00
|
|
|
return Err(Error::new_spanned(ty, "Invalid type").into());
|
2020-03-08 01:21:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 03:49:31 +00:00
|
|
|
if possible_types.is_empty() {
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
&ident,
|
|
|
|
"A GraphQL Union type must include one or more unique member types.",
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
|
2020-12-11 08:03:28 +00:00
|
|
|
let visible = visible_fn(&union_args.visible);
|
2020-03-08 01:21:29 +00:00
|
|
|
let expanded = quote! {
|
|
|
|
#(#type_into_impls)*
|
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2021-11-09 10:52:10 +00:00
|
|
|
#[#crate_name::async_trait::async_trait]
|
|
|
|
|
|
|
|
impl #impl_generics #crate_name::resolver_utils::ContainerType for #ident #ty_generics #where_clause {
|
|
|
|
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>) -> #crate_name::ServerResult<::std::option::Option<#crate_name::Value>> {
|
|
|
|
::std::result::Result::Ok(::std::option::Option::None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collect_all_fields<'__life>(&'__life self, ctx: &#crate_name::ContextSelectionSet<'__life>, fields: &mut #crate_name::resolver_utils::Fields<'__life>) -> #crate_name::ServerResult<()> {
|
|
|
|
match self {
|
|
|
|
#(#collect_all_fields),*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
|
|
|
#[#crate_name::async_trait::async_trait]
|
|
|
|
impl #impl_generics #crate_name::OutputType for #ident #ty_generics #where_clause {
|
2020-10-16 10:37:59 +00:00
|
|
|
fn type_name() -> ::std::borrow::Cow<'static, ::std::primitive::str> {
|
2020-06-24 06:04:48 +00:00
|
|
|
::std::borrow::Cow::Borrowed(#gql_typename)
|
2020-03-08 01:21:29 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 10:37:59 +00:00
|
|
|
fn introspection_type_name(&self) -> ::std::borrow::Cow<'static, ::std::primitive::str> {
|
2020-04-02 12:37:01 +00:00
|
|
|
match self {
|
|
|
|
#(#get_introspection_typename),*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 10:37:59 +00:00
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String {
|
2022-03-30 12:54:49 +00:00
|
|
|
registry.create_output_type::<Self, _>(#crate_name::registry::MetaTypeId::Union, |registry| {
|
2020-03-08 01:21:29 +00:00
|
|
|
#(#registry_types)*
|
|
|
|
|
2020-05-15 02:08:37 +00:00
|
|
|
#crate_name::registry::MetaType::Union {
|
2020-10-16 19:21:46 +00:00
|
|
|
name: ::std::borrow::ToOwned::to_owned(#gql_typename),
|
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-12-11 08:03:28 +00:00
|
|
|
},
|
|
|
|
visible: #visible,
|
2022-08-18 09:40:04 +00:00
|
|
|
inaccessible: #inaccessible,
|
2022-08-22 09:44:02 +00:00
|
|
|
tags: &[ #(#tags),* ],
|
2021-11-08 01:33:20 +00:00
|
|
|
rust_typename: ::std::any::type_name::<Self>(),
|
2020-03-08 01:21:29 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-03-19 09:20:12 +00:00
|
|
|
|
2021-06-07 12:51:20 +00:00
|
|
|
async fn resolve(&self, ctx: &#crate_name::ContextSelectionSet<'_>, _field: &#crate_name::Positioned<#crate_name::parser::types::Field>) -> #crate_name::ServerResult<#crate_name::Value> {
|
2020-09-29 23:45:48 +00:00
|
|
|
#crate_name::resolver_utils::resolve_container(ctx, self).await
|
2020-03-19 09:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-27 07:32:55 +00:00
|
|
|
|
2021-01-14 03:27:15 +00:00
|
|
|
impl #impl_generics #crate_name::UnionType for #ident #ty_generics #where_clause {}
|
2020-03-08 01:21:29 +00:00
|
|
|
};
|
2022-01-11 01:35:09 +00:00
|
|
|
|
2020-03-08 01:21:29 +00:00
|
|
|
Ok(expanded.into())
|
|
|
|
}
|