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

Custom attribute with both NUMBER and BOOL values #1214

Open
Shogo1222 opened this issue Jan 9, 2024 · 1 comment
Open

Custom attribute with both NUMBER and BOOL values #1214

Shogo1222 opened this issue Jan 9, 2024 · 1 comment

Comments

@Shogo1222
Copy link

Shogo1222 commented Jan 9, 2024

Hi,

How can I create a custom attribute that contains both number and BOOL values?
NUMBER may have been mixed up with past data in the same attribute in DynamoDB.
I would like to read all the numbers and BOOL values in DynamoDB and convert them to BOOL.

However, the attr_type must be specified, and it can only be either BOOL or number.
I would appreciate it if you could tell me how to resolve this issue.

DynamoDB:

{'value': {'N': '0'}}
{'value': {'BOOL': '0'}}

CustomAttribute:

class CustomAttribute(Attribute):
    def serialize(self, value):
        if value is None:
            return None
        elif value:
            return True
        else:
            return False

    def deserialize(self, value):
        if value is None:
            return None
        elif isinstance(value, bool):
            return bool(value)
        elif isinstance(value, str):
            return bool(json.loads(value))

if self.attr_type not in value:

I see here that it may not be supported, but if there is another way, please let me know.

Thanks.

@ikonst
Copy link
Contributor

ikonst commented Jan 18, 2024

I haven't tested it out, but looking at the source, will overriding BooleanAttribute's get_value work?

class CustomAttribute(BooleanAttribute):
  def get_value(self, value: Dict[str, Any]) -> Any:
      if 'N' in value:
         value = {'B': bool(value['N'])}
      return super().get_value(value)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants