Skip to content

Commit

Permalink
Fix array behavior (#692)
Browse files Browse the repository at this point in the history
**Public API Changes**
`uint8[]` will be sent in `binary` format instead of `list` format.

**Description**
Split from #646.

Fixed the behavior around arrays.

Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp>
  • Loading branch information
kenji-miyake authored Jan 20, 2022
1 parent cf1beef commit 5398588
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@
"uint32",
"int64",
"uint64",
"float32",
"float64",
],
"float": ["float32", "float64", "double", "float"],
"str": ["string"],
Expand Down Expand Up @@ -91,7 +89,7 @@
"string",
]
ros_header_types = ["Header", "std_msgs/Header", "roslib/Header"]
ros_binary_types = ["uint8[]", "char[]"]
ros_binary_types = ["uint8[]", "char[]", "sequence<uint8>", "sequence<char>"]
list_tokens = re.compile("<(.+?)>")
bounded_array_tokens = re.compile(r"(.+)\[.*\]")
ros_binary_types_list_braces = [
Expand Down Expand Up @@ -250,8 +248,13 @@ def _from_list_inst(inst, rostype):
rostype = re.search(bounded_array_tokens, rostype).group(1)

# Shortcut for primitives
if rostype in ros_primitive_types and rostype not in type_map.get("float"):
return list(inst)
if rostype in ros_primitive_types:
# Convert to Built-in integer types to dump as JSON
if isinstance(inst, np.ndarray) and rostype in type_map.get("int"):
return inst.tolist()

if rostype not in type_map.get("float"):
return list(inst)

# Call to _to_inst for every element of the list
return [_from_inst(x, rostype) for x in inst]
Expand Down Expand Up @@ -293,10 +296,11 @@ def _to_inst(msg, rostype, roottype, inst=None, stack=[]):


def _to_binary_inst(msg):
try:
return standard_b64decode(msg) if isinstance(msg, str) else bytes(bytearray(msg))
except Exception:
if isinstance(msg, str):
return list(standard_b64decode(msg))
if isinstance(msg, list):
return msg
return bytes(bytearray(msg))


def _to_time_inst(msg, rostype, inst=None):
Expand Down Expand Up @@ -352,8 +356,9 @@ def _to_list_inst(msg, rostype, roottype, inst, stack):
if len(msg) == 0:
return []

if isinstance(inst, np.ndarray):
return list(inst.astype(float))
# Special mappings for numeric types https://design.ros2.org/articles/idl_interface_definition.html
if isinstance(inst, array.array) or isinstance(inst, np.ndarray):
return msg

# Remove the list indicators from the rostype
try:
Expand Down
6 changes: 2 additions & 4 deletions rosbridge_library/src/rosbridge_library/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,8 @@ def serialize(self, msg, cid=None):
return bson.BSON.encode(msg)
else:
return json.dumps(msg)
except Exception:
if cid is not None:
# Only bother sending the log message if there's an id
self.log("error", "Unable to serialize %s message to client" % msg["op"], cid)
except Exception as e:
self.log("error", f"Unable to serialize message '{msg}': {e}")
return None

def deserialize(self, msg, cid=None):
Expand Down
1 change: 0 additions & 1 deletion rosbridge_library/test/internal/test_message_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ def test_assorted_msgs(self):
c.populate_instance(msg, inst2)
self.assertEqual(inst, inst2)

@unittest.skip
def test_int8array(self):
def test_int8_msg(rostype, data):
msg = {"data": data}
Expand Down

0 comments on commit 5398588

Please sign in to comment.