fix: allow non-string objects to be marshalled by write_string (#163)

This commit is contained in:
J. Nick Koston
2022-11-14 14:31:19 -06:00
committed by GitHub
parent b67ab0c4b4
commit 46f1d6bbc0
3 changed files with 27 additions and 6 deletions

View File

@@ -33,14 +33,14 @@ cdef class Marshaller:
) )
cdef unsigned int _write_boolean(self, object boolean) cdef unsigned int _write_boolean(self, object boolean)
cpdef write_string(self, str value, object _type) cpdef write_string(self, object value, object _type)
@cython.locals( @cython.locals(
value_len=cython.uint, value_len=cython.uint,
signature_len=cython.uint, signature_len=cython.uint,
written=cython.uint, written=cython.uint,
) )
cdef unsigned int _write_string(self, str value) cdef unsigned int _write_string(self, object value)
@cython.locals( @cython.locals(
signature_len=cython.uint, signature_len=cython.uint,

View File

@@ -179,18 +179,18 @@ class Marshaller:
def _marshall(self) -> bytearray: def _marshall(self) -> bytearray:
"""Marshalls the body into a byte array""" """Marshalls the body into a byte array"""
try: try:
self._construct_buffer() return self._construct_buffer()
except KeyError as ex: except KeyError as ex:
raise NotImplementedError(f'type is not implemented yet: "{ex.args}"') raise NotImplementedError(f'type is not implemented yet: "{ex.args}"')
except error: except error:
self.signature_tree.verify(self.body) self.signature_tree.verify(self.body)
return self._buf
def _construct_buffer(self) -> None: def _construct_buffer(self) -> bytearray:
self._buf.clear() self._buf.clear()
body = self.body body = self.body
for i, type_ in enumerate(self.signature_tree.types): for i, type_ in enumerate(self.signature_tree.types):
self._write_single(type_, body[i]) self._write_single(type_, body[i])
return self._buf
_writers: Dict[ _writers: Dict[
str, str,

View File

@@ -1,8 +1,8 @@
import io import io
import json import json
import os import os
from enum import Enum
from typing import Any, Dict from typing import Any, Dict
from unittest.mock import patch
import pytest import pytest
@@ -566,3 +566,24 @@ def test_unmarshall_big_endian_message():
message = unmarshaller.message message = unmarshaller.message
unpacked = unpack_variants(message.body) unpacked = unpack_variants(message.body)
assert unpacked == [42, "zip", "Trusted", True] assert unpacked == [42, "zip", "Trusted", True]
class RaucState(str, Enum):
"""Rauc slot states."""
GOOD = "good"
BAD = "bad"
ACTIVE = "active"
def test_marshalling_enum():
"""Test marshalling an enum."""
msg = Message(
path="/test",
member="test",
signature="s",
body=[RaucState.GOOD],
)
marshalled = msg._marshall(False)
unmarshalled_msg = Unmarshaller(io.BytesIO(marshalled)).unmarshall()
assert unpack_variants(unmarshalled_msg.body)[0] == RaucState.GOOD.value