Rewrite attribute parsing logic

This commit is contained in:
Graham Esau 2019-12-08 22:28:49 +00:00
parent 1bdbaaf082
commit 4c4fb1cf36
4 changed files with 141 additions and 92 deletions

View file

@ -11,10 +11,7 @@
"type": "string" "type": "string"
}, },
"system_cpu_time": { "system_cpu_time": {
"default": { "default": "0.000000000s",
"nanos": 0,
"secs": 0
},
"allOf": [ "allOf": [
{ {
"$ref": "#/definitions/DurationDef" "$ref": "#/definitions/DurationDef"

View file

@ -34,7 +34,6 @@ struct Process {
wall_time: Duration, wall_time: Duration,
#[serde(default, with = "DurationDef")] #[serde(default, with = "DurationDef")]
user_cpu_time: Duration, user_cpu_time: Duration,
// FIXME this should serialize the default as "0.000000000s"
#[serde(default, serialize_with = "custom_serialize")] #[serde(default, serialize_with = "custom_serialize")]
#[schemars(with = "DurationDef")] #[schemars(with = "DurationDef")]
system_cpu_time: Duration, system_cpu_time: Duration,

View file

@ -9,11 +9,12 @@ mod metadata;
mod preprocess; mod preprocess;
use metadata::*; use metadata::*;
use proc_macro2::TokenStream; use proc_macro2::{Group, Span, TokenStream, TokenTree};
use quote::ToTokens; use quote::ToTokens;
use serde_derive_internals::ast::{Container, Data, Field, Style, Variant}; use serde_derive_internals::ast::{Container, Data, Field, Style, Variant};
use serde_derive_internals::attr::{self, Default as SerdeDefault, TagType}; use serde_derive_internals::attr::{self, Default as SerdeDefault, TagType};
use serde_derive_internals::{Ctxt, Derive}; use serde_derive_internals::{Ctxt, Derive};
use syn::parse::{self, Parse};
use syn::spanned::Spanned; use syn::spanned::Spanned;
#[proc_macro_derive(JsonSchema, attributes(schemars, serde, doc))] #[proc_macro_derive(JsonSchema, attributes(schemars, serde, doc))]
@ -382,35 +383,70 @@ fn schema_for_struct(fields: &[Field], cattrs: &attr::Container) -> TokenStream
} }
fn get_json_schema_type(field: &Field) -> Box<dyn ToTokens> { fn get_json_schema_type(field: &Field) -> Box<dyn ToTokens> {
// TODO it would probably be simpler to parse attributes manually here, instead of // TODO support [schemars(schema_with= "...")] or equivalent
// using the serde-parsed attributes match field
let de_with_segments = without_last_element(field.attrs.deserialize_with(), "deserialize"); .original
let se_with_segments = without_last_element(field.attrs.serialize_with(), "serialize"); .attrs
if de_with_segments == se_with_segments { .iter()
if let Some(expr_path) = de_with_segments { .filter(|at| match at.path.get_ident() {
return Box::new(expr_path); // FIXME this is relying on order of attributes (schemars before serde) from preprocess.rs
Some(i) => i == "schemars" || i == "serde",
None => false,
})
.filter_map(get_with_from_attr)
.next()
{
Some(with) => match parse_lit_str::<syn::ExprPath>(&with) {
Ok(expr_path) => Box::new(expr_path),
Err(e) => Box::new(compile_error(vec![e])),
},
None => Box::new(field.ty.clone()),
} }
} }
Box::new(field.ty.clone())
}
fn without_last_element(path: Option<&syn::ExprPath>, last: &str) -> Option<syn::ExprPath> { fn get_with_from_attr(attr: &syn::Attribute) -> Option<syn::LitStr> {
match path { use syn::*;
Some(expr_path) let nested_metas = match attr.parse_meta() {
if expr_path Ok(Meta::List(meta)) => meta.nested,
.path _ => return None,
.segments };
.last() for nm in nested_metas {
.map(|p| p.ident == last) match nm {
.unwrap_or(false) => NestedMeta::Meta(Meta::NameValue(MetaNameValue {
path,
lit: Lit::Str(with),
..
})) if path.is_ident("with") => return Some(with),
_ => {}
}
}
None
}
fn parse_lit_str<T>(s: &syn::LitStr) -> parse::Result<T>
where
T: Parse,
{ {
let mut expr_path = expr_path.clone(); let tokens = spanned_tokens(s)?;
expr_path.path.segments.pop(); syn::parse2(tokens)
if let Some(segment) = expr_path.path.segments.pop() {
expr_path.path.segments.push(segment.into_value())
} }
Some(expr_path)
fn spanned_tokens(s: &syn::LitStr) -> parse::Result<TokenStream> {
let stream = syn::parse_str(&s.value())?;
Ok(respan_token_stream(stream, s.span()))
} }
_ => None,
fn respan_token_stream(stream: TokenStream, span: Span) -> TokenStream {
stream
.into_iter()
.map(|token| respan_token_tree(token, span))
.collect()
} }
fn respan_token_tree(mut token: TokenTree, span: Span) -> TokenTree {
if let TokenTree::Group(g) = &mut token {
*g = Group::new(g.delimiter(), respan_token_stream(g.stream(), span));
}
token.set_span(span);
token
} }

View file

@ -2,9 +2,30 @@ use quote::ToTokens;
use serde_derive_internals::Ctxt; use serde_derive_internals::Ctxt;
use std::collections::BTreeSet; use std::collections::BTreeSet;
use syn::parse::Parser; use syn::parse::Parser;
use syn::{ use syn::{Attribute, Data, DeriveInput, Field, GenericParam, Generics, Meta, NestedMeta, Variant};
Attribute, Data, DeriveInput, Field, GenericParam, Generics, Ident, Meta, NestedMeta, Variant,
}; // List of keywords that can appear in #[serde(...)]/#[schemars(...)] attributes, which we want serde to parse for us.
static SERDE_KEYWORDS: &[&str] = &[
"rename",
"rename_all",
"deny_unknown_fields",
"tag",
"content",
"untagged",
"bound",
"default",
"remote",
"alias",
"skip",
"skip_serializing",
"skip_deserializing",
"other",
"flatten",
// special cases - these keywords are not copied from schemars attrs to serde attrs
"serialize_with",
"deserialize_with",
"with",
];
pub fn add_trait_bounds(generics: &mut Generics) { pub fn add_trait_bounds(generics: &mut Generics) {
for param in &mut generics.params { for param in &mut generics.params {
@ -42,52 +63,48 @@ fn process_serde_field_attrs<'a>(ctxt: &Ctxt, fields: impl Iterator<Item = &'a m
} }
fn process_attrs(ctxt: &Ctxt, attrs: &mut Vec<Attribute>) { fn process_attrs(ctxt: &Ctxt, attrs: &mut Vec<Attribute>) {
let mut schemars_attrs = Vec::<Attribute>::new(); let (serde_attrs, other_attrs): (Vec<_>, Vec<_>) =
let mut serde_attrs = Vec::<Attribute>::new(); attrs.drain(..).partition(|at| at.path.is_ident("serde"));
let mut misc_attrs = Vec::<Attribute>::new();
for attr in attrs.drain(..) { *attrs = other_attrs;
if attr.path.is_ident("schemars") {
schemars_attrs.push(attr)
} else if attr.path.is_ident("serde") {
serde_attrs.push(attr)
} else {
misc_attrs.push(attr)
}
}
for attr in schemars_attrs.iter_mut() { let schemars_attrs: Vec<_> = attrs
let schemars_ident = attr.path.segments.pop().unwrap().into_value().ident;
attr.path
.segments
.push(Ident::new("serde", schemars_ident.span()).into());
}
let mut schemars_meta_names: BTreeSet<String> = schemars_attrs
.iter() .iter()
.flat_map(|attr| get_meta_items(&ctxt, attr)) .filter(|at| at.path.is_ident("schemars"))
.flatten()
.flat_map(|m| get_meta_ident(&ctxt, &m))
.collect(); .collect();
if schemars_meta_names.contains("with") {
schemars_meta_names.insert("serialize_with".to_string()); let (mut serde_meta, mut schemars_meta_names): (Vec<_>, BTreeSet<_>) = schemars_attrs
schemars_meta_names.insert("deserialize_with".to_string()); .iter()
.flat_map(|at| get_meta_items(&ctxt, at))
.flatten()
.filter_map(|meta| {
let keyword = get_meta_ident(&ctxt, &meta).ok()?;
if keyword.ends_with("with") || !SERDE_KEYWORDS.contains(&keyword.as_ref()) {
None
} else {
Some((meta, keyword))
}
})
.unzip();
if schemars_meta_names.contains("skip") {
schemars_meta_names.insert("skip_serializing".to_string());
schemars_meta_names.insert("skip_deserializing".to_string());
} }
let mut serde_meta = serde_attrs for meta in serde_attrs
.iter() .into_iter()
.flat_map(|attr| get_meta_items(&ctxt, attr)) .flat_map(|at| get_meta_items(&ctxt, &at))
.flatten() .flatten()
.filter(|m| { {
get_meta_ident(&ctxt, m) if let Ok(i) = get_meta_ident(&ctxt, &meta) {
.map(|i| !schemars_meta_names.contains(&i)) if !schemars_meta_names.contains(&i) && SERDE_KEYWORDS.contains(&i.as_ref()) {
.unwrap_or(false) serde_meta.push(meta);
}) }
.peekable(); }
}
*attrs = schemars_attrs; if !serde_meta.is_empty() {
if serde_meta.peek().is_some() {
let new_serde_attr = quote! { let new_serde_attr = quote! {
#[serde(#(#serde_meta),*)] #[serde(#(#serde_meta),*)]
}; };
@ -98,8 +115,6 @@ fn process_attrs(ctxt: &Ctxt, attrs: &mut Vec<Attribute>) {
Err(e) => ctxt.error_spanned_by(to_tokens(attrs), e), Err(e) => ctxt.error_spanned_by(to_tokens(attrs), e),
} }
} }
attrs.extend(misc_attrs)
} }
fn to_tokens(attrs: &[Attribute]) -> impl ToTokens { fn to_tokens(attrs: &[Attribute]) -> impl ToTokens {
@ -149,33 +164,35 @@ mod tests {
#[test] #[test]
fn test_process_serde_attrs() { fn test_process_serde_attrs() {
let mut input: DeriveInput = parse_quote! { let mut input: DeriveInput = parse_quote! {
#[serde(container, container2 = "blah")] #[serde(rename(serialize = "ser_name"), rename_all = "camelCase")]
#[serde(container3(foo, bar))] #[serde(default, unknown_word)]
#[schemars(container2 = "overridden", container4)] #[schemars(rename = "overriden", another_unknown_word)]
#[misc] #[misc]
struct MyStruct { struct MyStruct {
/// blah blah blah /// blah blah blah
#[serde(field, field2)] #[serde(alias = "first")]
field1: i32, field1: i32,
#[serde(field, field2, serialize_with = "se", deserialize_with = "de")] #[serde(serialize_with = "se", deserialize_with = "de")]
#[schemars(field = "overridden", with = "with")] #[schemars(with = "with")]
field2: i32, field2: i32,
#[schemars(field)] #[schemars(skip)]
#[serde(skip_serializing)]
field3: i32, field3: i32,
} }
}; };
let expected: DeriveInput = parse_quote! { let expected: DeriveInput = parse_quote! {
#[serde(container2 = "overridden", container4)] #[schemars(rename = "overriden", another_unknown_word)]
#[serde(container, container3(foo, bar))]
#[misc] #[misc]
#[serde(rename = "overriden", rename_all = "camelCase", default)]
struct MyStruct { struct MyStruct {
#[serde(field, field2)]
#[doc = r" blah blah blah"] #[doc = r" blah blah blah"]
#[serde(alias = "first")]
field1: i32, field1: i32,
#[serde(field = "overridden", with = "with")] #[schemars(with = "with")]
#[serde(field2)] #[serde(serialize_with = "se", deserialize_with = "de")]
field2: i32, field2: i32,
#[serde(field)] #[schemars(skip)]
#[serde(skip)]
field3: i32, field3: i32,
} }
}; };