#[must_use]
pub fn go_method_name(schema_name: &str) -> String {
let mut chars = schema_name.chars();
match chars.next() {
Some(c) => {
let head: String = c.to_uppercase().collect();
head + chars.as_str()
}
None => String::new(),
}
}
#[must_use]
pub fn go_request_type_name(schema_name: &str) -> String {
format!("Api{}Request", go_method_name(schema_name))
}
#[must_use]
pub fn go_executor_name(schema_name: &str) -> String {
format!("{}Execute", go_method_name(schema_name))
}
#[must_use]
pub fn go_body_type_name(schema_name: &str) -> String {
go_method_name(schema_name)
}
#[must_use]
pub fn go_output_type_name(schema_name: &str, response_schema: Option<&str>) -> String {
response_schema.map_or_else(
|| format!("{}Output", go_method_name(schema_name)),
|r| go_method_name(r),
)
}
#[cfg(test)]
mod tests {
use super::*;
fn akeyless_targets() -> Vec<(
&'static str,
&'static str,
&'static str,
&'static str,
Option<&'static str>,
&'static str,
)> {
vec![
(
"authMethodCreateApiKey",
"AuthMethodCreateApiKey",
"getAuthMethod",
"GetAuthMethod",
Some("AuthMethod"),
"AuthMethod",
),
(
"createRole",
"CreateRole",
"getRole",
"GetRole",
None,
"GetRoleOutput",
),
(
"setRoleRule",
"SetRoleRule",
"getRole",
"GetRole",
None,
"GetRoleOutput",
),
(
"createSecret",
"CreateSecret",
"getSecretValue",
"GetSecretValue",
None,
"GetSecretValueOutput",
),
(
"gatewayCreateK8SAuthConfig",
"GatewayCreateK8SAuthConfig",
"gatewayGetK8SAuthConfig",
"GatewayGetK8SAuthConfig",
Some("GatewayGetK8SAuthConfigOutput"),
"GatewayGetK8SAuthConfigOutput",
),
]
}
#[test]
fn five_representative_resources_round_trip() {
for (create, exp_create, read, exp_read, response, exp_output) in akeyless_targets() {
assert_eq!(
go_method_name(create),
exp_create,
"create method for {create}"
);
assert_eq!(go_method_name(read), exp_read, "read method for {read}");
assert_eq!(
go_output_type_name(read, response),
exp_output,
"output type for {read}"
);
}
}
#[test]
fn request_builder_naming_for_targets() {
for (create, exp_create, _, _, _, _) in akeyless_targets() {
assert_eq!(
go_request_type_name(create),
format!("Api{exp_create}Request"),
"request type for {create}",
);
}
}
#[test]
fn executor_naming_for_targets() {
for (create, exp_create, _, _, _, _) in akeyless_targets() {
assert_eq!(
go_executor_name(create),
format!("{exp_create}Execute"),
"executor for {create}",
);
}
}
#[test]
fn empty_input_returns_empty() {
assert_eq!(go_method_name(""), "");
assert_eq!(go_request_type_name(""), "ApiRequest");
assert_eq!(go_executor_name(""), "Execute");
}
#[test]
fn already_pascal_case_is_idempotent() {
assert_eq!(go_method_name("CreateRole"), "CreateRole");
assert_eq!(
go_method_name("AuthMethodCreateApiKey"),
"AuthMethodCreateApiKey"
);
}
#[test]
fn unicode_first_char() {
assert_eq!(go_method_name("ñame"), "Ñame");
}
}