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