improve santization
This commit is contained in:
parent
aff25f2292
commit
f1f9e2e938
|
@ -715,8 +715,10 @@ dependencies = [
|
|||
"schemars",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"syn",
|
||||
"thiserror",
|
||||
"typify",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1154,7 +1156,7 @@ checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
|
|||
[[package]]
|
||||
name = "typify"
|
||||
version = "0.0.6-dev"
|
||||
source = "git+https://github.com/oxidecomputer/typify#a45d3058ec748c7040988700a003e4ef9aac6f02"
|
||||
source = "git+https://github.com/oxidecomputer/typify#9afa917671b29fc231bc9ce304e041bdd685af09"
|
||||
dependencies = [
|
||||
"typify-impl",
|
||||
"typify-macro",
|
||||
|
@ -1163,7 +1165,7 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "typify-impl"
|
||||
version = "0.0.6-dev"
|
||||
source = "git+https://github.com/oxidecomputer/typify#a45d3058ec748c7040988700a003e4ef9aac6f02"
|
||||
source = "git+https://github.com/oxidecomputer/typify#9afa917671b29fc231bc9ce304e041bdd685af09"
|
||||
dependencies = [
|
||||
"convert_case",
|
||||
"log",
|
||||
|
@ -1174,12 +1176,13 @@ dependencies = [
|
|||
"serde_json",
|
||||
"syn",
|
||||
"thiserror",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typify-macro"
|
||||
version = "0.0.6-dev"
|
||||
source = "git+https://github.com/oxidecomputer/typify#a45d3058ec748c7040988700a003e4ef9aac6f02"
|
||||
source = "git+https://github.com/oxidecomputer/typify#9afa917671b29fc231bc9ce304e041bdd685af09"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
|
|
@ -16,11 +16,13 @@ proc-macro2 = "1.0"
|
|||
quote = "1.0"
|
||||
regex = "1.5"
|
||||
rustfmt-wrapper = "0.1"
|
||||
schemars = { version = "0.8", features = [ "chrono", "uuid" ] }
|
||||
serde = { version = "1.0", features = [ "derive" ] }
|
||||
schemars = { version = "0.8", features = ["chrono", "uuid"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
syn = { version = "1.0", features = ["parsing"] }
|
||||
thiserror = "1.0"
|
||||
typify = { git = "https://github.com/oxidecomputer/typify" }
|
||||
unicode-xid = "0.2"
|
||||
|
||||
[dev-dependencies]
|
||||
expectorate = "1.0"
|
||||
|
|
|
@ -14,6 +14,7 @@ use quote::{format_ident, quote, ToTokens};
|
|||
use template::PathTemplate;
|
||||
use thiserror::Error;
|
||||
use typify::{TypeId, TypeSpace};
|
||||
use unicode_xid::UnicodeXID;
|
||||
|
||||
use crate::to_schema::ToSchema;
|
||||
|
||||
|
@ -275,10 +276,10 @@ impl Generator {
|
|||
|
||||
let nam = parameter_data.name.clone();
|
||||
let schema = parameter_data.schema()?.to_schema();
|
||||
let name = format!(
|
||||
"{}{}",
|
||||
sanitize(operation_id, Case::Pascal),
|
||||
sanitize(&nam, Case::Pascal),
|
||||
|
||||
let name = sanitize(
|
||||
&format!("{}-{}", operation_id, nam),
|
||||
Case::Pascal,
|
||||
);
|
||||
let typ = self
|
||||
.type_space
|
||||
|
@ -302,13 +303,13 @@ impl Generator {
|
|||
|
||||
let nam = parameter_data.name.clone();
|
||||
let mut schema = parameter_data.schema()?.to_schema();
|
||||
let name = format!(
|
||||
"{}{}",
|
||||
sanitize(
|
||||
let name = sanitize(
|
||||
&format!(
|
||||
"{}-{}",
|
||||
operation.operation_id.as_ref().unwrap(),
|
||||
Case::Pascal
|
||||
nam
|
||||
),
|
||||
sanitize(&nam, Case::Pascal),
|
||||
Case::Pascal,
|
||||
);
|
||||
|
||||
if !parameter_data.required {
|
||||
|
@ -344,12 +345,12 @@ impl Generator {
|
|||
|
||||
if let Some(s) = &mt.schema {
|
||||
let schema = s.to_schema();
|
||||
let name = format!(
|
||||
"{}Body",
|
||||
sanitize(
|
||||
let name = sanitize(
|
||||
&format!(
|
||||
"{}-body",
|
||||
operation.operation_id.as_ref().unwrap(),
|
||||
Case::Pascal
|
||||
)
|
||||
),
|
||||
Case::Pascal,
|
||||
);
|
||||
let typ = self
|
||||
.type_space
|
||||
|
@ -449,12 +450,12 @@ impl Generator {
|
|||
|
||||
let typ = if let Some(schema) = &mt.schema {
|
||||
let schema = schema.to_schema();
|
||||
let name = format!(
|
||||
"{}Response",
|
||||
sanitize(
|
||||
let name = sanitize(
|
||||
&format!(
|
||||
"{}-response",
|
||||
operation.operation_id.as_ref().unwrap(),
|
||||
Case::Pascal
|
||||
)
|
||||
),
|
||||
Case::Pascal,
|
||||
);
|
||||
self.type_space
|
||||
.add_type_with_name(&schema, Some(name))?
|
||||
|
@ -1244,5 +1245,21 @@ impl ComponentLookup for Schema {
|
|||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue