Skip to content

Commit

Permalink
feat: handle nested elements
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelm committed Jun 25, 2022
1 parent 564aa96 commit 2467b8f
Show file tree
Hide file tree
Showing 9 changed files with 149,167 additions and 27,653 deletions.
15 changes: 15 additions & 0 deletions broadworks_ocip/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ElementInfo:
is_array: bool = attr.ib(default=False)
is_table: bool = attr.ib(default=False)
is_abstract: bool = attr.ib(default=False)
is_container: bool = attr.ib(default=False)


class OCIType:
Expand Down Expand Up @@ -86,6 +87,11 @@ def __init__(self, **kwargs):
raise TypeError(
f"{cname}: Expected {elem.name} to be a table/list but it is {type(value)}",
)
elif elem.is_container:
if not isinstance(value, dict):
raise TypeError(
f"{cname}: Expected {elem.name} to be a dict of elements but it is {type(value)}",
)
elif not isinstance(value, elem.type):
raise TypeError(
f"{cname}: Expected {elem.name} to be type {elem.type} but it is {type(value)}",
Expand Down Expand Up @@ -231,6 +237,15 @@ def etree_sub_element_(
for col in row:
col_item = etree.SubElement(row_item, "col")
col_item.text = col
elif sub_element.is_container:
elem = etree.SubElement(
element,
sub_element.xmlname,
nsmap=self._default_nsmap(),
)
for item in sub_element.type:
if item.name in value:
self.etree_sub_element_(elem, item, value[item.name])
elif sub_element.is_complex:
if sub_element.is_abstract:
elem = etree.SubElement(
Expand Down

0 comments on commit 2467b8f

Please sign in to comment.