Merge pull request #190 from phated/fix-derive-unwrap
Fix issue with unwrapping derive attributes
This commit is contained in:
commit
79e9547954
|
@ -41,14 +41,17 @@ pub fn add_container_attrs(
|
||||||
pub fn parse_derive(input: TokenStream) -> Result<(proc_macro::TokenStream, DeriveInput)> {
|
pub fn parse_derive(input: TokenStream) -> Result<(proc_macro::TokenStream, DeriveInput)> {
|
||||||
let mut input: DeriveInput = syn::parse2(input)?;
|
let mut input: DeriveInput = syn::parse2(input)?;
|
||||||
let attrs = &mut input.attrs;
|
let attrs = &mut input.attrs;
|
||||||
let pos = attrs
|
let graphql_attr = attrs
|
||||||
.iter()
|
.iter()
|
||||||
.find_position(|attr| attr.path.is_ident("graphql"))
|
.find_position(|attr| attr.path.is_ident("graphql"));
|
||||||
.unwrap()
|
|
||||||
.0;
|
if let Some((pos, _attr)) = graphql_attr {
|
||||||
let attribute = attrs.remove(pos);
|
let attribute = attrs.remove(pos);
|
||||||
let args = attribute.parse_args::<TokenStream>()?;
|
let args = attribute.parse_args::<TokenStream>()?;
|
||||||
Ok((args.into(), input))
|
Ok((args.into(), input))
|
||||||
|
} else {
|
||||||
|
Ok((TokenStream::new().into(), input))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_nested_validator(
|
fn parse_nested_validator(
|
||||||
|
|
|
@ -8,36 +8,60 @@ pub async fn test_derive() {
|
||||||
A,
|
A,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Infers the name based on Rust name
|
||||||
#[derive(GQLEnum, Eq, Copy, PartialEq, Clone)]
|
#[derive(GQLEnum, Eq, Copy, PartialEq, Clone)]
|
||||||
#[graphql(name = "MyEnumDerive1")]
|
|
||||||
enum MyEnumDerive {
|
enum MyEnumDerive {
|
||||||
#[cfg_attr(feature = "bson", item(name = "A1"))]
|
#[cfg_attr(feature = "bson", item(name = "A1"))]
|
||||||
A,
|
A,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Can be renamed with graphql(name = ..) attribute
|
||||||
|
#[derive(GQLEnum, Eq, Copy, PartialEq, Clone)]
|
||||||
|
#[graphql(name = "MyEnumDerive")]
|
||||||
|
enum MyEnumDeriveRenamed {
|
||||||
|
#[cfg_attr(feature = "bson", item(name = "A1"))]
|
||||||
|
A,
|
||||||
|
}
|
||||||
|
|
||||||
#[InputObject(name = "MyInputObj1")]
|
#[InputObject(name = "MyInputObj1")]
|
||||||
struct MyInputObj {
|
struct MyInputObj {
|
||||||
#[cfg_attr(feature = "bson", field(default))]
|
#[cfg_attr(feature = "bson", field(default))]
|
||||||
value: i32,
|
value: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Infers the name based on Rust name
|
||||||
#[derive(GQLInputObject)]
|
#[derive(GQLInputObject)]
|
||||||
#[graphql(name = "MyInputObjDerive1")]
|
|
||||||
struct MyInputObjDerive {
|
struct MyInputObjDerive {
|
||||||
#[cfg_attr(feature = "bson", field(default))]
|
#[cfg_attr(feature = "bson", field(default))]
|
||||||
value: i32,
|
value: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Can be renamed with graphql(name = ..) attribute
|
||||||
|
#[derive(GQLInputObject)]
|
||||||
|
#[graphql(name = "MyInputObjDerive")]
|
||||||
|
struct MyInputObjDeriveRenamed {
|
||||||
|
#[cfg_attr(feature = "bson", field(default))]
|
||||||
|
value: i32,
|
||||||
|
}
|
||||||
|
|
||||||
#[InputObject(name = "MySimpleObj1")]
|
#[InputObject(name = "MySimpleObj1")]
|
||||||
struct MySimpleObj {
|
struct MySimpleObj {
|
||||||
#[cfg_attr(feature = "bson", field(name = "value1"))]
|
#[cfg_attr(feature = "bson", field(name = "value1"))]
|
||||||
value: i32,
|
value: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Infers the name based on Rust name
|
||||||
#[derive(GQLInputObject)]
|
#[derive(GQLInputObject)]
|
||||||
#[graphql(name = "MySimpleObjDerive1")]
|
|
||||||
struct MySimpleObjDerive {
|
struct MySimpleObjDerive {
|
||||||
#[cfg_attr(feature = "bson", field(name = "value1"))]
|
#[cfg_attr(feature = "bson", field(name = "value1"))]
|
||||||
value: i32,
|
value: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Can be renamed with graphql(name = ..) attribute
|
||||||
|
#[derive(GQLInputObject)]
|
||||||
|
#[graphql(name = "MySimpleObjDerive")]
|
||||||
|
struct MySimpleObjDeriveRenamed {
|
||||||
|
#[cfg_attr(feature = "bson", field(name = "value1"))]
|
||||||
|
value: i32,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user