Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: feat: flatten request #1159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions proc-macros/src/render_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl RpcDescription {
// provided `Params` object.
// `params_seq` is the comma-delimited sequence of parameters we're passing to the rust function
// called..
let (parsing, params_seq) = self.render_params_decoding(&method.params, None);
let (parsing, params_seq) = self.render_params_decoding(&method.params, None, method.flatten);

let into_response = self.jrps_server_item(quote! { IntoResponse });

Expand Down Expand Up @@ -170,7 +170,7 @@ impl RpcDescription {
// provided `Params` object.
// `params_seq` is the comma-delimited sequence of parameters.
let pending = proc_macro2::Ident::new("subscription_sink", rust_method_name.span());
let (parsing, params_seq) = self.render_params_decoding(&sub.params, Some(pending));
let (parsing, params_seq) = self.render_params_decoding(&sub.params, Some(pending), false);
let into_sub_response = self.jrps_server_item(quote! { IntoSubscriptionCloseResponse });

check_name(&rpc_sub_name, rust_method_name.span());
Expand Down Expand Up @@ -279,6 +279,7 @@ impl RpcDescription {
&self,
params: &[(syn::PatIdent, syn::Type)],
sub: Option<proc_macro2::Ident>,
flatten: bool,
) -> (TokenStream2, TokenStream2) {
if params.is_empty() {
return (TokenStream2::default(), TokenStream2::default());
Expand Down Expand Up @@ -380,8 +381,15 @@ impl RpcDescription {
tokens: TokenStream2::from_str(&format!("({})", alias_vals.as_str())).unwrap(),
};

let serde_flatten = if flatten {
quote! { #[serde(flatten)] }
} else {
quote! {}
};

quote! {
#serde_alias
#serde_flatten
#name: #ty,
}
});
Expand Down
12 changes: 9 additions & 3 deletions proc-macros/src/rpc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ pub struct RpcMethod {
pub returns: Option<syn::Type>,
pub signature: syn::TraitItemMethod,
pub aliases: Vec<String>,
pub flatten: bool,
}

impl RpcMethod {
pub fn from_item(attr: Attribute, mut method: syn::TraitItemMethod) -> syn::Result<Self> {
let [aliases, blocking, name, param_kind] =
AttributeMeta::parse(attr)?.retain(["aliases", "blocking", "name", "param_kind"])?;
let [aliases, blocking, name, param_kind, flatten] =
AttributeMeta::parse(attr)?.retain(["aliases", "blocking", "name", "param_kind", "flatten"])?;

let aliases = parse_aliases(aliases)?;
let blocking = optional(blocking, Argument::flag)?.is_some();
let name = name?.string()?;
let param_kind = parse_param_kind(param_kind)?;
let flatten = optional(flatten, Argument::flag)?.is_some();

let sig = method.sig.clone();
let docs = extract_doc_comments(&method.attrs);
Expand All @@ -67,6 +69,10 @@ impl RpcMethod {
None => quote!(),
};

if flatten && !matches!(param_kind, ParamKind::Map) {
return Err(syn::Error::new(sig.span(), "Flatten request must have param_kind=map"));
}

if blocking && sig.asyncness.is_some() {
return Err(syn::Error::new(sig.span(), "Blocking method must be synchronous"));
}
Expand Down Expand Up @@ -98,7 +104,7 @@ impl RpcMethod {
// We've analyzed attributes and don't need them anymore.
method.attrs.clear();

Ok(Self { aliases, blocking, name, params, param_kind, returns, signature: method, docs, deprecated })
Ok(Self { aliases, blocking, name, params, param_kind, returns, signature: method, docs, deprecated, flatten })
}
}

Expand Down