2020-09-28 09:44:00 +00:00
|
|
|
use darling::ast::Data;
|
2020-03-27 02:20:20 +00:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use quote::quote;
|
2020-07-02 02:24:54 +00:00
|
|
|
use syn::ext::IdentExt;
|
2020-09-28 09:44:00 +00:00
|
|
|
use syn::Error;
|
2020-03-27 02:20:20 +00:00
|
|
|
|
2020-10-16 02:45:27 +00:00
|
|
|
use crate::args::{self, RenameRuleExt, RenameTarget};
|
|
|
|
use crate::utils::{generate_guards, get_crate_name, get_rustdoc, GeneratorResult};
|
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
pub fn generate(object_args: &args::SimpleObject) -> GeneratorResult<TokenStream> {
|
2020-03-27 02:20:20 +00:00
|
|
|
let crate_name = get_crate_name(object_args.internal);
|
2020-09-28 09:44:00 +00:00
|
|
|
let ident = &object_args.ident;
|
|
|
|
let generics = &object_args.generics;
|
2020-04-28 10:05:06 +00:00
|
|
|
let where_clause = &generics.where_clause;
|
2020-04-09 14:03:09 +00:00
|
|
|
let extends = object_args.extends;
|
2020-03-27 02:20:20 +00:00
|
|
|
let gql_typename = object_args
|
|
|
|
.name
|
|
|
|
.clone()
|
2020-10-14 09:08:57 +00:00
|
|
|
.unwrap_or_else(|| RenameTarget::Type.rename(ident.to_string()));
|
2020-03-27 02:20:20 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
let desc = get_rustdoc(&object_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-03-27 02:20:20 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
let s = match &object_args.data {
|
2020-05-10 04:41:05 +00:00
|
|
|
Data::Struct(e) => e,
|
2020-09-28 09:44:00 +00:00
|
|
|
_ => {
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
&ident,
|
|
|
|
"SimpleObject can only be applied to an struct.",
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
2020-05-10 04:41:05 +00:00
|
|
|
};
|
2020-04-21 07:27:43 +00:00
|
|
|
let mut getters = Vec::new();
|
2020-03-27 02:20:20 +00:00
|
|
|
let mut resolvers = Vec::new();
|
|
|
|
let mut schema_fields = Vec::new();
|
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
for field in &s.fields {
|
|
|
|
if field.skip {
|
|
|
|
continue;
|
2020-03-27 02:20:20 +00:00
|
|
|
}
|
2020-09-28 09:44:00 +00:00
|
|
|
let ident = match &field.ident {
|
|
|
|
Some(ident) => ident,
|
|
|
|
None => return Err(Error::new_spanned(&ident, "All fields must be named.").into()),
|
|
|
|
};
|
|
|
|
|
2020-10-14 09:08:57 +00:00
|
|
|
let field_name = field.name.clone().unwrap_or_else(|| {
|
|
|
|
object_args
|
|
|
|
.rename_fields
|
|
|
|
.rename(ident.unraw().to_string(), RenameTarget::Field)
|
|
|
|
});
|
2020-09-28 09:44:00 +00:00
|
|
|
let field_desc = get_rustdoc(&field.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-09-28 09:44:00 +00:00
|
|
|
let field_deprecation = field
|
|
|
|
.deprecation
|
|
|
|
.as_ref()
|
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-09-28 09:44:00 +00:00
|
|
|
let external = field.external;
|
|
|
|
let requires = match &field.requires {
|
2020-10-16 10:37:59 +00:00
|
|
|
Some(requires) => quote! { ::std::option::Option::Some(#requires) },
|
|
|
|
None => quote! { ::std::option::Option::None },
|
2020-09-28 09:44:00 +00:00
|
|
|
};
|
|
|
|
let provides = match &field.provides {
|
2020-10-16 10:37:59 +00:00
|
|
|
Some(provides) => quote! { ::std::option::Option::Some(#provides) },
|
|
|
|
None => quote! { ::std::option::Option::None },
|
2020-09-28 09:44:00 +00:00
|
|
|
};
|
|
|
|
let vis = &field.vis;
|
|
|
|
let ty = &field.ty;
|
2020-03-27 02:20:20 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
let cache_control = {
|
|
|
|
let public = field.cache_control.is_public();
|
|
|
|
let max_age = field.cache_control.max_age;
|
|
|
|
quote! {
|
|
|
|
#crate_name::CacheControl {
|
|
|
|
public: #public,
|
|
|
|
max_age: #max_age,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-03-27 02:20:20 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
schema_fields.push(quote! {
|
|
|
|
fields.insert(#field_name.to_string(), #crate_name::registry::MetaField {
|
|
|
|
name: #field_name.to_string(),
|
|
|
|
description: #field_desc,
|
2020-10-16 10:37:59 +00:00
|
|
|
args: ::std::default::Default::default(),
|
2020-09-28 09:44:00 +00:00
|
|
|
ty: <#ty as #crate_name::Type>::create_type_info(registry),
|
|
|
|
deprecation: #field_deprecation,
|
|
|
|
cache_control: #cache_control,
|
|
|
|
external: #external,
|
|
|
|
provides: #provides,
|
|
|
|
requires: #requires,
|
|
|
|
});
|
|
|
|
});
|
2020-03-27 02:20:20 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
let guard = match &field.guard {
|
|
|
|
Some(meta) => generate_guards(&crate_name, &meta)?,
|
|
|
|
None => None,
|
|
|
|
};
|
2020-09-30 18:40:17 +00:00
|
|
|
let guard = guard.map(|guard| quote! { #guard.check(ctx).await.map_err(|err| err.into_server_error().at(ctx.item.pos))?; });
|
2020-04-21 07:40:19 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
getters.push(if !field.owned {
|
|
|
|
quote! {
|
|
|
|
#[inline]
|
|
|
|
#[allow(missing_docs)]
|
2020-09-30 18:40:17 +00:00
|
|
|
#vis async fn #ident(&self, ctx: &#crate_name::Context<'_>) -> #crate_name::Result<&#ty> {
|
2020-10-16 10:37:59 +00:00
|
|
|
::std::result::Result::Ok(&self.#ident)
|
2020-09-28 09:44:00 +00:00
|
|
|
}
|
2020-04-27 06:14:29 +00:00
|
|
|
}
|
2020-09-28 09:44:00 +00:00
|
|
|
} else {
|
|
|
|
quote! {
|
|
|
|
#[inline]
|
|
|
|
#[allow(missing_docs)]
|
2020-09-30 18:40:17 +00:00
|
|
|
#vis async fn #ident(&self, ctx: &#crate_name::Context<'_>) -> #crate_name::Result<#ty> {
|
2020-10-16 10:37:59 +00:00
|
|
|
::std::result::Result::Ok(self.#ident.clone())
|
2020-09-28 09:44:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
resolvers.push(quote! {
|
|
|
|
if ctx.item.node.name.node == #field_name {
|
|
|
|
#guard
|
2020-09-30 18:40:17 +00:00
|
|
|
let res = self.#ident(ctx).await.map_err(|err| err.into_server_error().at(ctx.item.pos))?;
|
2020-09-28 09:44:00 +00:00
|
|
|
let ctx_obj = ctx.with_selection_set(&ctx.item.node.selection_set);
|
2020-09-30 18:40:17 +00:00
|
|
|
return #crate_name::OutputValueType::resolve(&res, &ctx_obj, ctx.item).await.map(::std::option::Option::Some);
|
2020-09-28 09:44:00 +00:00
|
|
|
}
|
|
|
|
});
|
2020-03-27 02:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let cache_control = {
|
2020-09-28 09:44:00 +00:00
|
|
|
let public = object_args.cache_control.is_public();
|
2020-03-27 02:20:20 +00:00
|
|
|
let max_age = object_args.cache_control.max_age;
|
|
|
|
quote! {
|
|
|
|
#crate_name::CacheControl {
|
|
|
|
public: #public,
|
|
|
|
max_age: #max_age,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let expanded = quote! {
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2020-10-14 22:27:31 +00:00
|
|
|
impl #generics #ident #generics #where_clause {
|
2020-04-21 07:27:43 +00:00
|
|
|
#(#getters)*
|
|
|
|
}
|
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2020-04-28 10:05:06 +00:00
|
|
|
impl #generics #crate_name::Type for #ident #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-27 02:20:20 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 10:37:59 +00:00
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String {
|
2020-05-15 02:08:37 +00:00
|
|
|
registry.create_type::<Self, _>(|registry| #crate_name::registry::MetaType::Object {
|
2020-03-27 02:20:20 +00:00
|
|
|
name: #gql_typename.to_string(),
|
|
|
|
description: #desc,
|
|
|
|
fields: {
|
2020-05-16 02:05:48 +00:00
|
|
|
let mut fields = #crate_name::indexmap::IndexMap::new();
|
2020-03-27 02:20:20 +00:00
|
|
|
#(#schema_fields)*
|
|
|
|
fields
|
|
|
|
},
|
|
|
|
cache_control: #cache_control,
|
2020-04-09 14:03:09 +00:00
|
|
|
extends: #extends,
|
2020-10-16 10:37:59 +00:00
|
|
|
keys: ::std::option::Option::None,
|
2020-03-27 02:20:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2020-03-27 02:20:20 +00:00
|
|
|
#[#crate_name::async_trait::async_trait]
|
2020-10-01 01:07:19 +00:00
|
|
|
|
2020-09-29 23:45:48 +00:00
|
|
|
impl #generics #crate_name::resolver_utils::ContainerType for #ident #generics #where_clause {
|
2020-10-10 02:32:43 +00:00
|
|
|
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>) -> #crate_name::ServerResult<::std::option::Option<#crate_name::Value>> {
|
2020-03-27 02:20:20 +00:00
|
|
|
#(#resolvers)*
|
2020-10-16 10:37:59 +00:00
|
|
|
::std::result::Result::Ok(::std::option::Option::None)
|
2020-03-27 02:20:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2020-03-27 02:20:20 +00:00
|
|
|
#[#crate_name::async_trait::async_trait]
|
2020-04-28 10:05:06 +00:00
|
|
|
impl #generics #crate_name::OutputValueType for #ident #generics #where_clause {
|
2020-10-01 01:07:19 +00:00
|
|
|
|
2020-10-10 02:32:43 +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-27 02:20:20 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-27 07:32:55 +00:00
|
|
|
|
2020-09-29 23:45:48 +00:00
|
|
|
impl #generics #crate_name::ObjectType for #ident #generics #where_clause {}
|
2020-03-27 02:20:20 +00:00
|
|
|
};
|
|
|
|
Ok(expanded.into())
|
|
|
|
}
|