Skip to content

Commit

Permalink
better default
Browse files Browse the repository at this point in the history
Signed-off-by: Jess Frazelle <[email protected]>
  • Loading branch information
jessfraz committed Apr 23, 2024
1 parent 558b256 commit 7888e29
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 111 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.69
0.3.0
2 changes: 1 addition & 1 deletion kittycad.rs.patch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"path": "/info/x-rust",
"value": {
"client": "// Authenticate via an API token.\nlet client = kittycad::Client::new(\"$TOKEN\");\n\n// - OR -\n\n// Authenticate with your token and host parsed from the environment variables:\n// `KITTYCAD_API_TOKEN`.\nlet client = kittycad::Client::new_from_env();",
"install": "[dependencies]\nkittycad = \"0.2.69\""
"install": "[dependencies]\nkittycad = \"0.3.0\""
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion kittycad/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "kittycad"
description = "A fully generated & opinionated API client for the KittyCAD API."
version = "0.2.69"
version = "0.3.0"
documentation = "https://docs.rs/kittycad"
readme = "README.md"
repository = "https://github.com/KittyCAD/kittycad.rs/tree/main/kittycad"
Expand Down
2 changes: 1 addition & 1 deletion kittycad/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ To install the library, add the following to your `Cargo.toml` file.

```toml
[dependencies]
kittycad = "0.2.69"
kittycad = "0.3.0"
```

## Basic example
Expand Down
2 changes: 1 addition & 1 deletion kittycad/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//!
//! ```toml
//! [dependencies]
//! kittycad = "0.2.69"
//! kittycad = "0.3.0"
//! ```
//!
//! ## Basic example
Expand Down
75 changes: 62 additions & 13 deletions kittycad/src/types.rs

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions kittycad/tests/one_of.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@
"type": "file_conversion",
"completed_at": "2022-06-17T00:28:10.059Z",
"created_at": "2022-06-17T00:27:43.806596404Z",
"error": null,
"id": "23a9759f-ee9b-47de-9a55-deb1ed035793",
"output_format": "obj",
"output_format_options": null,
"outputs": null,
"src_format": "stl",
"src_format_options": null,
"started_at": "2022-06-17T00:28:01.084Z",
"status": "completed",
"updated_at": "2022-06-17T00:27:43.806596404Z",
Expand Down
7 changes: 3 additions & 4 deletions openapitor/src/types/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rand::{Rng, SeedableRng};

use crate::types::{
exts::{ReferenceOrExt, SchemaRenderExt, TokenStreamExt},
is_default_property,
random::Random,
};

Expand Down Expand Up @@ -506,7 +507,7 @@ pub fn generate_example_rust_from_schema(

let inner_schema = v.get_schema_from_reference(&type_space.spec, true)?;
let type_name = crate::types::get_type_name_for_schema(
&k,
k,
&inner_schema,
&type_space.spec,
true,
Expand All @@ -523,9 +524,7 @@ pub fn generate_example_rust_from_schema(

// Check if this type is required.
if !o.required.contains(k)
&& !type_space
.clone()
.is_default_property(&type_name, &inner_schema.schema_data)?
&& !is_default_property(&type_name, &inner_schema.schema_data)?
&& !example
.rendered()?
.starts_with("crate::types::phone_number::PhoneNumber")
Expand Down
101 changes: 37 additions & 64 deletions openapitor/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ impl TypeSpace {
&property_schema.schema_kind
{
let inner_values =
self.get_object_values(&inner_name_ident, o, false)?;
self.get_object_values(&inner_name_ident, o, false, None)?;
values = quote! {
#values
#inner_name_ident {
Expand Down Expand Up @@ -638,7 +638,7 @@ impl TypeSpace {
}
}

let values = self.get_object_values(&struct_name, o, true)?;
let values = self.get_object_values(&struct_name, o, true, None)?;

// Implement pagination for this type if we should.
let mut pagination = quote!();
Expand Down Expand Up @@ -703,7 +703,7 @@ impl TypeSpace {
let type_name = get_type_name_for_schema(&prop, &inner_schema, &self.spec, true)?;
// Check if this type is required.
let required = o.required.contains(k)
|| self.is_default_property(&type_name, &inner_schema.schema_data)?;
|| is_default_property(&type_name, &inner_schema.schema_data)?;
if required && type_name.is_string()? {
fields.push(quote!(
self.#prop_ident.clone().into()
Expand Down Expand Up @@ -781,9 +781,15 @@ impl TypeSpace {
struct_name: &proc_macro2::Ident,
o: &openapiv3::ObjectType,
is_pub: bool,
ignore_key: Option<&str>,
) -> Result<proc_macro2::TokenStream> {
let mut values = quote!();
for (k, v) in &o.properties {
if let Some(ignore_key) = ignore_key {
if k == ignore_key {
continue;
}
}
let prop = clean_property_name(k);

// Get the schema for the property.
Expand Down Expand Up @@ -852,7 +858,7 @@ impl TypeSpace {

// Check if this type is required.
let required = o.required.contains(k)
|| self.is_default_property(&type_name, &inner_schema.schema_data)?;
|| is_default_property(&type_name, &inner_schema.schema_data)?;
if !required && !type_name.is_option()? {
// Make the type optional.
type_name = quote!(Option<#type_name>);
Expand Down Expand Up @@ -882,7 +888,7 @@ impl TypeSpace {
serde_props.push(quote!(skip_serializing_if = "Option::is_none"));
}
if !o.required.contains(k)
&& self.is_default_property(&type_name, &inner_schema.schema_data)?
&& is_default_property(&type_name, &inner_schema.schema_data)?
&& !type_name.is_option()?
{
serde_props.push(quote!(default));
Expand Down Expand Up @@ -944,14 +950,6 @@ impl TypeSpace {
Ok(values)
}

fn is_default_property(
&mut self,
type_name: &proc_macro2::TokenStream,
data: &openapiv3::SchemaData,
) -> Result<bool> {
Ok(data.default.is_some() && type_name.rendered()? == "bool")
}

/// Render a string type.
fn render_string_type(
&mut self,
Expand Down Expand Up @@ -1233,8 +1231,13 @@ impl TypeSpace {
if let openapiv3::SchemaKind::Type(openapiv3::Type::Object(o)) =
&s.schema_kind
{
enum_object_internal =
Some(render_enum_object_internal(&p, o, &self.spec, tag)?);
enum_object_internal = Some(render_enum_object_internal(
&self.clone(),
&p,
o,
&self.spec,
tag,
)?);
}
get_type_name_for_schema(&content_type_name, s, &self.spec, true)?
} else {
Expand Down Expand Up @@ -1284,8 +1287,13 @@ impl TypeSpace {
}
} else {
// Render this object.
let content_name =
render_enum_object_internal(&tag_name, o, &self.spec, tag)?;
let content_name = render_enum_object_internal(
&self.clone(),
&tag_name,
o,
&self.spec,
tag,
)?;
// Get the type name for this value.
values.insert(p.to_string(), one_of.clone());

Expand Down Expand Up @@ -1798,13 +1806,23 @@ fn get_type_name_for_array(
Ok(quote!(Vec<#t>))
}

fn is_default_property(
type_name: &proc_macro2::TokenStream,
data: &openapiv3::SchemaData,
) -> Result<bool> {
Ok(data.default.is_some()
&& (type_name.rendered()? == "bool" || type_name.rendered()?.starts_with("Vec<")))
}

// Render the internal enum type for an object.
fn render_enum_object_internal(
type_space: &TypeSpace,
name: &str,
o: &openapiv3::ObjectType,
spec: &openapiv3::OpenAPI,
ignore_key: &str,
) -> Result<proc_macro2::TokenStream> {
let mut type_space = type_space.clone();
let proper_name = proper_name(name);
let struct_name = format_ident!("{}", proper_name);

Expand All @@ -1826,55 +1844,10 @@ fn render_enum_object_internal(
}
}

let mut values = quote!();
for (k, v) in &o.properties {
if k == ignore_key {
continue;
}
// Get the type name for the schema.
let mut type_name = if let openapiv3::ReferenceOr::Item(i) = v {
get_type_name_for_schema(k, i, spec, true)?
} else {
get_type_name_from_reference(&v.reference()?, spec, true)?
};

// Get the schema for the property.
let inner_schema = if let openapiv3::ReferenceOr::Item(i) = v {
let s = &**i;
s.clone()
} else {
v.get_schema_from_reference(spec, true)?
};

let description = if let Some(d) = &inner_schema.schema_data.description {
let d_sanitized = sanitize_indents(d);
quote!(#[doc = #d_sanitized])
} else {
quote!()
};

// Check if this type is required.
if !o.required.contains(k) && !type_name.is_option()? {
// Make the type optional.
type_name = quote!(Option<#type_name>);
}
let prop_ident = format_ident!("{}", k);

let prop_value = quote!(
#prop_ident: #type_name,
);

values = quote!(
#values

#description
#prop_value
);
}

let inner_values = type_space.get_object_values(&struct_name, o, false, Some(ignore_key))?;
let rendered = quote! {
#struct_name {
#values
#inner_values
}
};

Expand Down
Loading

0 comments on commit 7888e29

Please sign in to comment.