Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions pulsar/schema/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@
from enum import Enum, EnumMeta


def _string_representation(x):
if hasattr(x, "__name__"):
return x.__name__
else:
return str(x)


def _check_record_or_field(x):
if (type(x) is type and not issubclass(x, Record)) \
and not isinstance(x, Field):
raise Exception('Argument ' + x + ' is not a Record or a Field')
raise Exception('Argument ' + _string_representation(x) + ' is not a Record or a Field')


class RecordMeta(type):
Expand Down Expand Up @@ -188,7 +195,7 @@ def validate_type(self, name, val):

if not isinstance(val, self.__class__):
raise TypeError("Invalid type '%s' for sub-record field '%s'. Expected: %s" % (
type(val), name, self.__class__))
type(val), name, _string_representation(self.__class__)))
return val

def default(self):
Expand Down Expand Up @@ -222,7 +229,7 @@ def validate_type(self, name, val):
return self.default()

if type(val) != self.python_type():
raise TypeError("Invalid type '%s' for field '%s'. Expected: %s" % (type(val), name, self.python_type()))
raise TypeError("Invalid type '%s' for field '%s'. Expected: %s" % (type(val), name, _string_representation(self.python_type())))
return val

def schema(self):
Expand Down Expand Up @@ -368,7 +375,7 @@ def default(self):
class CustomEnum(Field):
def __init__(self, enum_type, default=None, required=False, required_default=False):
if not issubclass(enum_type, Enum):
raise Exception(enum_type + " is not a valid Enum type")
raise Exception(_string_representation(enum_type) + " is not a valid Enum type")
self.enum_type = enum_type
self.values = {}
for x in enum_type.__members__.values():
Expand Down Expand Up @@ -400,7 +407,7 @@ def validate_type(self, name, val):
raise TypeError(
"Invalid enum value '%s' for field '%s'. Expected: %s" % (val, name, self.values.keys()))
elif type(val) != self.python_type():
raise TypeError("Invalid type '%s' for field '%s'. Expected: %s" % (type(val), name, self.python_type()))
raise TypeError("Invalid type '%s' for field '%s'. Expected: %s" % (type(val), name, _string_representation(self.python_type())))
else:
return val

Expand Down Expand Up @@ -445,7 +452,7 @@ def validate_type(self, name, val):
for x in val:
if type(x) != self.array_type.python_type():
raise TypeError('Array field ' + name + ' items should all be of type ' +
self.array_type.type())
_string_representation(self.array_type.type()))
return val

def schema(self):
Expand Down Expand Up @@ -488,7 +495,7 @@ def validate_type(self, name, val):
raise TypeError('Map keys for field ' + name + ' should all be strings')
if type(v) != self.value_type.python_type():
raise TypeError('Map values for field ' + name + ' should all be of type '
+ self.value_type.python_type())
+ _string_representation(self.value_type.python_type()))

return val

Expand Down