1. Ludia
  2. Untitled project
  3. dynamodb-mapper


Max Noel  committed e0c447c

* Split DynamoDBModel.to_dict() into to_db_dict() and to_json_dict(), since everyone (and most importantly Ludia) is doing JSON serialization, which doesn't have sets.

  • Participants
  • Parent commits 3066d7b
  • Branches default
  • Tags 1.1.0

Comments (0)

Files changed (3)

File dynamodb_mapper/model.py

View file
     To redefine serialization/deserialization semantics (e.g. to have more
     complex schemas, like auto-serialized JSON data structures), override the
-    from_dict (deserialization) and to_dict (serialization) methods.
+    from_dict (deserialization) and to_db_dict (serialization) methods.
     *Important implementation note regarding sets:* DynamoDB can't store empty
     sets. Therefore, since we have schema information available to us, we're
         for (name, type_) in self.__schema__.iteritems():
             setattr(self, name, type_())
-    def to_dict(self):
-        """Return a dict representation of the object,
-        according to the class's schema.
+    def to_db_dict(self):
+        """Return a dict representation of the object according to the class's
+        schema, suitable for direct storage in DynamoDB.
+        This means the values must all be numbers, strings or sets thereof.
         return {name: getattr(self, name) for name in self.__schema__}
+    def to_json_dict(self):
+        """Return a dict representation of the object, suitable for JSON
+        serialization.
+        This means the values must all be valid JSON object types
+        (in particular, sets must be converted to lists), but types not
+        suitable for DynamoDB (e.g. nested data structures) may be used.
+        Note that this method is never used for interaction with the database
+        (:meth:`to_db_dict` is).
+        """
+        out = {}
+        for name in self.__schema__:
+            value = getattr(self, name)
+            if isinstance(value, (set, frozenset)):
+                out[name] = list(value)
+            else:
+                out[name] = value
+        return out
     def _save_autoincrement_hash_key(self, item):
         """Compute an autoincremented hash_key for an item and save it to the DB.
         # Yes, that part is horrible. DynamoDB can't store empty sets,
         # so we're representing them as missing attributes on the DB side.
         item_data = {
-            key: value for (key, value) in self.to_dict().iteritems() if (
+            key: value for (key, value) in self.to_db_dict().iteritems() if (
                 value or not isinstance(value, (set, frozenset)))
         item = Item(table, attrs=item_data)

File dynamodb_mapper/tests/test_model.py

View file
         self.assertEquals(d.id, 0)
         self.assertEquals(d.name, u"")
-        d_dict = d.to_dict()
+        d_dict = d.to_db_dict()
         self.assertEquals(d_dict["id"], d.id)
         self.assertEquals(d_dict["name"], d.name)

File setup.cfg

View file
 name = dynamodb-mapper
-version = 1.0.2
+version = 1.1.0
 summary = Object mapper for Amazon DynamoDB
 description_file = README.rst
 author = Max Noel