module documentation
The Encoder: one message dataclass becomes one live ROS2 message instance.
The mirror of opensmi.ros2.runtime._decode, so that no rclpy object crosses the seam.
It runs on the event loop. Each conversion inverts one the Decoder made, and a wrong shape
aborts the process instead of raising:
- a nested message becomes its generated interface class again;
- a tuple becomes a list, which the generated constructors accept for every container;
- bytes stays bytes for a uint8 container and becomes a list of one-byte values for an octet one;
- a byte scalar becomes a one-byte bytes again, because handing the int back dies on Assertion 'PyBytes_Check(field)' failed (pinned by a Jazzy test).
The Encoder does not validate. opensmi.ros2.runtime.guard does that beforehand, as a pure
function that needs no ROS2 installation to test.
| Function | encode |
Convert one message dataclass into the rclpy object its ROS2 type is. |
| Function | _as |
Return a byte scalar as the one-byte bytes rclpy uses for IDL octet. |
| Function | _bytes |
Return how a byte container is handed over: uint8 as a buffer, byte as a list of bytes. |
| Function | _element |
Return the encoder for a scalar, or None when the value already needs no work. |
| Function | _field |
Return the function that turns one decoded field value back into what rclpy wants. |
| Function | _plan |
Return the interface class and the field-name and field-encoder pairs, in field order. |
Convert one message dataclass into the rclpy object its ROS2 type is.
| Parameters | |
message:Message | The payload, already checked by
opensmi.ros2.runtime.guard.require_sendable. |
| Returns | |
Any | Undocumented |
| Raises | |
MissingInterfacePackageError | When the type's interface package is not installed, which a declared type is refused for at init and an encoded one cannot reach. |
def _element_encoder(element:
ElementType, nested: type[ Message] | None) -> Callable[ [ Any], Any] | None:
¶
Return the encoder for a scalar, or None when the value already needs no work.
@cache
def _plan(message_type: type[ Message]) -> tuple[ Any, tuple[ tuple[ str, Callable[ [ Any], Any]], ...]]:
¶
Return the interface class and the field-name and field-encoder pairs, in field order.