Skip to content

Commit

Permalink
Add support for async functions in pdk_query_types (#9)
Browse files Browse the repository at this point in the history
* Add support for async functions in pdk_query_types

Currently it is impossible to use an asynchronous handler when using the
convenience macro `pdk_query_types`.

This adds the ability to append `.await` to the handler property to tell the macro to expect an
asynchronous handler.
  • Loading branch information
gagbo authored Feb 10, 2023
1 parent 444e6de commit 437798f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ exclude = ["providers/.cargo"]
authors = ["Fiberplane <[email protected]>"]
edition = "2021"
rust-version = "1.65"
version = "1.0.0-alpha.2"
version = "1.0.0-alpha.3"
license = "MIT OR Apache-2.0"

[workspace.dependencies]
fp-bindgen = { version = "3.0.0-alpha.1" }
fp-bindgen-support = { version = "3.0.0-alpha.1" }
#fiberplane-models = { version = "1.0.0-alpha.2", registry = "artifactory" }
fiberplane-models = { git = "ssh://[email protected]/fiberplane/fiberplane-rs.git", branch = "main" }
fiberplane-pdk = { version = "1.0.0-alpha.1", path = "fiberplane-pdk" }
fiberplane-pdk-macros = { version = "1.0.0-alpha.1", path = "fiberplane-pdk-macros" }
fiberplane-pdk = { version = "1.0.0-alpha.3", path = "fiberplane-pdk" }
fiberplane-pdk-macros = { version = "1.0.0-alpha.3", path = "fiberplane-pdk-macros" }
#fiberplane-provider-bindings = { version = "1.0.0-alpha.2", registry = "artifactory" }
fiberplane-provider-bindings = { git = "ssh://[email protected]/fiberplane/fiberplane-rs.git", branch = "main" }

Expand Down
7 changes: 4 additions & 3 deletions fiberplane-pdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use quote::quote;
/// requests of the given type. Handlers may have one or two arguments, the
/// types of which are given between parentheses. The first argument is always
/// the type of the query data, while the second optionally specifies a type
/// for the provider's config. Providing a handler is mandatory.
/// for the provider's config. Add a `.await` call to the handler
/// if the handler is asynchronous. Providing a handler is mandatory.
/// * **label** - A label that is used when presenting the query type to the
/// user. Some query types are not intended to be user-selected (such as the
/// `status` query type) in which case they should not define a label.
Expand Down Expand Up @@ -49,7 +50,7 @@ use quote::quote;
/// },
/// "x-custom-query-type" => {
/// label: "My custom query",
/// handler: query_custom(ExampleQueryData),
/// handler: query_custom(ExampleQueryData).await,
/// supported_mime_types: ["application/vnd.fiberplane.provider.my-provider.custom-data"],
/// }
/// }
Expand All @@ -58,7 +59,7 @@ use quote::quote;
/// todo!("Implement timeseries query handling")
/// }
///
/// fn query_custom(query_data: ExampleQueryData) -> Result<Blob> {
/// async fn query_custom(query_data: ExampleQueryData) -> Result<Blob> {
/// todo!("Implement custom query handling")
/// }
/// ```
Expand Down
26 changes: 22 additions & 4 deletions fiberplane-pdk-macros/src/query_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ pub fn define_query_types(input: TokenStream) -> TokenStream {
};
quote! { #ty::parse(#parse_arg)? }
});
quote! { #fn_name(#(#args),*) }
if handler.is_async {
quote! { #fn_name(#(#args),*).await }
} else {
quote! { #fn_name(#(#args),*) }
}
});

quote! { #identifier => #handler }
Expand Down Expand Up @@ -172,6 +176,7 @@ impl Parse for QueryTypeField {
}

struct QueryHandler {
is_async: bool,
ident: Ident,
_parens: token::Paren,
arg_types: Punctuated<Ident, Token![,]>,
Expand All @@ -180,10 +185,23 @@ struct QueryHandler {
impl Parse for QueryHandler {
fn parse(input: ParseStream) -> Result<Self> {
let content;

let ident = input.parse::<Ident>()?;
let _parens = parenthesized!(content in input);
let arg_types = content.parse_terminated(Ident::parse)?;
let is_async = if input.peek(Token![.]) && input.peek2(Token![await]) {
input.parse::<Token![.]>()?;
input.parse::<Token![await]>()?;
true
} else {
false
};

Ok(Self {
ident: input.parse()?,
_parens: parenthesized!(content in input),
arg_types: content.parse_terminated(Ident::parse)?,
is_async,
ident,
_parens,
arg_types,
})
}
}
Expand Down

0 comments on commit 437798f

Please sign in to comment.