def register_enum_type(type_class, _ddb_data_type): u""" Enum 型の data_type を登録する """ class EnumType(TypeDefinition): u""" Enum Type for {} """.format(type_class.__name__) data_type = type_class aliases = [type_class.__name__] ddb_data_type = _ddb_data_type def ddb_dump(self, value): u""" DynamoDB へ書き出す際の変換 """ return value.value def ddb_load(self, value): u""" DynamoDB から読み込む際の変換 """ return type_class(value) register_type(EnumType)
ddb_data_type = BINARY def ddb_dump(self, value): dumped = json.dumps(value) if isinstance(dumped, six.text_type): dumped = dumped.encode('ascii') return Binary(zlib.compress(dumped)) def ddb_load(self, value): decompressed = zlib.decompress(value.value) if isinstance(decompressed, six.binary_type): decompressed = decompressed.decode('ascii') return json.loads(decompressed) register_type(CompressedDict) class Widget(Model): """ Model for testing default field values """ __metadata__ = { 'global_indexes': [ GlobalIndex('gindex', 'string2', 'num'), ], } string = Field(hash_key=True) string2 = Field() num = Field(data_type=NUMBER) binary = Field(data_type=BINARY, coerce=True) str_set = Field(data_type=STRING_SET)
class IdentityType(TypeDefinition): u""" Identity Type """ data_type = Identity aliases = ['identity'] ddb_data_type = STRING def ddb_dump(self, value): u""" DynamoDB へ書き出す際の変換 """ return value.hex def ddb_load(self, value): u""" DynamoDB から読み込む際の変換 """ return Identity(value) register_type(IdentityType) def register_enum_type(type_class, _ddb_data_type): u""" Enum 型の data_type を登録する """ class EnumType(TypeDefinition): u""" Enum Type for {} """.format(type_class.__name__) data_type = type_class aliases = [type_class.__name__] ddb_data_type = _ddb_data_type def ddb_dump(self, value): u""" DynamoDB へ書き出す際の変換 """ return value.value
""" Custom data type that prepends 'custom:' before the string """ data_type = 'customtype' ddb_data_type = STRING def coerce(self, value, force): return value def ddb_dump(self, value): return 'custom:' + value def ddb_load(self, value): return value[len('custom:'):] register_type(CustomDataType) class CustomPkey(Model): """ Test model with datetime primary key """ hkey = Field(data_type=CustomDataType, hash_key=True) text = Field() class TestComposite(DynamoSystemTest): """ Tests for composite fields """ models = [Widget, Post] def test_composite_field(self): """ Composite fields should be a combination of their components """ w = Widget('a', 'b', 1)