fix: allow non-string objects to be marshalled by write_string (#163)
This commit is contained in:
parent
b67ab0c4b4
commit
46f1d6bbc0
@ -33,14 +33,14 @@ cdef class Marshaller:
|
||||
)
|
||||
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(
|
||||
value_len=cython.uint,
|
||||
signature_len=cython.uint,
|
||||
written=cython.uint,
|
||||
)
|
||||
cdef unsigned int _write_string(self, str value)
|
||||
cdef unsigned int _write_string(self, object value)
|
||||
|
||||
@cython.locals(
|
||||
signature_len=cython.uint,
|
||||
|
||||
@ -179,18 +179,18 @@ class Marshaller:
|
||||
def _marshall(self) -> bytearray:
|
||||
"""Marshalls the body into a byte array"""
|
||||
try:
|
||||
self._construct_buffer()
|
||||
return self._construct_buffer()
|
||||
except KeyError as ex:
|
||||
raise NotImplementedError(f'type is not implemented yet: "{ex.args}"')
|
||||
except error:
|
||||
self.signature_tree.verify(self.body)
|
||||
return self._buf
|
||||
|
||||
def _construct_buffer(self) -> None:
|
||||
def _construct_buffer(self) -> bytearray:
|
||||
self._buf.clear()
|
||||
body = self.body
|
||||
for i, type_ in enumerate(self.signature_tree.types):
|
||||
self._write_single(type_, body[i])
|
||||
return self._buf
|
||||
|
||||
_writers: Dict[
|
||||
str,
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
from enum import Enum
|
||||
from typing import Any, Dict
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
@ -566,3 +566,24 @@ def test_unmarshall_big_endian_message():
|
||||
message = unmarshaller.message
|
||||
unpacked = unpack_variants(message.body)
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user