Skip to content

Commit 4b54435

Browse files
authored
work around batch svc enum spec errors (#452)
1 parent afbc967 commit 4b54435

File tree

15 files changed

+2211
-574
lines changed

15 files changed

+2211
-574
lines changed

services/autorust/codegen/examples/gen_svc.rs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,7 @@ const INVALID_TYPE_WORKAROUND: &[(&str, &str, &str)] = &[(
3737
"rows",
3838
)];
3939

40-
const FIX_CASE_PROPERTIES: &[(&str, &str, &str)] = &[
41-
(
42-
"../../../azure-rest-api-specs/specification/batch/data-plane/Microsoft.Batch/stable/2021-06-01.14.0/BatchService.json",
43-
"TaskSchedulingPolicy",
44-
"nodeFillType",
45-
),
46-
(
47-
"../../../azure-rest-api-specs/specification/batch/data-plane/Microsoft.Batch/stable/2021-06-01.14.0/BatchService.json",
48-
"NodePlacementConfiguration",
49-
"policy",
50-
),
51-
(
52-
"../../../azure-rest-api-specs/specification/batch/data-plane/Microsoft.Batch/stable/2021-06-01.14.0/BatchService.json",
53-
"PublicIPAddressConfiguration",
54-
"provision",
55-
),
56-
];
40+
const FIX_CASE_PROPERTIES: &[&str] = &["BatchServiceClient", "BatchService"];
5741

5842
// because of recursive types, some properties have to be boxed
5943
// https://github.com/ctaggart/autorust/issues/73
@@ -179,12 +163,8 @@ fn gen_crate(spec: &SpecReadme) -> Result<()> {
179163
let mut feature_mod_names = Vec::new();
180164

181165
let mut fix_case_properties = HashSet::new();
182-
for (file_path, schema_name, property_name) in FIX_CASE_PROPERTIES {
183-
fix_case_properties.insert(PropertyName {
184-
file_path: PathBuf::from(file_path),
185-
schema_name: schema_name.to_string(),
186-
property_name: property_name.to_string(),
187-
});
166+
for spec_title in FIX_CASE_PROPERTIES {
167+
fix_case_properties.insert(spec_title.to_string());
188168
}
189169

190170
let mut box_properties = HashSet::new();

services/autorust/codegen/src/codegen.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ impl CodeGen {
2828
&self.config.output_folder
2929
}
3030

31-
pub fn has_case_workaround(&self, path: &Path) -> bool {
32-
self.config.fix_case_properties.iter().any(|x| x.file_path == path)
33-
}
34-
35-
pub fn should_workaround_case(&self, prop_nm: &PropertyName) -> bool {
36-
self.config.fix_case_properties.contains(prop_nm)
31+
pub fn should_workaround_case(&self) -> bool {
32+
if let Some(title) = self.spec.title() {
33+
self.config.fix_case_properties.contains(title)
34+
} else {
35+
false
36+
}
3737
}
3838

3939
pub fn should_force_optional(&self, prop_nm: &PropertyName) -> bool {

services/autorust/codegen/src/codegen_models.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn create_models(cg: &CodeGen) -> Result<TokenStream, Error> {
2121
let mut file = TokenStream::new();
2222
file.extend(create_generated_by_header());
2323

24-
let has_case_workaround = cg.spec.input_docs().any(|(x, _)| cg.has_case_workaround(x));
24+
let has_case_workaround = cg.should_workaround_case();
2525

2626
file.extend(quote! {
2727
#![allow(non_camel_case_types)]
@@ -201,7 +201,7 @@ fn create_struct(cg: &CodeGen, doc_file: &Path, struct_name: &str, schema: &Reso
201201
property_name: property_name.to_string(),
202202
};
203203

204-
let lowercase_workaround = cg.should_workaround_case(prop_nm);
204+
let lowercase_workaround = cg.should_workaround_case();
205205

206206
let (mut field_tp_name, field_tp) = create_struct_field_type(cg, doc_file, &ns, property_name, property, lowercase_workaround)?;
207207
// uncomment the next two lines to help identify entries that need boxed

services/autorust/codegen/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub struct Config {
6666
pub output_folder: PathBuf,
6767
pub box_properties: HashSet<PropertyName>,
6868
pub optional_properties: HashSet<PropertyName>,
69-
pub fix_case_properties: HashSet<PropertyName>,
69+
pub fix_case_properties: HashSet<String>,
7070
pub invalid_types: HashSet<PropertyName>,
7171
pub runs: Vec<Runs>,
7272
pub print_writing_file: bool,

services/autorust/codegen/src/spec.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ impl Spec {
8181
&self.docs
8282
}
8383

84+
pub fn title(&self) -> Option<&str> {
85+
let mut titles: Vec<_> = self
86+
.docs
87+
.values()
88+
.map(|doc| &doc.info.title)
89+
.filter(|t| t.is_some())
90+
.flatten()
91+
.collect();
92+
titles.sort_unstable();
93+
94+
titles.get(0).map(|t| t.as_str())
95+
}
96+
8497
pub fn consumes(&self) -> Vec<&String> {
8598
let versions: Vec<_> = self
8699
.docs()

0 commit comments

Comments
 (0)