async-graphql/async-graphql-derive/src/input_object.rs

77 lines
2.6 KiB
Rust
Raw Normal View History

2020-03-01 10:54:34 +00:00
use crate::args;
2020-03-02 00:24:49 +00:00
use crate::utils::get_crate_name;
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
pub fn generate(object_args: &args::Object, 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;
let s = match &input.data {
Data::Struct(s) => s,
_ => return Err(Error::new_spanned(input, "It should be a struct.")),
};
let gql_typename = object_args
.name
.clone()
.unwrap_or_else(|| ident.to_string());
2020-03-01 13:35:39 +00:00
let mut get_fields = Vec::new();
2020-03-01 16:52:05 +00:00
let mut get_json_fields = Vec::new();
2020-03-01 13:35:39 +00:00
let mut 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-01 13:35:39 +00:00
let field_args = args::InputField::parse(&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;
let name = field_args.name.unwrap_or_else(|| ident.to_string());
get_fields.push(quote! {
2020-03-02 00:24:49 +00:00
let #ident:#ty = #crate_name::GQLInputValue::parse(obj.remove(#name).unwrap_or(#crate_name::Value::Null))?;
2020-03-01 13:35:39 +00:00
});
2020-03-01 16:52:05 +00:00
get_json_fields.push(quote! {
2020-03-02 00:24:49 +00:00
let #ident:#ty = #crate_name::GQLInputValue::parse_from_json(obj.remove(#name).unwrap_or(#crate_name::serde_json::Value::Null))?;
2020-03-01 16:52:05 +00:00
});
2020-03-01 13:35:39 +00:00
fields.push(ident);
2020-03-01 10:54:34 +00:00
}
let expanded = quote! {
#input
2020-03-02 00:24:49 +00:00
impl #crate_name::GQLType for #ident {
fn type_name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed(#gql_typename)
2020-03-01 10:54:34 +00:00
}
}
2020-03-02 00:24:49 +00:00
impl #crate_name::GQLInputValue for #ident {
fn parse(value: #crate_name::Value) -> #crate_name::Result<Self> {
if let #crate_name::Value::Object(mut obj) = value {
2020-03-01 13:35:39 +00:00
#(#get_fields)*
Ok(Self { #(#fields),* })
} else {
2020-03-02 00:24:49 +00:00
Err(#crate_name::QueryError::ExpectedType {
2020-03-01 13:35:39 +00:00
expect: Self::type_name(),
actual: value,
}.into())
}
}
2020-03-01 16:52:05 +00:00
2020-03-02 00:24:49 +00:00
fn parse_from_json(value: #crate_name::serde_json::Value) -> #crate_name::Result<Self> {
if let #crate_name::serde_json::Value::Object(mut obj) = value {
2020-03-01 16:52:05 +00:00
#(#get_json_fields)*
Ok(Self { #(#fields),* })
} else {
2020-03-02 00:24:49 +00:00
Err(#crate_name::QueryError::ExpectedJsonType {
2020-03-01 16:52:05 +00:00
expect: Self::type_name(),
actual: value,
}.into())
}
}
2020-03-01 10:54:34 +00:00
}
2020-03-01 13:35:39 +00:00
2020-03-02 00:24:49 +00:00
impl #crate_name::GQLInputObject for #ident {}
2020-03-01 10:54:34 +00:00
};
Ok(expanded.into())
}