Skip to content

Commit

Permalink
fix some types
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carlstrom <[email protected]>
  • Loading branch information
InvincibleRMC committed Nov 23, 2024
1 parent 7415db7 commit 444e994
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pathlib import Path
import re
import sys
from typing import Dict, Final, List, Set, Tuple, TYPE_CHECKING, TypedDict
from typing import Dict, Final, List, Set, Tuple, TYPE_CHECKING, TypedDict, Union

from rosidl_parser import definition
from rosidl_parser.parser import parse_idl_file
Expand Down Expand Up @@ -378,6 +378,11 @@ def parse_rihs_string(rihs_str: str) -> Tuple[int, str]:
}


FIELD_VALUE_STRING_TYPES: Final = (definition.UnboundedString,
definition.BoundedString,
definition.BoundedWString,
definition.UnboundedWString)

def field_type_type_name(ftype: definition.AbstractType) -> str:
value_type = ftype
name_suffix = ''
Expand All @@ -388,7 +393,7 @@ def field_type_type_name(ftype: definition.AbstractType) -> str:

if isinstance(value_type, definition.BasicType):
value_type_name = FIELD_VALUE_TYPE_NAMES[value_type.typename]
elif isinstance(value_type, definition.AbstractGenericString):
elif isinstance(value_type, FIELD_VALUE_STRING_TYPES):
value_type_name = FIELD_VALUE_TYPE_NAMES[type(value_type)]
elif (
isinstance(value_type, definition.NamespacedType) or
Expand All @@ -407,25 +412,19 @@ def field_type_type_id(ftype: definition.AbstractType) -> int:

def field_type_capacity(ftype: definition.AbstractType) -> int:
if isinstance(ftype, definition.AbstractNestedType):
if ftype.has_maximum_size():
try:
return ftype.maximum_size
except AttributeError:
return ftype.size
if isinstance(ftype, definition.Array):
return ftype.size
return 0


def field_type_string_capacity(ftype: definition.AbstractType) -> int:
def field_type_string_capacity(ftype: definition.AbstractType) -> Union[int, str]:
value_type = ftype
if isinstance(ftype, definition.AbstractNestedType):
value_type = ftype.value_type

if isinstance(value_type, definition.AbstractGenericString):
if value_type.has_maximum_size():
try:
return value_type.maximum_size
except AttributeError:
return value_type.size
if isinstance(value_type, (definition.BoundedString, definition.BoundedWString)):
return value_type.maximum_size
return 0


Expand All @@ -443,7 +442,7 @@ def field_type_nested_type_name(ftype: definition.AbstractType, joiner: str = '/
class SerializeFieldTypeDict(TypedDict):
type_id: int
capacity: int
string_capacity: int
string_capacity: Union[int, str]
nested_type_name: str


Expand Down
8 changes: 4 additions & 4 deletions rosidl_generator_type_description/test/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ def test_field_type_serializer() -> None:
assert result == expected

bounded_sequence_limit = 32
test_type = definition.BoundedSequence(definition.UnboundedString(), bounded_sequence_limit)
test_type2 = definition.BoundedSequence(definition.UnboundedString(), bounded_sequence_limit)
expected = {
'type_id': 113,
'capacity': bounded_sequence_limit,
'string_capacity': 0,
'nested_type_name': '',
}
result = serialize_field_type(test_type)
result = serialize_field_type(test_type2)
assert result == expected

test_type = definition.BoundedWString(string_limit)
test_type3 = definition.BoundedWString(string_limit)
expected = {
'type_id': 22,
'capacity': 0,
'string_capacity': string_limit,
'nested_type_name': '',
}
result = serialize_field_type(test_type)
result = serialize_field_type(test_type3)
assert result == expected


Expand Down

0 comments on commit 444e994

Please sign in to comment.