-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathschema_protobuf.py
More file actions
56 lines (42 loc) · 1.79 KB
/
Copy pathschema_protobuf.py
File metadata and controls
56 lines (42 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Protobuf — suite Data Model v2 messages (schemas/v2)."""
from __future__ import annotations
import io
from typing import Any
from .base import Serializer
from ..data_v2 import protobuf_bridge
class ProtobufSerializer(Serializer):
package_name = "protobuf"
native_kind = "message"
stream_mode = "adapted"
def __init__(self) -> None:
super().__init__()
self._msg_cls = None
@property
def name(self) -> str:
return "protobuf"
def supports(self, test_data_name: str) -> bool:
return test_data_name in {
"message", "document", "telemetry", "strings", "event",
} and protobuf_bridge.available()
def prepare(self, test_data_name: str, test_data_type: type) -> None:
super().prepare(test_data_name, test_data_type)
self._msg_cls = protobuf_bridge.message_class_for(test_data_name, batch=False)
def prepare_data(self, obj: Any, test_data_name: str, test_data_type: type) -> Any:
batch = isinstance(obj, list)
self._msg_cls = protobuf_bridge.message_class_for(test_data_name, batch=batch)
if self._msg_cls is None:
raise TypeError(f"no protobuf mapping for {test_data_name}")
return protobuf_bridge.to_pb(obj)
def serialize_bytes(self, obj: Any) -> bytes:
return obj.SerializeToString()
def deserialize_bytes(self, data: bytes) -> Any:
if self._msg_cls is None:
raise RuntimeError("prepare required")
msg = self._msg_cls()
msg.ParseFromString(data)
return msg
def serialize_stream(self, obj: Any, stream: io.BytesIO) -> None:
stream.write(self.serialize_bytes(obj))
def deserialize_stream(self, stream: io.BytesIO) -> Any:
stream.seek(0)
return self.deserialize_bytes(stream.read())