Skip to content

Commit

Permalink
Support for defining CompositeTypes with custom inner types.
Browse files Browse the repository at this point in the history
E.g.: Given the following custom type:

Class MyDateType(CassandraType):
	@staticmethod
	def pack(v, *args, **kwargs):
		"""custom pack implementation"""

	@staticmethod
	def unpack(v, *args, **kwargs):
		"""custom unpack implementation"""

You can now do the following:

	c = CompositeType(MyDateType, LongType)

And the following:

	cf = ColumnFamily(pool, 'MyColumnFamily')
	cf.column_name_class = CompositeType(MyDateType, LongType)
  • Loading branch information
kylemcc committed Mar 14, 2012
1 parent 32e0d42 commit d3333ec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pycassa/marshal.py
Expand Up @@ -74,8 +74,13 @@ def _to_timestamp(v):
converted = v * 1e3
return long(converted)

def get_composite_packer(typestr):
packers = map(packer_for, _get_inner_types(typestr))
def get_composite_packer(typestr=None, composite_type=None):
assert (typestr or composite_type), "Must provide typestr or " + \
"CompositeType instance"
if typestr:
packers = map(packer_for, _get_inner_types(typestr))
elif composite_type:
packers = [c.pack for c in composite_type.components]

if _have_struct:
len_packer = _short_packer.pack
Expand Down Expand Up @@ -104,8 +109,13 @@ def pack_composite(items, slice_start=None):

return pack_composite

def get_composite_unpacker(typestr):
unpackers = map(unpacker_for, _get_inner_types(typestr))
def get_composite_unpacker(typestr=None, composite_type=None):
assert (typestr or composite_type), "Must provide typestr or " + \
"CompositeType instance"
if typestr:
unpackers = map(unpacker_for, _get_inner_types(typestr))
elif composite_type:
unpackers = [c.unpack for c in composite_type.components]

if _have_struct:
len_unpacker = lambda v: _short_packer.unpack(v)[0]
Expand Down
8 changes: 8 additions & 0 deletions pycassa/types.py
Expand Up @@ -226,3 +226,11 @@ def __init__(self, *components):

def __str__(self):
return "CompositeType(" + ", ".join(map(str, self.components)) + ")"

@property
def pack(self):
return marshal.get_composite_packer(composite_type=self)

@property
def unpack(self):
return marshal.get_composite_unpacker(composite_type=self)

0 comments on commit d3333ec

Please sign in to comment.