Keep parameter names of queries in query_group attribute

This commit is contained in:
Lukas Wirth 2021-06-28 17:16:09 +02:00
parent 6db6c3c4d0
commit 26b47e02e9

View file

@ -122,13 +122,17 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
.into(); .into();
} }
} }
let mut keys: Vec<Type> = vec![]; let mut keys: Vec<(Ident, Type)> = vec![];
for arg in iter { for (idx, arg) in iter.enumerate() {
match *arg { match arg {
FnArg::Typed(ref arg) => { FnArg::Typed(syn::PatType { pat, ty, .. }) => keys.push((
keys.push((*arg.ty).clone()); match pat.as_ref() {
} syn::Pat::Ident(ident_pat) => ident_pat.ident.clone(),
ref arg => { _ => Ident::new(&format!("key{}", idx), Span::call_site()),
},
Type::clone(ty),
)),
arg => {
return Error::new( return Error::new(
arg.span(), arg.span(),
format!("unsupported argument `{:?}` of `{}`", arg, method.sig.ident,), format!("unsupported argument `{:?}` of `{}`", arg, method.sig.ident,),
@ -176,9 +180,9 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
&format!("lookup_{}", method.sig.ident.to_string()), &format!("lookup_{}", method.sig.ident.to_string()),
method.sig.ident.span(), method.sig.ident.span(),
); );
let keys = &keys; let keys = keys.iter().map(|(_, ty)| ty);
let lookup_value: Type = parse_quote!((#(#keys),*)); let lookup_value: Type = parse_quote!((#(#keys),*));
let lookup_keys = vec![value.clone()]; let lookup_keys = vec![(parse_quote! { key }, value.clone())];
Some(Query { Some(Query {
query_type: lookup_query_type, query_type: lookup_query_type,
query_name: format!("lookup_{}", query_name), query_name: format!("lookup_{}", query_name),
@ -222,10 +226,8 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
let mut storage_fields = proc_macro2::TokenStream::new(); let mut storage_fields = proc_macro2::TokenStream::new();
let mut queries_with_storage = vec![]; let mut queries_with_storage = vec![];
for query in &queries { for query in &queries {
let key_names: &Vec<_> = &(0..query.keys.len()) let (key_names, keys): (Vec<_>, Vec<_>) =
.map(|i| Ident::new(&format!("key{}", i), Span::call_site())) query.keys.iter().map(|(pat, ty)| (pat, ty)).unzip();
.collect();
let keys = &query.keys;
let value = &query.value; let value = &query.value;
let fn_name = &query.fn_name; let fn_name = &query.fn_name;
let qt = &query.query_type; let qt = &query.query_type;
@ -396,7 +398,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
} }
QueryStorage::Transparent => panic!("should have been filtered"), QueryStorage::Transparent => panic!("should have been filtered"),
}; };
let keys = &query.keys; let keys = query.keys.iter().map(|(_, ty)| ty);
let value = &query.value; let value = &query.value;
let query_name = &query.query_name; let query_name = &query.query_name;
@ -489,9 +491,8 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
// Implement the QueryFunction trait for queries which need it. // Implement the QueryFunction trait for queries which need it.
if query.storage.needs_query_function() { if query.storage.needs_query_function() {
let span = query.fn_name.span(); let span = query.fn_name.span();
let key_names: &Vec<_> = &(0..query.keys.len())
.map(|i| Ident::new(&format!("key{}", i), Span::call_site())) let key_names: Vec<_> = query.keys.iter().map(|(pat, _)| pat).collect();
.collect();
let key_pattern = if query.keys.len() == 1 { let key_pattern = if query.keys.len() == 1 {
quote! { #(#key_names),* } quote! { #(#key_names),* }
} else { } else {
@ -683,7 +684,7 @@ struct Query {
attrs: Vec<syn::Attribute>, attrs: Vec<syn::Attribute>,
query_type: Ident, query_type: Ident,
storage: QueryStorage, storage: QueryStorage,
keys: Vec<syn::Type>, keys: Vec<(Ident, syn::Type)>,
value: syn::Type, value: syn::Type,
invoke: Option<syn::Path>, invoke: Option<syn::Path>,
cycle: Option<syn::Path>, cycle: Option<syn::Path>,