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

Generate unique internal var names and add tests for name collisions. #601

Merged
merged 5 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 15 additions & 11 deletions progenitor-impl/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use typify::{TypeId, TypeSpace};

use crate::{
template::PathTemplate,
util::{items, parameter_map, sanitize, Case},
util::{items, parameter_map, sanitize, unique_ident_from, Case},
Error, Generator, Result, TagStyle,
};
use crate::{to_schema::ToSchema, util::ReferenceOrExt};
Expand Down Expand Up @@ -792,15 +792,18 @@ 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_ident = format_ident!("{internal_prefix}_url");
let query_ident = format_ident!("{internal_prefix}_query");
let request_ident = format_ident!("{internal_prefix}_request");
let response_ident = format_ident!("{internal_prefix}_response");
let result_ident = format_ident!("{internal_prefix}_result");
let param_names = method
.params
.iter()
.map(|param| format_ident!("{}", param.name))
.collect::<Vec<_>>();

// Generate a unique Ident for internal variables
let url_ident = unique_ident_from("url", &param_names);
let query_ident = unique_ident_from("query", &param_names);
let request_ident = unique_ident_from("request", &param_names);
let response_ident = unique_ident_from("response", &param_names);
let result_ident = unique_ident_from("result", &param_names);

// Generate code for query parameters.
let query_items = method
Expand Down Expand Up @@ -1429,7 +1432,6 @@ impl Generator {
) -> Result<TokenStream> {
let struct_name = sanitize(&method.operation_id, Case::Pascal);
let struct_ident = format_ident!("{}", struct_name);
let client_ident = format_ident!("__progenitor_client");

// Generate an ident for each parameter.
let param_names = method
Expand All @@ -1438,6 +1440,8 @@ impl Generator {
.map(|param| format_ident!("{}", param.name))
.collect::<Vec<_>>();

let client_ident = unique_ident_from("client", &param_names);

let mut cloneable = true;

// Generate the type for each parameter.
Expand Down
18 changes: 18 additions & 0 deletions progenitor-impl/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,21 @@ pub(crate) fn sanitize(input: &str, case: Case) -> String {
format!("{}_", out)
}
}

/// Given a desired name and a slice of proc_macro2::Ident, generate a new
/// Ident that is unique from the slice.
pub(crate) fn unique_ident_from(
name: &str,
identities: &[proc_macro2::Ident],
) -> proc_macro2::Ident {
let mut name = name.to_string();

loop {
name.insert_str(0, "_");
let ident = quote::format_ident!("{}", name);

if !identities.contains(&ident) {
return ident;
}
}
inickles marked this conversation as resolved.
Show resolved Hide resolved
}
Loading