-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b20c0c
commit 9d006ae
Showing
4 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from typing import Union, Mapping, Optional, Any, Iterable | ||
|
||
from io import BytesIO | ||
|
||
from fastavro import schemaless_reader, schemaless_writer, parse_schema | ||
from fastavro.types import Schema | ||
|
||
from .base import Serializer, Deserializer, SerializationContext | ||
from .exceptions import SerializationError | ||
|
||
__all__ = ("AvroSerializer", "AvroDeserializer") | ||
|
||
|
||
class AvroSerializer(Serializer): | ||
def __init__(self, schema: Schema, **kwargs): | ||
""" | ||
Serializer that returns data in Avro format. | ||
Additional keyword arguments are passed directly to fastavro [schemaless_writer](https://fastavro.readthedocs.io/en/latest/writer.html#fastavro._write_py.schemaless_writer) method. | ||
:param schema: The avro schema. | ||
""" | ||
self._schema = parse_schema(schema) | ||
self._kwargs = kwargs or {} | ||
|
||
def __call__(self, value: Any, ctx: SerializationContext) -> bytes: | ||
data = BytesIO() | ||
|
||
try: | ||
schemaless_writer(data, self._schema, value, self._kwargs) | ||
except ValueError as exc: | ||
raise SerializationError(str(exc)) from exc | ||
|
||
result = data.getvalue() | ||
data.close() | ||
return result | ||
|
||
|
||
class AvroDeserializer(Deserializer): | ||
def __init__(self, schema: Schema, **kwargs): | ||
""" | ||
Deserializer that parses data from Avro. | ||
Additional keyword arguments are passed directly to fastavro [schemaless_reader](https://fastavro.readthedocs.io/en/latest/reader.html#fastavro._read_py.schemaless_reader) method. | ||
:param schema: The Avro schema. | ||
""" | ||
super().__init__() | ||
self._schema = parse_schema(schema) | ||
self._kwargs = kwargs or {} | ||
|
||
def __call__( | ||
self, value: bytes, ctx: SerializationContext | ||
) -> Union[Iterable[Mapping], Mapping]: | ||
try: | ||
return schemaless_reader(BytesIO(value), self._schema, **self._kwargs) | ||
except EOFError as exc: | ||
raise SerializationError(str(exc)) from exc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters