Skip to content

Commit 6d968b2

Browse files
cataggarctaggart
andauthored
add operation summary doc comments (#530)
* add operation summary doc comments * generate Co-authored-by: Cameron Taggart <cameron.taggart@gmail.com>
1 parent abee6e5 commit 6d968b2

File tree

506 files changed

+21729
-252
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

506 files changed

+21729
-252
lines changed

services/autorust/codegen/src/codegen.rs

Lines changed: 41 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
use crate::{identifier::ident, spec, Config, PropertyName, ResolvedSchema, Spec};
2-
use autorust_openapi::{DataType, ReferenceOr, Schema, SchemaCommon};
1+
use crate::{
2+
identifier::ident,
3+
spec::{self, TypeName},
4+
Config, PropertyName, ResolvedSchema, Spec,
5+
};
6+
use autorust_openapi::DataType;
37
use heck::CamelCase;
48
use once_cell::sync::Lazy;
59
use proc_macro2::TokenStream;
@@ -63,10 +67,6 @@ impl CodeGen {
6367
pub enum Error {
6468
#[error("SpecError: {0}")]
6569
Spec(#[from] spec::Error),
66-
#[error("ArrayExpectedToHaveItems")]
67-
ArrayExpectedToHaveItems,
68-
#[error("NoNameForRef")]
69-
NoNameForRef,
7070
#[error("creating function name: {0}")]
7171
FunctionName(#[source] crate::identifier::Error),
7272
#[error("creating type name for schema ref: {0}")]
@@ -123,10 +123,6 @@ pub fn is_vec(ts: &TokenStream) -> bool {
123123
ts.to_string().starts_with("Vec <")
124124
}
125125

126-
pub fn get_schema_array_items(schema: &SchemaCommon) -> Result<&ReferenceOr<Schema>, Error> {
127-
schema.items.as_ref().as_ref().ok_or(Error::ArrayExpectedToHaveItems)
128-
}
129-
130126
/// A header placed at the top the file to say that it is generated by AutoRust.
131127
pub fn create_generated_by_header() -> TokenStream {
132128
let version = env!("CARGO_PKG_VERSION");
@@ -168,106 +164,44 @@ pub fn enum_values_as_strings(values: &[Value]) -> Vec<&str> {
168164
.collect()
169165
}
170166

171-
pub enum TypeName {
172-
Reference(String),
173-
Array(Box<TypeName>),
174-
Value,
175-
Bytes,
176-
Int32,
177-
Int64,
178-
Float32,
179-
Float64,
180-
Boolean,
181-
String,
182-
}
183-
184-
impl TypeName {
185-
pub fn to_token_stream(&self, as_ref: bool, qualify_models: bool) -> Result<TokenStream, Error> {
186-
Ok(match self {
187-
TypeName::Reference(name) => {
188-
let idt = ident(&name.to_camel_case()).map_err(Error::TypeNameForSchemaRef)?;
189-
let idt = if qualify_models {
190-
quote! { models::#idt }
191-
} else {
192-
idt
193-
};
194-
match as_ref {
195-
true => quote! { impl Into<#idt> },
196-
false => idt,
197-
}
198-
}
199-
TypeName::Array(vec_items_typ) => {
200-
let vec_items_typ = vec_items_typ.to_token_stream(false, qualify_models)?;
201-
match as_ref {
202-
true => quote! { impl Into<Vec<#vec_items_typ>> },
203-
false => quote! { Vec<#vec_items_typ> },
204-
}
167+
pub fn type_name_gen(type_name: &TypeName, as_ref: bool, qualify_models: bool) -> Result<TokenStream, Error> {
168+
Ok(match type_name {
169+
TypeName::Reference(name) => {
170+
let idt = ident(&name.to_camel_case()).map_err(Error::TypeNameForSchemaRef)?;
171+
let idt = if qualify_models {
172+
quote! { models::#idt }
173+
} else {
174+
idt
175+
};
176+
match as_ref {
177+
true => quote! { impl Into<#idt> },
178+
false => idt,
205179
}
206-
TypeName::Value => match as_ref {
207-
true => quote! { impl Into<serde_json::Value> },
208-
false => quote! { serde_json::Value },
209-
},
210-
TypeName::Bytes => match as_ref {
211-
true => quote! { impl Into<bytes::Bytes> },
212-
false => quote! { bytes::Bytes },
213-
},
214-
TypeName::Int32 => quote! { i32 },
215-
TypeName::Int64 => quote! { i64 },
216-
TypeName::Float32 => quote! { f32 },
217-
TypeName::Float64 => quote! { f64 },
218-
TypeName::Boolean => quote! { bool },
219-
TypeName::String => match as_ref {
220-
true => quote! { impl Into<String> },
221-
false => quote! { String },
222-
},
223-
})
224-
}
225-
}
226-
227-
pub fn get_type_name_for_schema(schema: &SchemaCommon) -> Result<TypeName, Error> {
228-
Ok(if let Some(schema_type) = &schema.type_ {
229-
let format = schema.format.as_deref();
230-
match schema_type {
231-
DataType::Array => {
232-
let items = get_schema_array_items(schema)?;
233-
let vec_items_typ = get_type_name_for_schema_ref(items)?;
234-
TypeName::Array(Box::new(vec_items_typ))
235-
}
236-
DataType::Integer => {
237-
if format == Some("int32") {
238-
TypeName::Int32
239-
} else {
240-
TypeName::Int64
241-
}
242-
}
243-
DataType::Number => {
244-
if format == Some("float") {
245-
TypeName::Float32
246-
} else {
247-
TypeName::Float64
248-
}
249-
}
250-
DataType::String => TypeName::String,
251-
DataType::Boolean => TypeName::Boolean,
252-
DataType::Object => TypeName::Value,
253-
DataType::File => TypeName::Bytes,
254180
}
255-
} else {
256-
// eprintln!(
257-
// "WARN unknown type in get_type_name_for_schema, description {:?}",
258-
// schema.description
259-
// );
260-
TypeName::Value
261-
})
262-
}
263-
264-
pub fn get_type_name_for_schema_ref(schema: &ReferenceOr<Schema>) -> Result<TypeName, Error> {
265-
Ok(match schema {
266-
ReferenceOr::Reference { reference, .. } => {
267-
let name = reference.name.as_ref().ok_or(Error::NoNameForRef)?;
268-
TypeName::Reference(name.to_owned())
181+
TypeName::Array(vec_items_typ) => {
182+
let vec_items_typ = type_name_gen(vec_items_typ, false, qualify_models)?;
183+
match as_ref {
184+
true => quote! { impl Into<Vec<#vec_items_typ>> },
185+
false => quote! { Vec<#vec_items_typ> },
186+
}
269187
}
270-
ReferenceOr::Item(schema) => get_type_name_for_schema(&schema.common)?,
188+
TypeName::Value => match as_ref {
189+
true => quote! { impl Into<serde_json::Value> },
190+
false => quote! { serde_json::Value },
191+
},
192+
TypeName::Bytes => match as_ref {
193+
true => quote! { impl Into<bytes::Bytes> },
194+
false => quote! { bytes::Bytes },
195+
},
196+
TypeName::Int32 => quote! { i32 },
197+
TypeName::Int64 => quote! { i64 },
198+
TypeName::Float32 => quote! { f32 },
199+
TypeName::Float64 => quote! { f64 },
200+
TypeName::Boolean => quote! { bool },
201+
TypeName::String => match as_ref {
202+
true => quote! { impl Into<String> },
203+
false => quote! { String },
204+
},
271205
})
272206
}
273207

services/autorust/codegen/src/codegen_models.rs

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::{
22
codegen::{
3-
add_option, create_generated_by_header, enum_values_as_strings, get_schema_array_items, get_type_name_for_schema,
4-
get_type_name_for_schema_ref, is_basic_type, is_local_enum, is_local_struct, is_vec, Error,
3+
add_option, create_generated_by_header, enum_values_as_strings, is_basic_type, is_local_enum, is_local_struct, is_vec,
4+
type_name_gen, Error,
55
},
66
identifier::{CamelCaseIdent, SnakeCaseIdent},
7-
spec, CodeGen, PropertyName, ResolvedSchema,
7+
spec::{self, get_schema_array_items, get_type_name_for_schema, get_type_name_for_schema_ref},
8+
CodeGen, PropertyName, ResolvedSchema, Spec,
89
};
910
use autorust_openapi::{DataType, Reference, SchemaCommon};
1011
use indexmap::IndexMap;
@@ -20,28 +21,12 @@ fn is_array(schema: &SchemaCommon) -> bool {
2021
matches!(schema.type_, Some(DataType::Array))
2122
}
2223

23-
pub fn create_models(cg: &CodeGen) -> Result<TokenStream, Error> {
24-
let mut file = TokenStream::new();
25-
file.extend(create_generated_by_header());
26-
27-
let has_case_workaround = cg.should_workaround_case();
28-
29-
file.extend(quote! {
30-
#![allow(non_camel_case_types)]
31-
#![allow(unused_imports)]
32-
use serde::{Deserialize, Serialize};
33-
});
34-
if has_case_workaround {
35-
file.extend(quote! {
36-
use azure_core::util::case_insensitive_deserialize;
37-
});
38-
}
39-
24+
fn all_schemas(spec: &Spec) -> Result<IndexMap<RefKey, ResolvedSchema>, Error> {
4025
let mut all_schemas: IndexMap<RefKey, ResolvedSchema> = IndexMap::new();
4126

4227
// all definitions from input_files
43-
for (doc_file, doc) in cg.spec.input_docs() {
44-
let schemas = cg.spec.resolve_schema_map(doc_file, &doc.definitions).map_err(Error::Spec)?;
28+
for (doc_file, doc) in spec.input_docs() {
29+
let schemas = spec.resolve_schema_map(doc_file, &doc.definitions).map_err(Error::Spec)?;
4530
for (name, schema) in schemas {
4631
all_schemas.insert(
4732
RefKey {
@@ -54,14 +39,34 @@ pub fn create_models(cg: &CodeGen) -> Result<TokenStream, Error> {
5439
}
5540

5641
// any referenced schemas from other files
57-
for (doc_file, api) in cg.spec.input_docs() {
42+
for (doc_file, api) in spec.input_docs() {
5843
for reference in openapi::get_api_schema_references(doc_file, api) {
59-
add_schema_refs(cg, &mut all_schemas, doc_file, reference)?;
44+
add_schema_refs(spec, &mut all_schemas, doc_file, reference)?;
6045
}
6146
}
6247

48+
Ok(all_schemas)
49+
}
50+
51+
pub fn create_models(cg: &CodeGen) -> Result<TokenStream, Error> {
52+
let mut file = TokenStream::new();
53+
file.extend(create_generated_by_header());
54+
55+
let has_case_workaround = cg.should_workaround_case();
56+
57+
file.extend(quote! {
58+
#![allow(non_camel_case_types)]
59+
#![allow(unused_imports)]
60+
use serde::{Deserialize, Serialize};
61+
});
62+
if has_case_workaround {
63+
file.extend(quote! {
64+
use azure_core::util::case_insensitive_deserialize;
65+
});
66+
}
67+
6368
let mut schema_names = IndexMap::new();
64-
for (ref_key, schema) in &all_schemas {
69+
for (ref_key, schema) in &all_schemas(&cg.spec)? {
6570
let doc_file = &ref_key.file_path;
6671
let schema_name = &ref_key.name;
6772
if let Some(_first_doc_file) = schema_names.insert(schema_name, doc_file) {
@@ -89,24 +94,24 @@ pub fn create_models(cg: &CodeGen) -> Result<TokenStream, Error> {
8994

9095
fn create_basic_type_alias(property_name: &str, property: &ResolvedSchema) -> Result<(TokenStream, TokenStream), Error> {
9196
let id = property_name.to_camel_case_ident().map_err(Error::StructName)?;
92-
let value = get_type_name_for_schema(&property.schema.common)?.to_token_stream(false, false)?;
97+
let value = type_name_gen(&get_type_name_for_schema(&property.schema.common)?, false, false)?;
9398
Ok((id, value))
9499
}
95100

96101
// For create_models. Recursively adds schema refs.
97102
fn add_schema_refs(
98-
cg: &CodeGen,
103+
spec: &Spec,
99104
schemas: &mut IndexMap<RefKey, ResolvedSchema>,
100105
doc_file: &Path,
101106
schema_ref: Reference,
102107
) -> Result<(), Error> {
103-
let schema = cg.spec.resolve_schema_ref(doc_file, schema_ref)?;
108+
let schema = spec.resolve_schema_ref(doc_file, schema_ref)?;
104109
if let Some(ref_key) = schema.ref_key.clone() {
105-
if !schemas.contains_key(&ref_key) && !cg.spec.is_input_file(&ref_key.file_path) {
110+
if !schemas.contains_key(&ref_key) && !spec.is_input_file(&ref_key.file_path) {
106111
let refs = get_schema_schema_references(&schema.schema);
107112
schemas.insert(ref_key.clone(), schema);
108113
for reference in refs {
109-
add_schema_refs(cg, schemas, &ref_key.file_path, reference)?;
114+
add_schema_refs(spec, schemas, &ref_key.file_path, reference)?;
110115
}
111116
}
112117
}
@@ -161,7 +166,7 @@ fn create_enum(
161166
fn create_vec_alias(alias_name: &str, schema: &ResolvedSchema) -> Result<TokenStream, Error> {
162167
let items = get_schema_array_items(&schema.schema.common)?;
163168
let typ = &alias_name.to_camel_case_ident().map_err(Error::VecAliasName)?;
164-
let items_typ = get_type_name_for_schema_ref(items)?.to_token_stream(false, false)?;
169+
let items_typ = type_name_gen(&get_type_name_for_schema_ref(items)?, false, false)?;
165170
Ok(quote! { pub type #typ = Vec<#items_typ>; })
166171
}
167172

@@ -175,7 +180,7 @@ fn create_struct(cg: &CodeGen, doc_file: &Path, struct_name: &str, schema: &Reso
175180
let required: HashSet<&str> = schema.schema.required.iter().map(String::as_str).collect();
176181

177182
for schema in &schema.schema.all_of {
178-
let type_name = get_type_name_for_schema_ref(schema)?.to_token_stream(false, false)?;
183+
let type_name = type_name_gen(&get_type_name_for_schema_ref(schema)?, false, false)?;
179184
let field_name = type_name.to_string().to_snake_case_ident().map_err(Error::StructFieldName)?;
180185
props.extend(quote! {
181186
#[serde(flatten)]
@@ -289,7 +294,7 @@ fn create_struct_field_type(
289294
Ok((tp_name, tps))
290295
} else {
291296
Ok((
292-
get_type_name_for_schema(&property.schema.common)?.to_token_stream(false, false)?,
297+
type_name_gen(&get_type_name_for_schema(&property.schema.common)?, false, false)?,
293298
Vec::new(),
294299
))
295300
}

0 commit comments

Comments
 (0)