-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Python serializer wrapper over the generated bindings.
- Loading branch information
1 parent
1db9113
commit 243be14
Showing
5 changed files
with
99 additions
and
5 deletions.
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,27 @@ | ||
%module GsrSerDe | ||
%{ | ||
#include "mutable_byte_array.h" | ||
%} | ||
|
||
#if defined(SWIGPYTHON) | ||
#define SWIG_PYTHON_STRICT_BYTE_CHAR | ||
%typemap(out) mutable_byte_array * %{ | ||
mutable_byte_array *array = $1; | ||
PyObject * obj = PyMemoryView_FromMemory((char *) array->data, array->max_len, PyBUF_READ); | ||
delete_mutable_byte_array($1); | ||
$result = obj; | ||
%} | ||
#endif | ||
|
||
//Methods that map to the C implementation. | ||
typedef struct mutable_byte_array { | ||
%extend { | ||
mutable_byte_array(size_t len); | ||
|
||
~mutable_byte_array(); | ||
|
||
unsigned char * get_data(); | ||
|
||
size_t get_max_len(); | ||
} | ||
} mutable_byte_array; |
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,24 @@ | ||
%module GsrSerDe | ||
%{ | ||
#include "read_only_byte_array.h" | ||
%} | ||
|
||
#if defined(SWIGPYTHON) | ||
%include "pybuffer.i" | ||
#endif | ||
|
||
typedef struct read_only_byte_array { | ||
%extend { | ||
//PyBuffer provides type-maps for accepting Python bytes. | ||
#if defined(SWIGPYTHON) | ||
%pybuffer_binary(unsigned char * data, size_t len); | ||
#endif | ||
read_only_byte_array(unsigned char *data, size_t len); | ||
|
||
~read_only_byte_array(); | ||
|
||
unsigned char * get_data(); | ||
|
||
size_t get_len(); | ||
} | ||
} read_only_byte_array; |
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,35 @@ | ||
from .GsrSerDe import * | ||
|
||
""" | ||
TODO: Add documentation and tests. | ||
""" | ||
|
||
|
||
class GlueSchemaRegistrySchema: | ||
def __init__(self, schema_name, schema_def, data_format): | ||
self.gsr_schema = glue_schema_registry_schema(schema_name, schema_def, data_format) | ||
|
||
def __del__(self): | ||
del self.gsr_schema | ||
|
||
def schema_name(self): | ||
return self.gsr_schema.get_schema_name() | ||
|
||
def data_format(self): | ||
return self.gsr_schema.get_data_format() | ||
|
||
def schema_def(self): | ||
return self.gsr_schema.get_schema_def() | ||
|
||
|
||
class GlueSchemaRegistrySerializer: | ||
def __init__(self): | ||
self.serializer = glue_schema_registry_serializer() | ||
|
||
def __del__(self): | ||
del self.serializer | ||
|
||
def encode(self, schema: GlueSchemaRegistrySchema, byte_arr: bytes) -> bytes: | ||
ba = read_only_byte_array(byte_arr) | ||
encoded_byte_buffer = self.serializer.encode(ba, schema.gsr_schema) | ||
return encoded_byte_buffer |
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,3 @@ | ||
from .PyGsrSerDe import * | ||
|
||
__all__ = ['GlueSchemaRegistrySchema', 'GlueSchemaRegistrySerializer'] |