pulsar.schema.schema

  1#
  2# Licensed to the Apache Software Foundation (ASF) under one
  3# or more contributor license agreements.  See the NOTICE file
  4# distributed with this work for additional information
  5# regarding copyright ownership.  The ASF licenses this file
  6# to you under the Apache License, Version 2.0 (the
  7# "License"); you may not use this file except in compliance
  8# with the License.  You may obtain a copy of the License at
  9#
 10#   http://www.apache.org/licenses/LICENSE-2.0
 11#
 12# Unless required by applicable law or agreed to in writing,
 13# software distributed under the License is distributed on an
 14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15# KIND, either express or implied.  See the License for the
 16# specific language governing permissions and limitations
 17# under the License.
 18#
 19
 20
 21from abc import abstractmethod
 22import json
 23import _pulsar
 24import enum
 25
 26
 27class Schema(object):
 28    def __init__(self, record_cls, schema_type, schema_definition, schema_name):
 29        self._record_cls = record_cls
 30        self._schema_info = _pulsar.SchemaInfo(schema_type, schema_name,
 31                                               json.dumps(schema_definition, indent=True))
 32
 33    @abstractmethod
 34    def encode(self, obj):
 35        pass
 36
 37    @abstractmethod
 38    def decode(self, data):
 39        pass
 40
 41    def schema_info(self):
 42        return self._schema_info
 43
 44    def _validate_object_type(self, obj):
 45        if not isinstance(obj, self._record_cls):
 46            raise TypeError('Invalid record obj of type ' + str(type(obj))
 47                            + ' - expected type is ' + str(self._record_cls))
 48
 49
 50class BytesSchema(Schema):
 51    def __init__(self):
 52        super(BytesSchema, self).__init__(bytes, _pulsar.SchemaType.BYTES, None, 'BYTES')
 53
 54    def encode(self, data):
 55        self._validate_object_type(data)
 56        return data
 57
 58    def decode(self, data):
 59        return data
 60
 61
 62class StringSchema(Schema):
 63    def __init__(self):
 64        super(StringSchema, self).__init__(str, _pulsar.SchemaType.STRING, None, 'STRING')
 65
 66    def encode(self, obj):
 67        self._validate_object_type(obj)
 68        return obj.encode('utf-8')
 69
 70    def decode(self, data):
 71        return data.decode('utf-8')
 72
 73
 74class JsonSchema(Schema):
 75
 76    def __init__(self, record_cls):
 77        super(JsonSchema, self).__init__(record_cls, _pulsar.SchemaType.JSON,
 78                                         record_cls.schema(), 'JSON')
 79
 80    def _get_serialized_value(self, o):
 81        if isinstance(o, enum.Enum):
 82            return o.value
 83        else:
 84            return o.__dict__
 85
 86    def encode(self, obj):
 87        self._validate_object_type(obj)
 88        # Copy the dict of the object as to not modify the provided object via the reference provided
 89        data = obj.__dict__.copy()
 90        if '_default' in data:
 91            del data['_default']
 92        if '_required' in data:
 93            del data['_required']
 94        if '_required_default' in data:
 95            del data['_required_default']
 96
 97        return json.dumps(data, default=self._get_serialized_value, indent=True).encode('utf-8')
 98
 99    def decode(self, data):
100        return self._record_cls(**json.loads(data))
class Schema:
28class Schema(object):
29    def __init__(self, record_cls, schema_type, schema_definition, schema_name):
30        self._record_cls = record_cls
31        self._schema_info = _pulsar.SchemaInfo(schema_type, schema_name,
32                                               json.dumps(schema_definition, indent=True))
33
34    @abstractmethod
35    def encode(self, obj):
36        pass
37
38    @abstractmethod
39    def decode(self, data):
40        pass
41
42    def schema_info(self):
43        return self._schema_info
44
45    def _validate_object_type(self, obj):
46        if not isinstance(obj, self._record_cls):
47            raise TypeError('Invalid record obj of type ' + str(type(obj))
48                            + ' - expected type is ' + str(self._record_cls))
Schema(record_cls, schema_type, schema_definition, schema_name)
29    def __init__(self, record_cls, schema_type, schema_definition, schema_name):
30        self._record_cls = record_cls
31        self._schema_info = _pulsar.SchemaInfo(schema_type, schema_name,
32                                               json.dumps(schema_definition, indent=True))
@abstractmethod
def encode(self, obj)
34    @abstractmethod
35    def encode(self, obj):
36        pass
@abstractmethod
def decode(self, data)
38    @abstractmethod
39    def decode(self, data):
40        pass
def schema_info(self)
42    def schema_info(self):
43        return self._schema_info
class BytesSchema(Schema):
51class BytesSchema(Schema):
52    def __init__(self):
53        super(BytesSchema, self).__init__(bytes, _pulsar.SchemaType.BYTES, None, 'BYTES')
54
55    def encode(self, data):
56        self._validate_object_type(data)
57        return data
58
59    def decode(self, data):
60        return data
BytesSchema()
52    def __init__(self):
53        super(BytesSchema, self).__init__(bytes, _pulsar.SchemaType.BYTES, None, 'BYTES')
def encode(self, data)
55    def encode(self, data):
56        self._validate_object_type(data)
57        return data
def decode(self, data)
59    def decode(self, data):
60        return data
Inherited Members
Schema
schema_info
class StringSchema(Schema):
63class StringSchema(Schema):
64    def __init__(self):
65        super(StringSchema, self).__init__(str, _pulsar.SchemaType.STRING, None, 'STRING')
66
67    def encode(self, obj):
68        self._validate_object_type(obj)
69        return obj.encode('utf-8')
70
71    def decode(self, data):
72        return data.decode('utf-8')
StringSchema()
64    def __init__(self):
65        super(StringSchema, self).__init__(str, _pulsar.SchemaType.STRING, None, 'STRING')
def encode(self, obj)
67    def encode(self, obj):
68        self._validate_object_type(obj)
69        return obj.encode('utf-8')
def decode(self, data)
71    def decode(self, data):
72        return data.decode('utf-8')
Inherited Members
Schema
schema_info
class JsonSchema(Schema):
 75class JsonSchema(Schema):
 76
 77    def __init__(self, record_cls):
 78        super(JsonSchema, self).__init__(record_cls, _pulsar.SchemaType.JSON,
 79                                         record_cls.schema(), 'JSON')
 80
 81    def _get_serialized_value(self, o):
 82        if isinstance(o, enum.Enum):
 83            return o.value
 84        else:
 85            return o.__dict__
 86
 87    def encode(self, obj):
 88        self._validate_object_type(obj)
 89        # Copy the dict of the object as to not modify the provided object via the reference provided
 90        data = obj.__dict__.copy()
 91        if '_default' in data:
 92            del data['_default']
 93        if '_required' in data:
 94            del data['_required']
 95        if '_required_default' in data:
 96            del data['_required_default']
 97
 98        return json.dumps(data, default=self._get_serialized_value, indent=True).encode('utf-8')
 99
100    def decode(self, data):
101        return self._record_cls(**json.loads(data))
JsonSchema(record_cls)
77    def __init__(self, record_cls):
78        super(JsonSchema, self).__init__(record_cls, _pulsar.SchemaType.JSON,
79                                         record_cls.schema(), 'JSON')
def encode(self, obj)
87    def encode(self, obj):
88        self._validate_object_type(obj)
89        # Copy the dict of the object as to not modify the provided object via the reference provided
90        data = obj.__dict__.copy()
91        if '_default' in data:
92            del data['_default']
93        if '_required' in data:
94            del data['_required']
95        if '_required_default' in data:
96            del data['_required_default']
97
98        return json.dumps(data, default=self._get_serialized_value, indent=True).encode('utf-8')
def decode(self, data)
100    def decode(self, data):
101        return self._record_cls(**json.loads(data))
Inherited Members
Schema
schema_info