Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<feat>(comm): Add store_hex option #21

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions inpynamodb/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,25 @@ def as_dict(self, attributes_to_get=None):

class UUIDAttribute(UnicodeAttribute):
def __init__(self, hash_key=False, range_key=False, null=None, defaultable=True,
default=None, uuid_version=1, attr_name=None):
default=None, uuid_version=1, attr_name=None, store_hex=False):
"""
:param hash_key: Indicates that this attribute is hash key of model.
:param range_key: Indicates that this attribute is range key of model.
:param null: Indicate this attribute is nullable.
:param default: Default value of this attribute.
If auto == True, this value will be ignored because UUID will be generated as default.
:param uuid_version: UUID version which this attribute will use. Only supports 1 and 4.
:param store_hex: If true, store uuid in hex form
"""
self.uuid_version = uuid_version
self.store_hex = store_hex

super(UUIDAttribute, self).__init__(hash_key=hash_key, range_key=range_key, defaultable=defaultable,
null=null, default=default, attr_name=attr_name)

def serialize(self, value):
if isinstance(value, uuid.UUID):
value_str = str(value)
value_str = value.hex if self.store_hex else str(value)
try:
uuid.UUID(value_str, version=self.uuid_version)
return super(UUIDAttribute, self).serialize(value_str)
Expand Down