-
Notifications
You must be signed in to change notification settings - Fork 45
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
store types as tuple of abstract types #33
Changes from all commits
1b8d512
e2443ad
d6589fd
64003b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright 2019 Mikael Arguedas. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import importlib | ||
from typing import Any | ||
|
||
from rosidl_parser.definition import NamespacedType | ||
|
||
|
||
def import_message_from_namespaced_type(message_type: NamespacedType) -> Any: | ||
module = importlib.import_module( | ||
'.'.join(message_type.value_type.namespaces)) | ||
return getattr(module, message_type.value_type.name) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,11 @@ | |
from typing import Any | ||
from typing import Dict | ||
|
||
from rosidl_parser.definition import AbstractNestedType | ||
from rosidl_parser.definition import NamespacedType | ||
from rosidl_runtime_py.convert import get_message_slot_types | ||
from rosidl_runtime_py.import_message import import_message_from_namespaced_type | ||
|
||
|
||
def set_message_fields(msg: Any, values: Dict[str, str]) -> None: | ||
""" | ||
|
@@ -33,4 +38,13 @@ def set_message_fields(msg: Any, values: Dict[str, str]) -> None: | |
except TypeError: | ||
value = field_type() | ||
set_message_fields(value, field_value) | ||
rosidl_type = get_message_slot_types(msg)[field_name] | ||
# Check if field is an array of ROS messages | ||
if isinstance(rosidl_type, AbstractNestedType): | ||
if isinstance(rosidl_type.value_type, NamespacedType): | ||
field_elem_type = import_message_from_namespaced_type(rosidl_type) | ||
for n in range(len(value)): | ||
submsg = field_elem_type() | ||
set_message_fields(submsg, value[n]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added test coverage in ad65970 |
||
value[n] = submsg | ||
setattr(msg, field_name, value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am surprised this works in cases where the field type is
numpy.ndarray
orarray.array
. Can you please also add a test with a static array (which would cover thenumpy
case).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it surprised me as well that the code comparing to
list
didn't need to be updated after the numpy array PR..Static array test case added in 712d4da