change TyF64 to f64 according to JsonSchema and cleanup docs code (#6081)
* cleanup gen_std Signed-off-by: Jess Frazelle <github@jessfraz.com> * cleanup docs Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix table Signed-off-by: Jess Frazelle <github@jessfraz.com> --------- Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
@ -10,6 +10,7 @@ use convert_case::Casing;
|
||||
use handlebars::Renderable;
|
||||
use indexmap::IndexMap;
|
||||
use itertools::Itertools;
|
||||
use schemars::schema::SingleOrVec;
|
||||
use serde_json::json;
|
||||
use tokio::task::JoinSet;
|
||||
|
||||
@ -673,6 +674,7 @@ fn cleanup_type_links<'a>(output: &str, types: impl Iterator<Item = &'a String>)
|
||||
// TODO: This is a hack for the handlebars template being too complex.
|
||||
cleaned_output = cleaned_output.replace("`[, `number`, `number`]`", "`[number, number]`");
|
||||
cleaned_output = cleaned_output.replace("`[, `number`, `number`, `number`]`", "`[number, number, number]`");
|
||||
cleaned_output = cleaned_output.replace("`[, `integer`, `integer`, `integer`]`", "`[integer, integer, integer]`");
|
||||
|
||||
// Fix the links to the types.
|
||||
for type_name in types.map(|s| &**s).chain(DECLARED_TYPES) {
|
||||
@ -691,7 +693,7 @@ fn cleanup_type_links<'a>(output: &str, types: impl Iterator<Item = &'a String>)
|
||||
// TODO handle union types generically rather than special casing them.
|
||||
cleaned_output = cleaned_output.replace(
|
||||
"`Sketch | Plane | Face`",
|
||||
"[`Sketch`](/docs/kcl/types/Sketch) `|` [`Plane`](/docs/kcl/types/Face) `|` [`Plane`](/docs/kcl/types/Face)",
|
||||
"[`Sketch`](/docs/kcl/types/Sketch) OR [`Plane`](/docs/kcl/types/Plane) OR [`Face`](/docs/kcl/types/Face)",
|
||||
);
|
||||
|
||||
cleanup_static_links(&cleaned_output)
|
||||
@ -720,6 +722,11 @@ fn add_to_types(
|
||||
));
|
||||
};
|
||||
|
||||
if name == "SourceRange" {
|
||||
types.insert(name.to_string(), schema.clone());
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// If we have an array we want to generate the type markdown files for each item in the
|
||||
// array.
|
||||
if let Some(array) = &o.array {
|
||||
@ -727,7 +734,7 @@ fn add_to_types(
|
||||
if let Some(items) = &array.items {
|
||||
match items {
|
||||
schemars::schema::SingleOrVec::Single(item) => {
|
||||
if is_primitive(item)?.is_some() && name != "SourceRange" {
|
||||
if is_primitive(item)?.is_some() {
|
||||
return Ok(());
|
||||
}
|
||||
return add_to_types(name.trim_start_matches('[').trim_end_matches(']'), item, types);
|
||||
@ -816,9 +823,8 @@ fn generate_type(
|
||||
}
|
||||
|
||||
let cleaned_schema = recurse_and_create_references(name, schema, types)?;
|
||||
let new_schema = super::cleanup_number_tuples(&cleaned_schema);
|
||||
|
||||
let schemars::schema::Schema::Object(o) = new_schema else {
|
||||
let schemars::schema::Schema::Object(o) = cleaned_schema else {
|
||||
return Err(anyhow::anyhow!(
|
||||
"Failed to get object schema, should have not been a primitive"
|
||||
));
|
||||
@ -950,6 +956,13 @@ fn recurse_and_create_references(
|
||||
"Failed to get object schema, should have not been a primitive"
|
||||
));
|
||||
};
|
||||
|
||||
// If this is a primitive just use the primitive.
|
||||
if to.subschemas.is_none() && to.object.is_none() && to.reference.is_none() {
|
||||
return Ok(t.clone());
|
||||
}
|
||||
|
||||
// Otherwise append the metadata to our reference.
|
||||
if let Some(metadata) = obj.metadata.as_mut() {
|
||||
if metadata.description.is_none() {
|
||||
metadata.description = to.metadata.as_ref().and_then(|m| m.description.clone());
|
||||
@ -1052,6 +1065,13 @@ fn recurse_and_create_references(
|
||||
*item = new_item;
|
||||
}
|
||||
}
|
||||
|
||||
if subschema.one_of.is_none() && subschema.all_of.is_none() && subschema.any_of.is_none() && obj.array.is_none()
|
||||
{
|
||||
if let Some(SingleOrVec::Single(_)) = &o.instance_type {
|
||||
return Ok(schema.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let obj = cleanup_type_description(&obj)
|
||||
|
@ -660,69 +660,6 @@ pub fn get_description_string_from_schema(schema: &schemars::schema::RootSchema)
|
||||
None
|
||||
}
|
||||
|
||||
pub fn cleanup_number_tuples_root(mut schema: schemars::schema::RootSchema) -> schemars::schema::RootSchema {
|
||||
cleanup_number_tuples_object(&mut schema.schema);
|
||||
schema
|
||||
}
|
||||
|
||||
fn cleanup_number_tuples_object(o: &mut schemars::schema::SchemaObject) {
|
||||
if let Some(object) = &mut o.object {
|
||||
for (_, value) in object.properties.iter_mut() {
|
||||
*value = cleanup_number_tuples(value);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(array) = &mut o.array {
|
||||
if let Some(items) = &mut array.items {
|
||||
match items {
|
||||
schemars::schema::SingleOrVec::Single(_) => {
|
||||
// Do nothing since its only a single item.
|
||||
}
|
||||
schemars::schema::SingleOrVec::Vec(items) => {
|
||||
if items.len() == 2 {
|
||||
// Get the second item and see if its a NumericType.
|
||||
|
||||
if let Some(schemars::schema::Schema::Object(obj)) = items.get(1) {
|
||||
if let Some(reference) = &obj.reference {
|
||||
if reference == "#/components/schemas/NumericType" {
|
||||
// Get the first item.
|
||||
if let Some(schemars::schema::Schema::Object(obj2)) = items.first() {
|
||||
let mut obj2 = obj2.clone();
|
||||
obj2.metadata = o.metadata.clone();
|
||||
// Replace the array with the first item.
|
||||
*o = obj2;
|
||||
}
|
||||
}
|
||||
} else if NUMERIC_TYPE_SCHEMA.object == obj.object {
|
||||
if let Some(schemars::schema::Schema::Object(obj2)) = items.first() {
|
||||
let mut obj2 = obj2.clone();
|
||||
obj2.metadata = o.metadata.clone();
|
||||
// Replace the array with the first item.
|
||||
*o = obj2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Some numbers will be tuples of 2 where the second type is always "NumericType". We want to
|
||||
/// replace these with the first item in the array and not have an array as it messes
|
||||
/// with the docs generation which assumes if there is a tuple that you give 2 values not one
|
||||
/// in the form of an array.
|
||||
fn cleanup_number_tuples(schema: &schemars::schema::Schema) -> schemars::schema::Schema {
|
||||
let mut schema = schema.clone();
|
||||
|
||||
if let schemars::schema::Schema::Object(o) = &mut schema {
|
||||
cleanup_number_tuples_object(o);
|
||||
}
|
||||
|
||||
schema
|
||||
}
|
||||
|
||||
pub fn is_primitive(schema: &schemars::schema::Schema) -> Result<Option<Primitive>> {
|
||||
match schema {
|
||||
schemars::schema::Schema::Object(o) => {
|
||||
|
Reference in New Issue
Block a user