Skip to content

Commit

Permalink
fixing code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
sagojez committed Jul 10, 2024
1 parent 010af7a commit 2501fb2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
4 changes: 3 additions & 1 deletion integrationos-api/src/endpoints/schema_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ pub async fn get_common_model_proj(
"primary": true,
"active": true,
};
let options = FindOptions::builder().projection(doc! { "_id": 1 }).build();
let options = FindOptions::builder()
.projection(doc! { "_id": 1, "name": 1 })
.build();

let mut cursor = collection.find(filter, options).await?;
let mut common_models: Vec<Document> = Vec::new();
Expand Down
33 changes: 25 additions & 8 deletions integrationos-domain/src/domain/schema/common_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,23 +271,40 @@ impl DataType {

fn as_typescript_schema(&self, e_name: String) -> String {
match self {
DataType::String => "Schema.UndefinedOr(Schema.String)".into(),
DataType::Number => "Schema.UndefinedOr(Schema.Number)".into(),
DataType::Boolean => "Schema.UndefinedOr(Schema.Boolean)".into(),
DataType::Date => "Schema.UndefinedOr(Schema.Date)".into(),
DataType::String => "Schema.optional(Schema.NullishOr(Schema.String))".into(),
DataType::Number => "Schema.optional(Schema.NullishOr(Schema.Number))".into(),
DataType::Boolean => "Schema.optional(Schema.NullishOr(Schema.Boolean))".into(),
DataType::Date => "Schema.optional(Schema.NullishOr(Schema.Date))".into(),
DataType::Enum { reference, .. } => {
if reference.is_empty() {
format!("Schema.UndefinedOr({})", e_name.pascal_case())
format!(
"Schema.optional(Schema.NullishOr({}))",
e_name.pascal_case()
)
} else {
format!("Schema.UndefinedOr({})", reference)
format!("Schema.optional(Schema.NullishOr({}))", reference)
}
}
DataType::Expandable(expandable) => {
format!("Schema.UndefinedOr({})", expandable.reference())
format!(
"Schema.optional(Schema.NullishOr({}))",
expandable.reference()
)
}
DataType::Array { element_type } => {
let name = (*element_type).as_typescript_schema(e_name);
format!("Schema.Array({})", name)
let refined = if name.contains("Schema.optional") {
name.replace("Schema.optional(", "")
.replace(")", "")
.replace("Schema.NullishOr(", "")
.replace(")", "")
} else {
name
};
format!(
"Schema.optional(Schema.NullishOr(Schema.Array({})))",
refined
)
}
}
}
Expand Down

0 comments on commit 2501fb2

Please sign in to comment.