1
0

update magic draw parser to handle more edge cases

This commit is contained in:
Rokas Puzonas 2023-03-06 19:57:00 +02:00
parent 70408b0602
commit bf92f783e4
4 changed files with 32 additions and 28 deletions

View File

@ -61,7 +61,7 @@ fn parse_class<R: Read>(
})?; })?;
Ok(DDLClass { Ok(DDLClass {
class_id: class_id.context("Missing class id")?, class_id: class_id.context("Missing dll class id")?,
property_ids, property_ids,
}) })
} }

View File

@ -216,6 +216,8 @@ fn get_sql_type(
Ok(match type_name { Ok(match type_name {
SQLTypeName::Int => SQLType::Int, SQLTypeName::Int => SQLType::Int,
SQLTypeName::Date => SQLType::Date, SQLTypeName::Date => SQLType::Date,
SQLTypeName::Datetime => SQLType::Datetime,
SQLTypeName::Time => SQLType::Time,
SQLTypeName::Float => SQLType::Float, SQLTypeName::Float => SQLType::Float,
SQLTypeName::Bool => SQLType::Bool, SQLTypeName::Bool => SQLType::Bool,
SQLTypeName::Decimal => SQLType::Decimal, SQLTypeName::Decimal => SQLType::Decimal,
@ -266,17 +268,6 @@ pub fn parse_project<R: Read + Seek>(project_file: R) -> Result<Vec<SQLTableColl
for ddl_script in ddl_project.scripts { for ddl_script in ddl_project.scripts {
let mut tables = vec![]; let mut tables = vec![];
let model_properties = ddl_script
.classess
.iter()
.flat_map(|class| {
class
.property_ids
.iter()
.map(|prop| (&class.class_id, prop))
})
.collect::<Vec<_>>();
let mut model_classess = vec![]; let mut model_classess = vec![];
for ddl_class in &ddl_script.classess { for ddl_class in &ddl_script.classess {
let model_class = find_class_by_id(&models, &ddl_class.class_id) let model_class = find_class_by_id(&models, &ddl_class.class_id)
@ -302,7 +293,7 @@ pub fn parse_project<R: Read + Seek>(project_file: R) -> Result<Vec<SQLTableColl
let type_href = unwrap_opt_continue!(&property.type_href); let type_href = unwrap_opt_continue!(&property.type_href);
let type_name = sql_type_names let type_name = sql_type_names
.get(type_href) .get(type_href)
.context("Proerty type name conversion not found")?; .context("Property type name conversion not found")?;
let check_constraint = get_sql_check_constraint(&models, &prop_name); let check_constraint = get_sql_check_constraint(&models, &prop_name);
let foreign_key = get_foreign_key(&modifiers, &model_classess, property_id)?; let foreign_key = get_foreign_key(&modifiers, &model_classess, property_id)?;

View File

@ -13,7 +13,7 @@ use super::utils::{check_attribute, check_name, get_attribute, parse_element, My
#[derive(Debug)] #[derive(Debug)]
struct UsedPackage { struct UsedPackage {
share_point_id: String, share_point_ids: Vec<String>,
name: String, name: String,
needed_types: Vec<String>, needed_types: Vec<String>,
} }
@ -23,6 +23,8 @@ pub enum SQLTypeName {
Int, Int,
Decimal, Decimal,
Date, Date,
Datetime,
Time,
Float, Float,
Bool, Bool,
Char, Char,
@ -39,22 +41,26 @@ fn parse_used_package<R: Read>(
attrs: &[OwnedAttribute], attrs: &[OwnedAttribute],
needed_types: &[&str], needed_types: &[&str],
) -> Result<UsedPackage> { ) -> Result<UsedPackage> {
let mut share_point_id = None; let mut share_point_ids = vec![];
let project_uri = get_attribute(&attrs, None, "usedProjectURI")?; let project_uri = get_attribute(&attrs, None, "usedProjectURI")?;
let name = project_uri.split("/").last().unwrap(); let name = project_uri.split("/").last().unwrap();
parse_element(parser, &mut |p, name, attrs| { parse_element(parser, &mut |p, name, attrs| {
if share_point_id.is_none() && check_name(&name, None, "mountPoints") { if check_name(&name, None, "mountPoints") {
share_point_id = get_attribute(&attrs, None, "sharePointID") if let Ok(mount_id) = get_attribute(&attrs, None, "sharePointID").map(str::to_string) {
.ok() share_point_ids.push(mount_id);
.map(str::to_string); }
} }
Ok(()) Ok(())
})?; })?;
if share_point_ids.is_empty() {
bail!("Share point mount ids not found")
}
Ok(UsedPackage { Ok(UsedPackage {
name: name.to_string(), name: name.to_string(),
share_point_id: share_point_id.context("Share point id not found")?, share_point_ids,
needed_types: needed_types.iter().map(|s| s.to_string()).collect(), needed_types: needed_types.iter().map(|s| s.to_string()).collect(),
}) })
} }
@ -109,14 +115,16 @@ fn is_umodel_snapshot_file(filename: &str) -> bool {
fn parse_type_name(str: &str) -> Result<SQLTypeName> { fn parse_type_name(str: &str) -> Result<SQLTypeName> {
use SQLTypeName::*; use SQLTypeName::*;
Ok(match str { Ok(match &str.to_lowercase()[..] {
"decimal" | "dec" => Decimal, "decimal" | "dec" => Decimal,
"char" => Char, "char" => Char,
"varchar" => Varchar, "varchar" | "string" => Varchar,
"float" => Float, "float" | "double precision" => Float, // TODO: Cheecky double precision -> float
"Integer" | "integer" | "int" => Int, "integer" | "int" => Int,
"date" => Date, "date" => Date,
"Boolean" => Bool, "datetime" => Datetime,
"time" => Time,
"boolean" => Bool,
_ => bail!("Unknown SQL type: '{}'", str), _ => bail!("Unknown SQL type: '{}'", str),
}) })
} }
@ -161,8 +169,9 @@ fn parse_primitive_types<R: Read>(
} => { } => {
if check_name(&name, Some("uml"), "Package") { if check_name(&name, Some("uml"), "Package") {
if let Some(id) = get_attribute(&attributes, None, "ID").ok() { if let Some(id) = get_attribute(&attributes, None, "ID").ok() {
let id = id.to_string();
if let Some(package) = if let Some(package) =
used_packages.iter().find(|p| p.share_point_id.eq(id)) used_packages.iter().find(|p| p.share_point_ids.contains(&id))
{ {
let package_types = parse_types_package(&mut parser)? let package_types = parse_types_package(&mut parser)?
.into_iter() .into_iter()

View File

@ -147,7 +147,7 @@ fn parse_constraint<R: Read>(
if let Some((prop_name, check_body)) = body.unwrap().split_once(" in ") { if let Some((prop_name, check_body)) = body.unwrap().split_once(" in ") {
return Ok(Some(UMLConstraint { return Ok(Some(UMLConstraint {
id, id,
class_id: Some(constrainted_element_id.context("Missing class id")?), class_id: Some(constrainted_element_id.context("Missing constraint class id")?),
body: Some(format!("in {}", check_body)), body: Some(format!("in {}", check_body)),
property_id: None, property_id: None,
property_name: Some(prop_name.into()), property_name: Some(prop_name.into()),
@ -155,9 +155,13 @@ fn parse_constraint<R: Read>(
} }
} }
if constrainted_element_id.is_none() {
return Ok(None);
}
return Ok(Some(UMLConstraint { return Ok(Some(UMLConstraint {
id, id,
property_id: Some(constrainted_element_id.context("Missing property id")?), property_id: Some(constrainted_element_id.unwrap()),
body: None, body: None,
class_id: None, class_id: None,
property_name: None, property_name: None,