Skip to content

Commit

Permalink
feat(ocafile): add support to parse array[object]
Browse files Browse the repository at this point in the history
  • Loading branch information
mitfik committed Jan 1, 2024
1 parent 3c355b7 commit 3bb8396
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
49 changes: 43 additions & 6 deletions oca-file/src/ocafile/instructions/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ pub fn extract_attribute_type(attr_pair: Pair) -> Option<(String, NestedAttrType
attr_name = item.as_str().to_string();
debug!("Extracting attribute key {:?}", attr_name);
},
Rule::object_attr_type => {
// TODO hack to make it work for ARRAY needs to be solved properly
debug!("Matching object attribute type from rule: {:?}", item);
let mut entries = IndexMap::new();
// TODO recurently parse nested objects
// Currently extract_attribute_type fn does not handle nested objects,
// ita always overwrites the attr
let (entry_key, entry_value) = extract_attribute_type(item).unwrap();
entries.insert(entry_key, entry_value);
attr_type = NestedAttrType::Object(entries);
},
Rule::_attr_type => {
debug!("Attribute type to parse: {:?}", item);
if let Some(attr_type_rule) = item.clone().into_inner().next() {
Expand Down Expand Up @@ -44,13 +55,39 @@ pub fn extract_attribute_type(attr_pair: Pair) -> Option<(String, NestedAttrType
}
Rule::array_attr_type => {
debug!("Matching array attribute type from rule: {:?}", attr_type_rule);
if let Some(value) = attr_type_rule.clone().into_inner().next() {
match AttributeType::from_str(value.as_span().as_str()) {
Ok(base_attr_type) => {
attr_type = NestedAttrType::Array(Box::new(NestedAttrType::Value(base_attr_type)));
// TODO hack: First try basic type if doesn not work try to extract next level.
for temp_attr_type in attr_type_rule.clone().into_inner() {
match temp_attr_type.as_rule() {
Rule::base_attr_type => {
match AttributeType::from_str(temp_attr_type.as_span().as_str()) {
Ok(base_attr_type) => {
debug!("Attribute type: {:?}", base_attr_type);
attr_type = NestedAttrType::Value(base_attr_type);
}
Err(e) => {
panic!("Invalid attribute type {:?}", e);
}
}
}
Err(e) => {
panic!("Invalid attribute type {:?}", e);
_ => {
if let Some((_, inner_type)) = extract_attribute_type(temp_attr_type.clone()) {
// TODO recursion needed
match inner_type {
NestedAttrType::Value(base_attr_type) => {
attr_type = NestedAttrType::Array(Box::new(NestedAttrType::Value(base_attr_type)));
}
NestedAttrType::Reference(ref_value) => {
attr_type = NestedAttrType::Array(Box::new(NestedAttrType::Reference(ref_value)));
}
NestedAttrType::Object(entries) => {
attr_type = NestedAttrType::Array(Box::new(NestedAttrType::Object(entries)));
}
NestedAttrType::Array(box_attr_type) => {
attr_type = NestedAttrType::Array(Box::new(NestedAttrType::Array(box_attr_type)));
},
NestedAttrType::Null => todo!(),
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions oca-file/src/ocafile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ mod tests {
-- version=0.0.1
-- name=プラスウルトラ
ADD ATTRIBUTE name=Text age=Numeric car=Object({vin=Text, model=Text, year=Numeric})
ADD ATTRIBUTE incidentals_spare_parts=Array[Object({part_number=Text, description=Text, unit=Text, quantity=Numeric})]
ADD ATTRIBUTE d=Text i=Text passed=Boolean
ADD META en PROPS description="Entrance credential" name="Entrance credential"
ADD CHARACTER_ENCODING ATTRS d="utf-8" i="utf-8" passed="utf-8"
Expand Down

0 comments on commit 3bb8396

Please sign in to comment.