2020-03-01 10:54:34 +00:00
|
|
|
use crate::args;
|
2020-05-26 10:34:43 +00:00
|
|
|
use crate::utils::{check_reserved_name, get_crate_name, get_rustdoc};
|
2020-03-09 12:00:57 +00:00
|
|
|
use inflector::Inflector;
|
2020-03-01 10:54:34 +00:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use quote::quote;
|
2020-03-01 13:35:39 +00:00
|
|
|
use syn::{Data, DeriveInput, Error, Result};
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-03 03:48:00 +00:00
|
|
|
pub fn generate(object_args: &args::InputObject, input: &DeriveInput) -> Result<TokenStream> {
|
2020-03-02 00:24:49 +00:00
|
|
|
let crate_name = get_crate_name(object_args.internal);
|
2020-03-01 10:54:34 +00:00
|
|
|
let ident = &input.ident;
|
2020-03-03 03:48:00 +00:00
|
|
|
let attrs = &input.attrs;
|
|
|
|
let vis = &input.vis;
|
2020-03-01 10:54:34 +00:00
|
|
|
let s = match &input.data {
|
|
|
|
Data::Struct(s) => s,
|
|
|
|
_ => return Err(Error::new_spanned(input, "It should be a struct.")),
|
|
|
|
};
|
|
|
|
|
2020-03-03 03:48:00 +00:00
|
|
|
let mut struct_fields = Vec::new();
|
|
|
|
for field in &s.fields {
|
|
|
|
let vis = &field.vis;
|
|
|
|
let ty = &field.ty;
|
|
|
|
let ident = &field.ident;
|
2020-04-08 04:02:48 +00:00
|
|
|
let attrs = field
|
|
|
|
.attrs
|
|
|
|
.iter()
|
|
|
|
.filter(|attr| !attr.path.is_ident("field"))
|
|
|
|
.collect::<Vec<_>>();
|
2020-03-03 03:48:00 +00:00
|
|
|
struct_fields.push(quote! {
|
2020-04-08 04:02:48 +00:00
|
|
|
#(#attrs)*
|
2020-03-03 03:48:00 +00:00
|
|
|
#vis #ident: #ty
|
|
|
|
});
|
|
|
|
}
|
|
|
|
let new_struct = quote! {
|
|
|
|
#(#attrs)*
|
|
|
|
#vis struct #ident {
|
|
|
|
#(#struct_fields),*
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-01 10:54:34 +00:00
|
|
|
let gql_typename = object_args
|
|
|
|
.name
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| ident.to_string());
|
2020-03-19 09:20:12 +00:00
|
|
|
check_reserved_name(&gql_typename, object_args.internal)?;
|
|
|
|
|
2020-03-03 11:15:18 +00:00
|
|
|
let desc = object_args
|
|
|
|
.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-03 11:15:18 +00:00
|
|
|
.unwrap_or_else(|| quote! {None});
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-01 13:35:39 +00:00
|
|
|
let mut get_fields = Vec::new();
|
2020-05-26 10:34:43 +00:00
|
|
|
let mut put_fields = Vec::new();
|
2020-03-01 13:35:39 +00:00
|
|
|
let mut fields = Vec::new();
|
2020-03-03 03:48:00 +00:00
|
|
|
let mut schema_fields = Vec::new();
|
2020-03-01 16:52:05 +00:00
|
|
|
|
2020-03-01 10:54:34 +00:00
|
|
|
for field in &s.fields {
|
2020-03-21 07:07:11 +00:00
|
|
|
let field_args = args::InputField::parse(&crate_name, &field.attrs)?;
|
2020-03-01 10:54:34 +00:00
|
|
|
let ident = field.ident.as_ref().unwrap();
|
2020-03-01 13:35:39 +00:00
|
|
|
let ty = &field.ty;
|
2020-03-22 01:34:32 +00:00
|
|
|
let validator = &field_args.validator;
|
2020-03-09 12:00:57 +00:00
|
|
|
let name = field_args
|
|
|
|
.name
|
|
|
|
.unwrap_or_else(|| ident.to_string().to_camel_case());
|
2020-03-03 03:48:00 +00:00
|
|
|
let desc = field_args
|
|
|
|
.desc
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! {Some(#s)})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
2020-05-26 10:34:43 +00:00
|
|
|
let schema_default = field_args
|
2020-03-03 03:48:00 +00:00
|
|
|
.default
|
|
|
|
.as_ref()
|
2020-05-26 10:34:43 +00:00
|
|
|
.map(|value| {
|
2020-05-27 01:27:59 +00:00
|
|
|
quote! {Some( <#ty as #crate_name::InputValueType>::to_value(&#value).to_string() )}
|
2020-03-03 03:48:00 +00:00
|
|
|
})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
2020-03-04 02:38:07 +00:00
|
|
|
|
|
|
|
if let Some(default) = &field_args.default {
|
|
|
|
get_fields.push(quote! {
|
2020-05-26 10:34:43 +00:00
|
|
|
let #ident: #ty = {
|
2020-03-04 02:38:07 +00:00
|
|
|
match obj.get(#name) {
|
2020-05-11 13:47:24 +00:00
|
|
|
Some(value) => #crate_name::InputValueType::parse(value.clone())?,
|
2020-05-26 10:34:43 +00:00
|
|
|
None => #default,
|
2020-03-04 02:38:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
get_fields.push(quote! {
|
2020-05-11 13:47:24 +00:00
|
|
|
let #ident:#ty = #crate_name::InputValueType::parse(obj.get(#name).cloned().unwrap_or(#crate_name::Value::Null))?;
|
2020-03-04 02:38:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-26 10:34:43 +00:00
|
|
|
put_fields.push(quote! {
|
|
|
|
map.insert(#name.to_string(), #crate_name::InputValueType::to_value(&self.#ident));
|
|
|
|
});
|
|
|
|
|
2020-03-01 13:35:39 +00:00
|
|
|
fields.push(ident);
|
2020-03-03 03:48:00 +00:00
|
|
|
schema_fields.push(quote! {
|
2020-05-15 02:08:37 +00:00
|
|
|
fields.insert(#name.to_string(), #crate_name::registry::MetaInputValue {
|
2020-03-03 03:48:00 +00:00
|
|
|
name: #name,
|
|
|
|
description: #desc,
|
2020-03-19 09:20:12 +00:00
|
|
|
ty: <#ty as #crate_name::Type>::create_type_info(registry),
|
2020-05-26 10:34:43 +00:00
|
|
|
default_value: #schema_default,
|
2020-03-22 01:34:32 +00:00
|
|
|
validator: #validator,
|
2020-04-05 08:00:26 +00:00
|
|
|
});
|
2020-03-03 03:48:00 +00:00
|
|
|
})
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let expanded = quote! {
|
2020-03-03 03:48:00 +00:00
|
|
|
#new_struct
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
impl #crate_name::Type for #ident {
|
2020-03-02 00:24:49 +00:00
|
|
|
fn type_name() -> std::borrow::Cow<'static, str> {
|
|
|
|
std::borrow::Cow::Borrowed(#gql_typename)
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
2020-03-03 03:48:00 +00:00
|
|
|
|
2020-03-03 11:15:18 +00:00
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> String {
|
2020-05-15 02:08:37 +00:00
|
|
|
registry.create_type::<Self, _>(|registry| #crate_name::registry::MetaType::InputObject {
|
2020-03-19 09:20:12 +00:00
|
|
|
name: #gql_typename.to_string(),
|
2020-03-03 11:15:18 +00:00
|
|
|
description: #desc,
|
2020-04-05 08:00:26 +00:00
|
|
|
input_fields: {
|
2020-05-16 02:05:48 +00:00
|
|
|
let mut fields = #crate_name::indexmap::IndexMap::new();
|
2020-04-05 08:00:26 +00:00
|
|
|
#(#schema_fields)*
|
|
|
|
fields
|
|
|
|
}
|
2020-03-03 03:48:00 +00:00
|
|
|
})
|
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
impl #crate_name::InputValueType for #ident {
|
2020-05-11 13:47:24 +00:00
|
|
|
fn parse(value: #crate_name::Value) -> #crate_name::InputValueResult<Self> {
|
2020-03-19 09:20:12 +00:00
|
|
|
use #crate_name::Type;
|
2020-03-02 11:25:21 +00:00
|
|
|
|
2020-05-11 13:47:24 +00:00
|
|
|
if let #crate_name::Value::Object(obj) = &value {
|
2020-03-01 13:35:39 +00:00
|
|
|
#(#get_fields)*
|
2020-05-10 10:27:46 +00:00
|
|
|
Ok(Self { #(#fields),* })
|
2020-03-01 13:35:39 +00:00
|
|
|
} else {
|
2020-05-11 13:47:24 +00:00
|
|
|
Err(#crate_name::InputValueError::ExpectedType(value))
|
2020-03-01 13:35:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-26 10:34:43 +00:00
|
|
|
|
|
|
|
fn to_value(&self) -> #crate_name::Value {
|
|
|
|
let mut map = std::collections::BTreeMap::new();
|
|
|
|
#(#put_fields)*
|
|
|
|
#crate_name::Value::Object(map)
|
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
2020-03-01 13:35:39 +00:00
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
impl #crate_name::InputObjectType for #ident {}
|
2020-03-01 10:54:34 +00:00
|
|
|
};
|
|
|
|
Ok(expanded.into())
|
|
|
|
}
|