Skip to content
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

feat: allow serializer overriding #320

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,4 @@ pyo3 = { version = "0.21.2", features = [
"experimental-declarative-modules",
] }
validated_struct = "2.1.0"
zenoh = { version = "1.0.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main", features = ["unstable",
"internal",
], default-features = false }
zenoh = { version = "1.0.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main", features = ["unstable", "internal"], default-features = false }
2 changes: 1 addition & 1 deletion examples/z_pub_thr.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def main(conf: zenoh.Config, payload_size: int):
data = bytearray()
for i in range(0, payload_size):
data.append(i % 10)
data = zenoh.ZBytes(bytes(data))
data = zenoh.ZBytes(data)
congestion_control = zenoh.CongestionControl.BLOCK

with zenoh.open(conf) as session:
Expand Down
394 changes: 242 additions & 152 deletions src/bytes.rs

Large diffs are not rendered by default.

19 changes: 0 additions & 19 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,6 @@ impl<I: Iterator<Item = Result<T, E>>, T, E> Iterator for TryProcessIter<'_, I,
}
}

pub(crate) fn try_process<I, T, E, R>(
iter: I,
process: impl FnOnce(TryProcessIter<'_, I::IntoIter, E>) -> R,
) -> Result<R, E>
where
I: IntoIterator<Item = Result<T, E>>,
{
let mut error = None;
let iter = TryProcessIter {
iter: iter.into_iter(),
error: &mut error,
};
let res = process(iter);
if let Some(err) = error {
return Err(err);
}
Ok(res)
}

pub(crate) fn short_type_name<T: ?Sized>() -> &'static str {
let name = std::any::type_name::<T>();
name.rsplit_once("::").map_or(name, |(_, name)| name)
Expand Down
30 changes: 30 additions & 0 deletions tests/test_override_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Copyright (c) 2024 ZettaScale Technology
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# ZettaScale Zenoh Team, <[email protected]>
#

from zenoh import ZBytes, deserializer, serializer


def test_override_serializer():
assert ZBytes.serialize(42) != ZBytes.serialize("42")

@deserializer
def deserialize_int_from_str(zbytes: ZBytes) -> int:
return int(zbytes.deserialize(str))

@serializer
def serialize_int_as_str(foo: int) -> ZBytes:
return ZBytes.serialize(str(foo))

assert ZBytes.serialize(42).deserialize(int) == 42
assert ZBytes.serialize(42) == ZBytes.serialize("42")
11 changes: 9 additions & 2 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,28 @@
from zenoh import ZBytes, deserializer, serializer

default_serializer_tests = [
(ZBytes, ZBytes.serialize(b"foo")),
(bytes, b"foo"),
(bytearray, bytearray(b"foo")),
(str, "foo"),
(int, 42),
(float, 0.5),
(bool, True),
(ZBytes, ZBytes.serialize(b"foo")),
(list, [ZBytes.serialize(0), ZBytes.serialize(1)]),
(tuple, (ZBytes.serialize(0), ZBytes.serialize(1))),
(dict, {ZBytes.serialize("foo"): ZBytes.serialize("bar")}),
(set, {ZBytes.serialize(0), ZBytes.serialize(1)}),
(frozenset, frozenset([ZBytes.serialize(0), ZBytes.serialize(1)])),
]
if sys.version_info >= (3, 9):
default_serializer_tests = [
*default_serializer_tests,
(list[int], [0, 1, 2]),
(dict[str, str], {"foo": "bar"}),
(tuple[int, int], (0, 1)),
(tuple[int, ...], (0, 1, 2)),
(dict[str, str], {"foo": "bar"}),
(set[int], {0, 1, 2}),
(frozenset[int], frozenset([0, 1, 2])),
(list[tuple[int, int]], [(0, 1), (2, 3)]),
]

Expand Down
2 changes: 1 addition & 1 deletion zenoh/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ _IntoWhatAmIMatcher = WhatAmIMatcher | str
class ZBytes:
"""ZBytes contains the serialized bytes of user data."""

def __new__(cls, bytes: bytes = None) -> Self: ...
def __new__(cls, bytes: bytes | bytearray = None) -> Self: ...
@classmethod
def serialize(cls, obj: Any) -> Self:
"""Serialize object according to its type,
Expand Down
Loading