Skip to content
This repository has been archived by the owner on Apr 10, 2021. It is now read-only.

Commit

Permalink
Refactor spec file (#75)
Browse files Browse the repository at this point in the history
* Refactor spec file

* 197

Co-authored-by: Cameron Taggart <cameron.taggart@gmail.com>
  • Loading branch information
rylev and ctaggart authored Oct 29, 2020
1 parent 5a6d507 commit 99721a9
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 157 deletions.
38 changes: 19 additions & 19 deletions codegen/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,6 @@ impl CodeGen {
self.config.api_version.as_deref()
}

// For create_models. Recursively adds schema refs.
fn add_schema_refs(&self, schemas: &mut IndexMap<RefKey, ResolvedSchema>, doc_file: &Path, schema_ref: &str) -> Result<()> {
let schema = self.spec.resolve_schema_ref(doc_file, schema_ref).context(SpecError)?;
if let Some(ref_key) = schema.ref_key.clone() {
if !schemas.contains_key(&ref_key) {
if !self.spec.is_input_file(&ref_key.file) {
let refs = get_schema_schema_refs(&schema.schema);
schemas.insert(ref_key.clone(), schema);
for rf in refs {
self.add_schema_refs(schemas, &ref_key.file, &rf)?;
}
}
}
}
Ok(())
}

pub fn create_models(&self) -> Result<TokenStream> {
let mut file = TokenStream::new();
file.extend(create_generated_by_header());
Expand Down Expand Up @@ -104,7 +87,7 @@ impl CodeGen {
for (doc_file, doc) in &self.spec.docs {
if self.spec.is_input_file(doc_file) {
for rf in get_api_schema_refs(doc) {
self.add_schema_refs(&mut all_schemas, doc_file, &rf)?;
self.add_schema_refs(&mut all_schemas, doc_file, Reference::parse(&rf))?;
}
}
}
Expand Down Expand Up @@ -152,7 +135,7 @@ impl CodeGen {
let paths = self.spec.resolve_path_map(doc_file, &doc.paths).context(SpecError)?;
for (path, item) in &paths {
// println!("{}", path);
for op in spec::pathitem_operations(item) {
for op in spec::path_item_operations(item) {
// println!("{:?}", op.operation_id);
let (module_name, function_name) = op.function_name(path);
let function = create_function(self, doc_file, path, item, &op, &param_re, &function_name)?;
Expand Down Expand Up @@ -191,6 +174,23 @@ impl CodeGen {
Ok(file)
}

// For create_models. Recursively adds schema refs.
fn add_schema_refs(&self, schemas: &mut IndexMap<RefKey, ResolvedSchema>, doc_file: &Path, schema_ref: Reference) -> Result<()> {
let schema = self.spec.resolve_schema_ref(doc_file, schema_ref).context(SpecError)?;
if let Some(ref_key) = schema.ref_key.clone() {
if !schemas.contains_key(&ref_key) {
if !self.spec.is_input_file(&ref_key.file) {
let refs = get_schema_schema_refs(&schema.schema);
schemas.insert(ref_key.clone(), schema);
for rf in refs {
self.add_schema_refs(schemas, &ref_key.file, Reference::parse(&rf))?;
}
}
}
}
Ok(())
}

fn create_vec_alias(&self, doc_file: &Path, alias_name: &str, schema: &ResolvedSchema) -> Result<TokenStream> {
let items = get_schema_array_items(&schema.schema.common)?;
let typ = ident(&alias_name.to_camel_case());
Expand Down
Loading

0 comments on commit 99721a9

Please sign in to comment.