Skip to content

Commit 4c6409f

Browse files
committed
[#15] body bind 및 path param bind 구현
1 parent 11da7e1 commit 4c6409f

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

rupring/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,5 +619,6 @@ impl<T: IModule + Clone + Copy + Sync + Send + 'static> RupringFactory<T> {
619619
#[cfg(test)]
620620
mod test_proc_macro;
621621

622+
pub use anyhow;
622623
pub use serde;
623624
pub use serde_json;

rupring/src/request.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ pub struct Request {
1313
pub(crate) di_context: Arc<crate::DIContext>,
1414
}
1515

16+
pub trait BindFromRequest {
17+
fn bind(&mut self, request: Request) -> anyhow::Result<Self>
18+
where
19+
Self: Sized;
20+
}
21+
1622
impl UnwindSafe for Request {}
1723

1824
impl Request {

rupring_macro/src/lib.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,10 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream {
638638
format!(r#"#[derive(rupring::serde::Serialize, rupring::serde::Deserialize)]"#).as_str();
639639
define_struct_for_json += format!(r#"pub struct {struct_name}__JSON {{"#).as_str();
640640

641+
let mut json_field_names = vec![];
642+
let mut path_field_names = vec![];
643+
let mut query_field_names = vec![];
644+
641645
for field in ast.fields.iter() {
642646
let mut description = "".to_string();
643647
let mut example = r#""""#.to_string();
@@ -759,6 +763,8 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream {
759763
}
760764

761765
if is_path_parameter {
766+
path_field_names.push(field_name.clone());
767+
762768
code += format!(
763769
r#"swagger_definition.path_parameters.push(rupring::swagger::json::SwaggerParameter {{
764770
name: "{field_name}".to_string(),
@@ -778,6 +784,8 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream {
778784
}
779785

780786
if is_query_parameter {
787+
query_field_names.push(field_name.clone());
788+
781789
code += format!(
782790
r#"swagger_definition.query_parameters.push(rupring::swagger::json::SwaggerParameter {{
783791
name: "{field_name}".to_string(),
@@ -796,6 +804,8 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream {
796804
continue;
797805
}
798806

807+
json_field_names.push(field_name.clone());
808+
799809
define_struct_for_json += format!(
800810
r#"
801811
pub {field_name}: {field_type},
@@ -853,5 +863,44 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream {
853863

854864
code += define_struct_for_json.as_str();
855865

866+
let mut request_bind_code = "".to_string();
867+
request_bind_code +=
868+
format!(r#"impl rupring::request::BindFromRequest for {struct_name} {{"#).as_str();
869+
870+
request_bind_code +=
871+
"fn bind(&mut self, request: rupring::request::Request) -> rupring::anyhow::Result<Self> {";
872+
request_bind_code += "use rupring::ParamStringDeserializer;";
873+
874+
request_bind_code += format!("let mut json_bound = rupring::serde_json::from_str::<{struct_name}__JSON>(request.body.as_str())?;").as_str();
875+
876+
request_bind_code += format!("let mut bound = {struct_name} {{").as_str();
877+
878+
for field_name in json_field_names {
879+
request_bind_code += format!("{field_name}: json_bound.{field_name},").as_str();
880+
}
881+
882+
for field_name in path_field_names {
883+
request_bind_code += format!(
884+
r#"{field_name}: rupring::ParamString(
885+
request.path_parameters["{field_name}"].clone()
886+
).
887+
deserialize().
888+
map_err(
889+
|_|Err(rupring::anyhow::anyhow!("{field_name} is invalid"))
890+
)?,
891+
"#
892+
)
893+
.as_str();
894+
}
895+
896+
request_bind_code += format!("}};").as_str();
897+
898+
request_bind_code += "Ok(bound)";
899+
request_bind_code += "}";
900+
901+
request_bind_code += format!(r#"}}"#).as_str();
902+
903+
code += request_bind_code.as_str();
904+
856905
return TokenStream::from_str(code.as_str()).unwrap();
857906
}

0 commit comments

Comments
 (0)