Skip to content

Commit

Permalink
Prefix internal variable names to help avoid collisions (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
inickles authored Oct 12, 2023
1 parent f367c43 commit 233cb80
Show file tree
Hide file tree
Showing 21 changed files with 7,577 additions and 7,504 deletions.
55 changes: 35 additions & 20 deletions progenitor-impl/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,16 @@ impl Generator {
method: &OperationMethod,
client: TokenStream,
) -> Result<MethodSigBody> {
// We prefix internal variable names to mitigate possible name
// collisions with the input spec. See:
// https://github.com/oxidecomputer/progenitor/issues/288
let internal_prefix = "__progenitor";
let url_var = format_ident!("{internal_prefix}_url");
let query_var = format_ident!("{internal_prefix}_query");
let request_var = format_ident!("{internal_prefix}_request");
let response_var = format_ident!("{internal_prefix}_response");
let result_var = format_ident!("{internal_prefix}_result");

// Generate code for query parameters.
let query_items = method
.params
Expand All @@ -802,12 +812,12 @@ impl Generator {
let qn_ident = format_ident!("{}", &param.name);
let res = if *required {
quote! {
query.push((#qn, #qn_ident .to_string()));
#query_var.push((#qn, #qn_ident .to_string()));
}
} else {
quote! {
if let Some(v) = & #qn_ident {
query.push((#qn, v.to_string()));
#query_var.push((#qn, v.to_string()));
}
}
};
Expand All @@ -823,11 +833,11 @@ impl Generator {
} else {
let size = query_items.len();
let query_build = quote! {
let mut query = Vec::with_capacity(#size);
let mut #query_var = Vec::with_capacity(#size);
#(#query_items)*
};
let query_use = quote! {
.query(&query)
.query(&#query_var)
};

(query_build, query_use)
Expand Down Expand Up @@ -903,6 +913,9 @@ impl Generator {
.collect();

let url_path = method.path.compile(url_renames, client.clone());
let url_path = quote! {
let #url_var = #url_path;
};

// Generate code to handle the body param.
let body_func = method.params.iter().filter_map(|param| {
Expand Down Expand Up @@ -963,22 +976,22 @@ impl Generator {
let decode = match &response.typ {
OperationResponseType::Type(_) => {
quote! {
ResponseValue::from_response(response).await
ResponseValue::from_response(#response_var).await
}
}
OperationResponseType::None => {
quote! {
Ok(ResponseValue::empty(response))
Ok(ResponseValue::empty(#response_var))
}
}
OperationResponseType::Raw => {
quote! {
Ok(ResponseValue::stream(response))
Ok(ResponseValue::stream(#response_var))
}
}
OperationResponseType::Upgrade => {
quote! {
ResponseValue::upgrade(response).await
ResponseValue::upgrade(#response_var).await
}
}
};
Expand Down Expand Up @@ -1013,22 +1026,22 @@ impl Generator {
OperationResponseType::Type(_) => {
quote! {
Err(Error::ErrorResponse(
ResponseValue::from_response(response)
ResponseValue::from_response(#response_var)
.await?
))
}
}
OperationResponseType::None => {
quote! {
Err(Error::ErrorResponse(
ResponseValue::empty(response)
ResponseValue::empty(#response_var)
))
}
}
OperationResponseType::Raw => {
quote! {
Err(Error::ErrorResponse(
ResponseValue::stream(response)
ResponseValue::stream(#response_var)
))
}
}
Expand Down Expand Up @@ -1073,17 +1086,19 @@ impl Generator {
// API description.
let default_response = match method.responses.iter().last() {
Some(response) if response.status_code.is_default() => quote! {},
_ => quote! { _ => Err(Error::UnexpectedResponse(response)), },
_ => {
quote! { _ => Err(Error::UnexpectedResponse(#response_var)), }
}
};

let pre_hook = self.settings.pre_hook.as_ref().map(|hook| {
quote! {
(#hook)(&#client.inner, &request);
(#hook)(&#client.inner, &#request_var);
}
});
let post_hook = self.settings.post_hook.as_ref().map(|hook| {
quote! {
(#hook)(&#client.inner, &result);
(#hook)(&#client.inner, &#result_var);
}
});

Expand All @@ -1095,8 +1110,8 @@ impl Generator {

#headers_build

let request = #client.client
. #method_func (url)
let #request_var = #client.client
. #method_func (#url_var)
#accept_header
#(#body_func)*
#query_use
Expand All @@ -1105,14 +1120,14 @@ impl Generator {
.build()?;

#pre_hook
let result = #client.client
.execute(request)
let #result_var = #client.client
.execute(#request_var)
.await;
#post_hook

let response = result?;
let #response_var = #result_var?;

match response.status().as_u16() {
match #response_var.status().as_u16() {
// These will be of the form...
// 201 => ResponseValue::from_response(response).await,
// 200..299 => ResponseValue::empty(response),
Expand Down
10 changes: 5 additions & 5 deletions progenitor-impl/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl PathTemplate {
});

quote! {
let url = format!(#fmt, #client.baseurl, #(#components,)*);
format!(#fmt, #client.baseurl, #(#components,)*)
}
}

Expand Down Expand Up @@ -306,10 +306,10 @@ mod tests {
let t = parse("/measure/{number}").unwrap();
let out = t.compile(rename, quote::quote! { self });
let want = quote::quote! {
let url = format!("{}/measure/{}",
format!("{}/measure/{}",
self.baseurl,
encode_path(&number.to_string()),
);
)
};
assert_eq!(want.to_string(), out.to_string());
}
Expand All @@ -326,12 +326,12 @@ mod tests {
let t = parse("/abc/def:{one}:jkl/{two}/a:{three}").unwrap();
let out = t.compile(rename, quote::quote! { self });
let want = quote::quote! {
let url = format!("{}/abc/def:{}:jkl/{}/a:{}",
format!("{}/abc/def:{}:jkl/{}/a:{}",
self.baseurl,
encode_path(&one.to_string()),
encode_path(&two.to_string()),
encode_path(&three.to_string()),
);
)
};
assert_eq!(want.to_string(), out.to_string());
}
Expand Down
Loading

0 comments on commit 233cb80

Please sign in to comment.