Skip to content

Commit

Permalink
Add Python serializer wrapper over the generated bindings.
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktooth committed May 16, 2022
1 parent 1db9113 commit 243be14
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 5 deletions.
15 changes: 10 additions & 5 deletions native-schema-registry/c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

This module provides a C language based API for the schema registry serializer / de-serializers.

##Build
## Build
We use CMake to build the targets in this module.

###Compile
### Compile
```asm
#Run in c directory
Expand All @@ -14,17 +14,22 @@ cmake -S. -Bbuild
cd build
cmake --build .
```
###Testing
### Testing
```asm
ctest
#Re-run failed tests with verbose output
ctest --rerun-failed --output-on-failure
```

###Sanitizers
### Sanitizers
We use address,leak sanitizers to detect memory leaks and any potential issues during build. As of now, they don't work well on OSX

###Platform Support
### Platform Support

TBD

## License

**Project License** [Apache License Version 2.0](https://github.com/awslabs/aws-glue-schema-registry/blob/master/LICENSE.txt)

N.B.: Although this repository is released under the Apache-2.0 license, its build dependencies include the third party Swig project. The Swig project's licensing includes the GPL-3.0 license.
27 changes: 27 additions & 0 deletions native-schema-registry/c/src/swig/mutable_byte_array.i
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;
24 changes: 24 additions & 0 deletions native-schema-registry/c/src/swig/read_only_byte_array.i
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;
35 changes: 35 additions & 0 deletions native-schema-registry/python/PyGsrSerDe/PyGsrSerDe.py
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
3 changes: 3 additions & 0 deletions native-schema-registry/python/PyGsrSerDe/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .PyGsrSerDe import *

__all__ = ['GlueSchemaRegistrySchema', 'GlueSchemaRegistrySerializer']

0 comments on commit 243be14

Please sign in to comment.