-
Notifications
You must be signed in to change notification settings - Fork 3
Generate endpoint type for #[derive(WpDerivedRequest)]
#149
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
328ba06
Implement endpoint generation for wp_derive_request_builder
oguzkocer 5720fe8
Add ApiBaseUrl.by_extending_and_splitting_by_forward_slash
oguzkocer 702b918
Add generated mod to users endpoint to showcase derived endpoint
oguzkocer 352d321
Add fields query pairs to generated endpoint filter functions
oguzkocer dfa7e3e
Add unit tests for splitting url parts in wp_derive_request_builder
oguzkocer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
use std::fmt::Display; | ||
|
||
use helpers_to_generate_tokens::*; | ||
use proc_macro2::{Span, TokenStream}; | ||
use proc_macro_crate::{crate_name, FoundCrate}; | ||
use quote::{format_ident, quote}; | ||
use strum::IntoEnumIterator; | ||
use strum_macros::EnumIter; | ||
use syn::Ident; | ||
|
||
use crate::{ | ||
parse::{ParsedEnum, ParsedVariant, RequestType}, | ||
sparse_field_attr::SparseFieldAttr, | ||
}; | ||
|
||
mod helpers_to_generate_tokens; | ||
|
||
pub(crate) fn generate_types(parsed_enum: &ParsedEnum) -> syn::Result<TokenStream> { | ||
let config = Config::new(parsed_enum); | ||
|
||
Ok(generate_endpoint_type(&config, parsed_enum)) | ||
} | ||
|
||
fn generate_endpoint_type(config: &Config, parsed_enum: &ParsedEnum) -> TokenStream { | ||
let api_base_url_type = &config.api_base_url_type; | ||
let endpoint_ident = format_ident!("{}Endpoint", parsed_enum.enum_ident); | ||
|
||
let functions = parsed_enum.variants.iter().map(|variant| { | ||
let url_parts = variant.attr.url_parts.as_slice(); | ||
let params_type = &variant.attr.params; | ||
let request_type = variant.attr.request_type; | ||
let url_from_endpoint = fn_body_get_url_from_api_base_url(url_parts); | ||
let query_pairs = fn_body_query_pairs(params_type, request_type); | ||
|
||
ContextAndFilterHandler::from_request_type(request_type) | ||
.into_iter() | ||
.map(|context_and_filter_handler| { | ||
let fn_signature = fn_signature( | ||
PartOf::Endpoint, | ||
&variant.variant_ident, | ||
url_parts, | ||
params_type, | ||
request_type, | ||
context_and_filter_handler, | ||
&config.sparse_field_type, | ||
); | ||
let context_query_pair = | ||
fn_body_context_query_pairs(&config.crate_ident, context_and_filter_handler); | ||
let api_endpoint_url_type = &config.api_endpoint_url_type; | ||
let fields_query_pairs = | ||
fn_body_fields_query_pairs(&config.crate_ident, context_and_filter_handler); | ||
quote! { | ||
pub #fn_signature -> #api_endpoint_url_type { | ||
#url_from_endpoint | ||
#context_query_pair | ||
#query_pairs | ||
#fields_query_pairs | ||
url.into() | ||
} | ||
} | ||
}) | ||
.collect::<TokenStream>() | ||
}); | ||
|
||
quote! { | ||
#[derive(Debug)] | ||
pub struct #endpoint_ident { | ||
api_base_url: #api_base_url_type, | ||
} | ||
|
||
impl #endpoint_ident { | ||
pub fn new(api_base_url: #api_base_url_type) -> Self { | ||
Self { api_base_url } | ||
} | ||
|
||
#(#functions)* | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub enum PartOf { | ||
Endpoint, | ||
RequestBuilder, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub enum ContextAndFilterHandler { | ||
None, | ||
NoFilterTakeContextAsArgument, | ||
NoFilterTakeContextAsFunctionName(WpContext), | ||
FilterTakeContextAsArgument, | ||
FilterNoContext, | ||
} | ||
|
||
impl ContextAndFilterHandler { | ||
fn from_request_type(request_type: RequestType) -> Vec<Self> { | ||
match request_type { | ||
crate::parse::RequestType::ContextualGet => { | ||
let mut v: Vec<Self> = WpContext::iter() | ||
.map(Self::NoFilterTakeContextAsFunctionName) | ||
.collect(); | ||
v.push(ContextAndFilterHandler::FilterTakeContextAsArgument); | ||
v | ||
} | ||
crate::parse::RequestType::Get => { | ||
vec![ | ||
ContextAndFilterHandler::NoFilterTakeContextAsArgument, | ||
ContextAndFilterHandler::FilterTakeContextAsArgument, | ||
] | ||
} | ||
crate::parse::RequestType::Delete | crate::parse::RequestType::Post => { | ||
vec![ | ||
ContextAndFilterHandler::None, | ||
ContextAndFilterHandler::FilterNoContext, | ||
] | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, EnumIter)] | ||
pub enum WpContext { | ||
Edit, | ||
Embed, | ||
View, | ||
} | ||
|
||
impl Display for WpContext { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let s = match self { | ||
WpContext::Edit => "Edit", | ||
WpContext::Embed => "Embed", | ||
WpContext::View => "View", | ||
}; | ||
write!(f, "{}", s) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Config { | ||
pub crate_ident: Ident, | ||
pub api_base_url_type: TokenStream, | ||
pub api_endpoint_url_type: TokenStream, | ||
pub request_builder_type: TokenStream, | ||
pub endpoint_ident: Ident, | ||
pub request_builder_ident: Ident, | ||
pub sparse_field_type: SparseFieldAttr, | ||
} | ||
|
||
impl Config { | ||
fn new(parsed_enum: &ParsedEnum) -> Self { | ||
let crate_name = "wp_api"; | ||
let found_crate = proc_macro_crate::crate_name(crate_name) | ||
.unwrap_or_else(|_| panic!("{} is not present in `Cargo.toml`", crate_name)); | ||
|
||
let crate_ident = match found_crate { | ||
FoundCrate::Itself => format_ident!("crate"), | ||
FoundCrate::Name(name) => Ident::new(&name, Span::call_site()), | ||
}; | ||
let api_base_url_type = | ||
quote! { std::sync::Arc<#crate_ident::request::endpoint::ApiBaseUrl> }; | ||
let api_endpoint_url_type = quote! { #crate_ident::request::endpoint::ApiEndpointUrl }; | ||
let request_builder_type = quote! { std::sync::Arc<#crate_ident::request::RequestBuilder> }; | ||
Self { | ||
crate_ident, | ||
api_base_url_type, | ||
api_endpoint_url_type, | ||
request_builder_type, | ||
endpoint_ident: format_ident!("{}Endpoint", parsed_enum.enum_ident), | ||
request_builder_ident: format_ident!("{}Builder", parsed_enum.enum_ident), | ||
sparse_field_type: parsed_enum.sparse_field_attr.clone(), | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method would probably benefit from some of its own testing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's address that at a later time. I am not sure if this function will remain or not.