Skip to content

Commit

Permalink
#167 - Cant serialize byte arrays
Browse files Browse the repository at this point in the history
- Add function to check whether something is a primitive array
  • Loading branch information
jcklie committed Aug 14, 2021
1 parent 9c7d6d1 commit 01cd86d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cassis/typesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@
}


_PRIMITIVE_ARRAY_TYPES = {
"uima.cas.FloatArray",
"uima.cas.IntegerArray",
"uima.cas.BooleanArray",
"uima.cas.ByteArray",
"uima.cas.ShortArray",
"uima.cas.LongArray",
"uima.cas.DoubleArray",
}


def _string_to_valid_classname(name: str):
return re.sub("[^a-zA-Z0-9_]", "_", name)

Expand Down Expand Up @@ -550,6 +561,21 @@ def is_primitive_collection(self, type_name) -> bool:
else:
return self.is_primitive_collection(self.get_type(type_name).supertypeName)

def is_primitive_array(self, type_name) -> bool:
"""Checks if the type identified by `type_name` is a primitive array, e.g. array of primitives.
Args:
type_name: The name of the type to query for.
Returns:
Returns True if the type identified by `type_name` is a primitive array type, else False
"""
if type_name == TOP_TYPE_NAME:
return False
elif type_name in _PRIMITIVE_ARRAY_TYPES:
return True
else:
return self.is_primitive_array(self.get_type(type_name).supertypeName)

def subsumes(self, parent_name: str, child_name: str) -> bool:
"""Determines if the type `child_name` is a child of `parent_name`.
Expand Down
34 changes: 34 additions & 0 deletions tests/test_typesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,40 @@ def test_is_primitive_collection(type_name: str, expected: bool):
assert typesystem.is_primitive_collection(type_name) == expected


@pytest.mark.parametrize(
"type_name, expected",
[
("uima.cas.ArrayBase", False),
("uima.cas.FSArray", False),
("uima.cas.FloatArray", True),
("uima.cas.IntegerArray", True),
("uima.cas.StringArray", False),
("uima.cas.ListBase", False),
("uima.cas.FSList", False),
("uima.cas.EmptyFSList", False),
("uima.cas.NonEmptyFSList", False),
("uima.cas.FloatList", False),
("uima.cas.EmptyFloatList", False),
("uima.cas.NonEmptyFloatList", False),
("uima.cas.IntegerList", False),
("uima.cas.EmptyIntegerList", False),
("uima.cas.NonEmptyIntegerList", False),
("uima.cas.StringList", False),
("uima.cas.EmptyStringList", False),
("uima.cas.NonEmptyStringList", False),
("uima.cas.BooleanArray", True),
("uima.cas.ByteArray", True),
("uima.cas.ShortArray", True),
("uima.cas.LongArray", True),
("uima.cas.DoubleArray", True),
],
)
def test_is_primitive_collection(type_name: str, expected: bool):
typesystem = TypeSystem()

assert typesystem.is_primitive_array(type_name) == expected


@pytest.mark.parametrize(
"parent_name, child_name, expected",
[
Expand Down

0 comments on commit 01cd86d

Please sign in to comment.