PynamoDB icon indicating copy to clipboard operation
PynamoDB copied to clipboard

Don't appear to be able to create a dynamic map of sets

Open john-tipper opened this issue 2 years ago • 0 comments

I have a DynamoDB table where I want the items to have a field that is a map of a set of strings. I want to create this from a top level map like this:

    kv_by_aws_account: Dict[str, Dict[str, Set[str]]] = {
        "111111111111": {
            "k1": {
                "v1",
                "v2",
                "v3"
            },
            "k2": {
                "v4",
                "v5"
            }
        },
        "222222222222": {
            "k1": {
                "v1",
                "v2",
            },
            "k3": {
                "v3",
            }
        }
    }

I want this to create 2 items in my table:

    pk             sk            values_by_key
    
    AWS_ACCOUNT    111111111111  {"k1": ["v1", "v2", "v3"], "k2": ["v4", "v5"]}
    AWS_ACCOUNT    222222222222  {"k1": ["v1", "v2"], "k3": ["v3"]}

I am using a model that looks like this:

    class MyModel(Model):
        class Meta:
            table_name = "MyTable"
    
        pk = UnicodeAttribute(hash_key=True)
        sk = UnicodeAttribute(range_key=True)
    
        key_values = DynamicMapAttribute()

I create my model like this:

models = [MyModel("AWS_ACCOUNT", acc, key_values=DynamicMapAttribute(**kv_by_aws_account[acc])) for acc in kv_by_aws_account.keys()]

However, I just get a map of a list of strings, instead of a map of string sets. Is there a way to get a dynamic map of sets or is this a limitation of Pynamodb?

john-tipper avatar Jan 19 '23 21:01 john-tipper