Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with Name String Array primitive array type during deserialization #257

Merged
merged 3 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions cassis/xmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,10 @@ def _parse_primitive_array(self, type_: Type, value: Union[str, List[str]]) -> L
return [float(e) for e in elements] if value else []
elif type_name in [TYPE_NAME_INTEGER_ARRAY, TYPE_NAME_SHORT_ARRAY, TYPE_NAME_LONG_ARRAY]:
return [int(e) for e in elements] if value else []
elif type_name == TYPE_NAME_STRING_ARRAY:
if elements:
raise ValueError(f"String array values must be provided as nested elements: {elements}")
return []
elif type_name == TYPE_NAME_BOOLEAN_ARRAY:
return [self._parse_bool(e) for e in elements] if value else []
elif type_name == TYPE_NAME_BYTE_ARRAY:
Expand Down Expand Up @@ -613,10 +617,13 @@ def _serialize_feature_structure(self, cas: Cas, root: etree.Element, fs: Featur
value = sofa._offset_converter.python_to_external(value)

if ts.is_instance_of(feature.rangeType, TYPE_NAME_STRING_ARRAY) and not feature.multipleReferencesAllowed:
if value.elements is not None: # Compare to none to not skip if elements is empty!
for e in value.elements:
child = etree.SubElement(elem, feature_name)
child.text = e
if value.elements is not None: # Compare to none as not to skip if elements is empty!
if not value.elements:
elem.attrib[feature_name] = ""
else:
for e in value.elements:
child = etree.SubElement(elem, feature_name)
child.text = e
elif ts.is_instance_of(feature.rangeType, TYPE_NAME_STRING_LIST) and not feature.multipleReferencesAllowed:
if value is not None: # Compare to none to not skip if elements is empty!
for e in self._collect_list_elements(feature.rangeType.name, value):
Expand Down
29 changes: 16 additions & 13 deletions tests/test_files/xmi/cas_with_array_features.xmi
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?><xmi:XMI xmlns:noNamespace="http:///uima/noNamespace.ecore" xmlns:tcas="http:///uima/tcas.ecore" xmlns:xmi="http://www.omg.org/XMI" xmlns:cas="http:///uima/cas.ecore" xmi:version="2.0">
<cas:NULL xmi:id="0"/>
<cas:FSArray xmi:id="4" elements="2 3"/>
<noNamespace:FsArrayHolder xmi:id="6" fsArray="2 3"/>
<noNamespace:FsArrayHolderMR xmi:id="8" fsArray="7"/>
<noNamespace:IntArrayHolder xmi:id="10" intArray="1 2"/>
<noNamespace:IntArrayHolderMR xmi:id="12" intArray="11"/>
<noNamespace:FloatArrayHolder xmi:id="14" floatArray="-1.0 0.0 1.0 NaN -Infinity Infinity 1.4E-45 3.4028235E38"/>
<noNamespace:FloatArrayHolderMR xmi:id="16" floatArray="15"/>
<noNamespace:StringArrayHolder xmi:id="18">
<noNamespace:FsArrayHolder xmi:id="5" fsArray="2 3"/>
<noNamespace:FsArrayHolderMR xmi:id="9" fsArray="10"/>
<noNamespace:IntArrayHolder xmi:id="13" intArray=""/>
<noNamespace:IntArrayHolder xmi:id="11" intArray="1 2"/>
<noNamespace:IntArrayHolderMR xmi:id="15" intArray="16"/>
<noNamespace:FloatArrayHolder xmi:id="17" floatArray="-1.0 0.0 1.0 NaN -Infinity Infinity 1.4E-45 3.4028235E38"/>
<noNamespace:FloatArrayHolder xmi:id="19" floatArray=""/>
<noNamespace:FloatArrayHolderMR xmi:id="21" floatArray="22"/>
<noNamespace:StringArrayHolder xmi:id="23">
<stringArray>blah</stringArray>
<stringArray>blub</stringArray>
</noNamespace:StringArrayHolder>
<noNamespace:StringArrayHolderMR xmi:id="20" stringArray="19"/>
<noNamespace:StringArrayHolder xmi:id="25" stringArray=""/>
<noNamespace:StringArrayHolderMR xmi:id="27" stringArray="28"/>
<cas:Sofa xmi:id="1" sofaNum="1" sofaID="_InitialView"/>
<tcas:Annotation xmi:id="2" sofa="1" begin="0" end="1"/>
<tcas:Annotation xmi:id="3" sofa="1" begin="1" end="2"/>
<cas:FSArray xmi:id="7" elements="2 3"/>
<cas:IntegerArray xmi:id="11" elements="1 2"/>
<cas:FloatArray xmi:id="15" elements="-1.0 0.0 1.0 NaN -Infinity Infinity 1.4E-45 3.4028235E38"/>
<cas:StringArray xmi:id="19">
<cas:FSArray xmi:id="10" elements="2 3"/>
<cas:IntegerArray xmi:id="16" elements="1 2"/>
<cas:FloatArray xmi:id="22" elements="-1.0 0.0 1.0 NaN -Infinity Infinity 1.4E-45 3.4028235E38"/>
<cas:StringArray xmi:id="28">
<elements>blah</elements>
<elements>blub</elements>
</cas:StringArray>
<cas:View sofa="1" members="4 6 8 10 12 14 16 18 20"/>
<cas:View sofa="1" members="4 5 9 11 13 15 17 19 21 23 25 27"/>
</xmi:XMI>