(codegen) Allow #[deprecated] on field in impl_object

This commit is contained in:
Christoph Herzog 2019-05-13 12:37:22 +02:00
parent db0d5952dd
commit 520cac29a0
No known key found for this signature in database
GPG key ID: DAFF71D48B493238
2 changed files with 78 additions and 14 deletions

View file

@ -33,6 +33,16 @@ impl Root {
0
}
#[deprecated]
fn deprecated_outer() -> bool {
true
}
#[deprecated(note = "Deprecation Reason")]
fn deprecated_outer_with_reason() -> bool {
true
}
#[graphql(deprecated = "Deprecation reason")]
fn deprecated() -> i32 {
0
@ -275,6 +285,44 @@ fn introspect_interface_field_description() {
});
}
#[test]
fn introspect_object_field_deprecated_outer() {
run_field_info_query("Root", "deprecatedOuter", |field| {
assert_eq!(
field.get_field_value("name"),
Some(&Value::scalar("deprecatedOuter"))
);
assert_eq!(field.get_field_value("description"), Some(&Value::null()));
assert_eq!(
field.get_field_value("isDeprecated"),
Some(&Value::scalar(true))
);
assert_eq!(
field.get_field_value("deprecationReason"),
Some(&Value::null()),
);
});
}
#[test]
fn introspect_object_field_deprecated_outer_with_reason() {
run_field_info_query("Root", "deprecatedOuterWithReason", |field| {
assert_eq!(
field.get_field_value("name"),
Some(&Value::scalar("deprecatedOuterWithReason"))
);
assert_eq!(field.get_field_value("description"), Some(&Value::null()));
assert_eq!(
field.get_field_value("isDeprecated"),
Some(&Value::scalar(true))
);
assert_eq!(
field.get_field_value("deprecationReason"),
Some(&Value::scalar("Deprecation Reason")),
);
});
}
#[test]
fn introspect_object_field_deprecated() {
run_field_info_query("Root", "deprecated", |field| {

View file

@ -76,14 +76,23 @@ pub fn get_deprecated(attrs: &Vec<Attribute>) -> Option<DeprecationAttr> {
fn get_deprecated_meta_list(list: &MetaList) -> DeprecationAttr {
for meta in &list.nested {
match meta {
&NestedMeta::Meta(Meta::NameValue(ref nv)) if nv.ident == "note" => match &nv.lit {
&Lit::Str(ref strlit) => {
return DeprecationAttr {
reason: Some(strlit.value().to_string()),
};
&NestedMeta::Meta(Meta::NameValue(ref nv)) => {
if nv.ident == "note" {
match &nv.lit {
&Lit::Str(ref strlit) => {
return DeprecationAttr {
reason: Some(strlit.value()),
};
}
_ => panic!("deprecated attribute note value only has string literal"),
}
} else {
panic!(
"Unrecognized setting on #[deprecated(..)] attribute: {}",
nv.ident
);
}
_ => panic!("deprecated attribute note value only has string literal"),
},
}
_ => {}
}
}
@ -434,7 +443,7 @@ pub enum FieldAttributeParseMode {
enum FieldAttribute {
Name(syn::LitStr),
Description(syn::LitStr),
Deprecation(Option<syn::LitStr>),
Deprecation(DeprecationAttr),
Skip(syn::Ident),
Arguments(HashMap<String, FieldAttributeArgument>),
}
@ -466,11 +475,13 @@ impl parse::Parse for FieldAttribute {
"deprecated" | "deprecation" => {
let reason = if input.peek(Token![=]) {
input.parse::<Token![=]>()?;
Some(input.parse()?)
Some(input.parse::<syn::LitStr>()?.value())
} else {
None
};
Ok(FieldAttribute::Deprecation(reason))
Ok(FieldAttribute::Deprecation(DeprecationAttr {
reason: reason,
}))
}
"skip" => Ok(FieldAttribute::Skip(ident)),
"arguments" => {
@ -525,10 +536,8 @@ impl parse::Parse for FieldAttributes {
FieldAttribute::Description(name) => {
output.description = Some(name.value());
}
FieldAttribute::Deprecation(reason_opt) => {
output.deprecation = Some(DeprecationAttr {
reason: reason_opt.map(|val| val.value()),
});
FieldAttribute::Deprecation(attr) => {
output.deprecation = Some(attr);
}
FieldAttribute::Skip(_) => {
output.skip = true;
@ -553,6 +562,7 @@ impl FieldAttributes {
_mode: FieldAttributeParseMode,
) -> syn::parse::Result<Self> {
let doc_comment = get_doc_comment(&attrs);
let deprecation = get_deprecated(&attrs);
let attr_opt = attrs
.into_iter()
@ -562,9 +572,15 @@ impl FieldAttributes {
Some(attr) => syn::parse(attr.tts.into())?,
None => Self::default(),
};
// Check for regular doc comment.
if output.description.is_none() {
output.description = doc_comment;
}
if output.deprecation.is_none() {
output.deprecation = deprecation;
}
Ok(output)
}