improve santization

This commit is contained in:
Adam H. Leventhal 2022-01-16 09:09:30 -08:00
parent aff25f2292
commit f1f9e2e938
3 changed files with 47 additions and 25 deletions

9
Cargo.lock generated
View File

@ -715,8 +715,10 @@ dependencies = [
"schemars", "schemars",
"serde", "serde",
"serde_json", "serde_json",
"syn",
"thiserror", "thiserror",
"typify", "typify",
"unicode-xid",
] ]
[[package]] [[package]]
@ -1154,7 +1156,7 @@ checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
[[package]] [[package]]
name = "typify" name = "typify"
version = "0.0.6-dev" version = "0.0.6-dev"
source = "git+https://github.com/oxidecomputer/typify#a45d3058ec748c7040988700a003e4ef9aac6f02" source = "git+https://github.com/oxidecomputer/typify#9afa917671b29fc231bc9ce304e041bdd685af09"
dependencies = [ dependencies = [
"typify-impl", "typify-impl",
"typify-macro", "typify-macro",
@ -1163,7 +1165,7 @@ dependencies = [
[[package]] [[package]]
name = "typify-impl" name = "typify-impl"
version = "0.0.6-dev" version = "0.0.6-dev"
source = "git+https://github.com/oxidecomputer/typify#a45d3058ec748c7040988700a003e4ef9aac6f02" source = "git+https://github.com/oxidecomputer/typify#9afa917671b29fc231bc9ce304e041bdd685af09"
dependencies = [ dependencies = [
"convert_case", "convert_case",
"log", "log",
@ -1174,12 +1176,13 @@ dependencies = [
"serde_json", "serde_json",
"syn", "syn",
"thiserror", "thiserror",
"unicode-xid",
] ]
[[package]] [[package]]
name = "typify-macro" name = "typify-macro"
version = "0.0.6-dev" version = "0.0.6-dev"
source = "git+https://github.com/oxidecomputer/typify#a45d3058ec748c7040988700a003e4ef9aac6f02" source = "git+https://github.com/oxidecomputer/typify#9afa917671b29fc231bc9ce304e041bdd685af09"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View File

@ -19,8 +19,10 @@ rustfmt-wrapper = "0.1"
schemars = { version = "0.8", features = ["chrono", "uuid"] } schemars = { version = "0.8", features = ["chrono", "uuid"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
syn = { version = "1.0", features = ["parsing"] }
thiserror = "1.0" thiserror = "1.0"
typify = { git = "https://github.com/oxidecomputer/typify" } typify = { git = "https://github.com/oxidecomputer/typify" }
unicode-xid = "0.2"
[dev-dependencies] [dev-dependencies]
expectorate = "1.0" expectorate = "1.0"

View File

@ -14,6 +14,7 @@ use quote::{format_ident, quote, ToTokens};
use template::PathTemplate; use template::PathTemplate;
use thiserror::Error; use thiserror::Error;
use typify::{TypeId, TypeSpace}; use typify::{TypeId, TypeSpace};
use unicode_xid::UnicodeXID;
use crate::to_schema::ToSchema; use crate::to_schema::ToSchema;
@ -275,10 +276,10 @@ impl Generator {
let nam = parameter_data.name.clone(); let nam = parameter_data.name.clone();
let schema = parameter_data.schema()?.to_schema(); let schema = parameter_data.schema()?.to_schema();
let name = format!(
"{}{}", let name = sanitize(
sanitize(operation_id, Case::Pascal), &format!("{}-{}", operation_id, nam),
sanitize(&nam, Case::Pascal), Case::Pascal,
); );
let typ = self let typ = self
.type_space .type_space
@ -302,13 +303,13 @@ impl Generator {
let nam = parameter_data.name.clone(); let nam = parameter_data.name.clone();
let mut schema = parameter_data.schema()?.to_schema(); let mut schema = parameter_data.schema()?.to_schema();
let name = format!( let name = sanitize(
"{}{}", &format!(
sanitize( "{}-{}",
operation.operation_id.as_ref().unwrap(), operation.operation_id.as_ref().unwrap(),
Case::Pascal nam
), ),
sanitize(&nam, Case::Pascal), Case::Pascal,
); );
if !parameter_data.required { if !parameter_data.required {
@ -344,12 +345,12 @@ impl Generator {
if let Some(s) = &mt.schema { if let Some(s) = &mt.schema {
let schema = s.to_schema(); let schema = s.to_schema();
let name = format!( let name = sanitize(
"{}Body", &format!(
sanitize( "{}-body",
operation.operation_id.as_ref().unwrap(), operation.operation_id.as_ref().unwrap(),
Case::Pascal ),
) Case::Pascal,
); );
let typ = self let typ = self
.type_space .type_space
@ -449,12 +450,12 @@ impl Generator {
let typ = if let Some(schema) = &mt.schema { let typ = if let Some(schema) = &mt.schema {
let schema = schema.to_schema(); let schema = schema.to_schema();
let name = format!( let name = sanitize(
"{}Response", &format!(
sanitize( "{}-response",
operation.operation_id.as_ref().unwrap(), operation.operation_id.as_ref().unwrap(),
Case::Pascal ),
) Case::Pascal,
); );
self.type_space self.type_space
.add_type_with_name(&schema, Some(name))? .add_type_with_name(&schema, Some(name))?
@ -1244,5 +1245,21 @@ impl ComponentLookup for Schema {
} }
fn sanitize(input: &str, case: Case) -> String { fn sanitize(input: &str, case: Case) -> String {
input.replace('/', "-").to_case(case) let out = input
.replace("'", "")
.replace(|c: char| !c.is_xid_continue(), "-")
.to_case(case);
let out = match out.chars().next() {
None => "x".to_case(case),
Some(c) if c.is_xid_start() => out,
Some(_) => format!("_{}", out),
};
// Make sure the string is a valid Rust identifier.
if syn::parse_str::<syn::Ident>(&out).is_ok() {
out
} else {
format!("{}_", out)
}
} }