Created
June 24, 2013 22:12
-
-
Save JensRantil/5854146 to your computer and use it in GitHub Desktop.
Custom schematics type.
This file contains hidden or 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
class EventId(types.BaseType): | |
"""Schema type for aggregate event id.""" | |
DIVIDER = ':' | |
def to_primitive(self, value): | |
if not isinstance(value, tuple): | |
raise ValidationError('{0} is not a tuple'.format(value)) | |
if len(value) != 2: | |
raise ValidationError('{0} does not have two items'.format(value)) | |
prim = EventId.DIVIDER.join(str(v) for v in value) | |
return prim | |
def convert(self, value): | |
if not isinstance(value, basestring): | |
raise ValidationError('{0} is not a string'.format(value)) | |
pieces = value.split(EventId.DIVIDER) | |
if len(pieces) != 2: | |
raise ValidationError(("{0} had more than one" | |
" dividers.").format(value)) | |
datastruct = tuple(pieces) | |
return datastruct |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment