contain declared model types in a "types" module

This commit is contained in:
Joshua M. Clulow 2021-07-02 00:56:53 +00:00
parent dc05524823
commit 4b0c2cec68
1 changed files with 34 additions and 30 deletions

View File

@ -462,7 +462,7 @@ impl TypeSpace {
} }
} }
fn render_type(&self, tid: &TypeId) -> Result<String> { fn render_type(&self, tid: &TypeId, in_mod: bool) -> Result<String> {
if let Some(te) = self.id_to_entry.get(&tid) { if let Some(te) = self.id_to_entry.get(&tid) {
match &te.details { match &te.details {
TypeDetails::Basic => { TypeDetails::Basic => {
@ -473,30 +473,23 @@ impl TypeSpace {
} }
} }
TypeDetails::Array(itid) => { TypeDetails::Array(itid) => {
if let Some(ite) = self.id_to_entry.get(&itid) { Ok(format!("Vec<{}>", self.render_type(itid, in_mod)?))
if let Some(n) = &ite.name {
Ok(format!("Vec<{}>", n))
} else {
bail!("array inner type {:?} no name?", ite);
}
} else {
bail!("array inner type {:?} missing?", itid);
}
} }
TypeDetails::Optional(itid) => { TypeDetails::Optional(itid) => {
if let Some(ite) = self.id_to_entry.get(&itid) { Ok(format!("Option<{}>", self.render_type(itid, in_mod)?))
if let Some(n) = &ite.name {
Ok(format!("Option<{}>", n))
} else {
bail!("optional inner type {:?} no name?", ite);
}
} else {
bail!("optional inner type {:?} missing?", itid);
}
} }
TypeDetails::Object(_) => { TypeDetails::Object(_) => {
if let Some(n) = &te.name { if let Some(n) = &te.name {
if in_mod {
Ok(n.to_string()) Ok(n.to_string())
} else {
/*
* Model types are declared in the "types" module,
* and must be referenced with that prefix when not
* in the module itself.
*/
Ok(format!("types::{}", n.to_string()))
}
} else { } else {
bail!("object type {:?} does not have a name?", tid); bail!("object type {:?} does not have a name?", tid);
} }
@ -762,10 +755,6 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
* Deal with any dependencies we require to produce this client. * Deal with any dependencies we require to produce this client.
*/ */
a(""); a("");
if ts.import_chrono {
a("use chrono::prelude::*;");
}
a("use serde::{Serialize, Deserialize};");
a("use anyhow::Result;"); /* XXX */ a("use anyhow::Result;"); /* XXX */
a(""); a("");
@ -805,15 +794,28 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
/* /*
* Declare named types we know about: * Declare named types we know about:
*/ */
a("pub mod types {");
if ts.import_chrono {
a(" use chrono::prelude::*;");
}
a(" use serde::{Serialize, Deserialize};");
a("");
for te in ts.id_to_entry.values() { for te in ts.id_to_entry.values() {
match &te.details { match &te.details {
TypeDetails::Object(omap) => { TypeDetails::Object(omap) => {
a("#[derive(Serialize, Deserialize, Debug)]"); a(" #[derive(Serialize, Deserialize, Debug)]");
a(&format!("pub struct {} {{", te.name.as_deref().unwrap())); a(&format!(
" pub struct {} {{",
te.name.as_deref().unwrap()
));
for (name, tid) in omap.iter() { for (name, tid) in omap.iter() {
a(&format!(" pub {}: {},", name, ts.render_type(tid)?)); a(&format!(
" pub {}: {},",
name,
ts.render_type(tid, true)?
));
} }
a("}"); a(" }");
a(""); a("");
} }
TypeDetails::Basic => {} TypeDetails::Basic => {}
@ -822,6 +824,8 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
TypeDetails::Optional(_) => {} TypeDetails::Optional(_) => {}
} }
} }
a("}");
a("");
/* /*
* Declare the client object: * Declare the client object:
@ -889,7 +893,7 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
if let Some(s) = &mt.schema { if let Some(s) = &mt.schema {
let tid = ts.select(None, s)?; let tid = ts.select(None, s)?;
( (
Some(format!("&{}", ts.render_type(&tid)?)), Some(format!("&{}", ts.render_type(&tid, false)?)),
Some("json".to_string()), Some("json".to_string()),
) )
} else { } else {
@ -991,7 +995,7 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result<String> {
let tid = ts.select(None, s)?; let tid = ts.select(None, s)?;
a(&format!( a(&format!(
" ) -> Result<{}> {{", " ) -> Result<{}> {{",
ts.render_type(&tid)? ts.render_type(&tid, false)?
)); ));
} else { } else {
bail!("media type encoding, no schema: {:#?}", mt); bail!("media type encoding, no schema: {:#?}", mt);