Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1146)

Unified Diff: mojo/public/python/mojo/bindings/descriptor.py

Issue 643023003: mojo: Add maps to python bindings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | mojo/public/tools/bindings/generators/mojom_python_generator.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/python/mojo/bindings/descriptor.py
diff --git a/mojo/public/python/mojo/bindings/descriptor.py b/mojo/public/python/mojo/bindings/descriptor.py
index 4e0ab7e91bb05a1d8118b37d35814d13999f211e..0c97c06e8370ae91679b38e10969e17d38b7434d 100644
--- a/mojo/public/python/mojo/bindings/descriptor.py
+++ b/mojo/public/python/mojo/bindings/descriptor.py
@@ -10,6 +10,7 @@ import array
import itertools
import struct
+import mojo.bindings.reflection as reflection
import mojo.bindings.serialization as serialization
# pylint: disable=E0611,F0401
@@ -404,6 +405,54 @@ class StructType(PointerType):
return self.struct_type.Deserialize(data, handles)
+class MapType(SerializableType):
+ """Type objects for maps."""
+
+ def __init__(self, key_type, value_type, nullable=False):
+ self._key_type = key_type
+ self._value_type = value_type
+ dictionary = {
+ '__metaclass__': reflection.MojoStructType,
+ '__module__': __name__,
+ 'DESCRIPTOR': {
+ 'fields': [
+ SingleFieldGroup('keys', GenericArrayType(key_type), 0, 0),
+ SingleFieldGroup('values', GenericArrayType(value_type), 1, 1),
+ ],
+ }
+ }
+ self.struct = reflection.MojoStructType('MapStruct', (object,), dictionary)
+ self.struct_type = StructType(lambda: self.struct, nullable)
+ SerializableType.__init__(self, self.struct_type.typecode)
+
+ def Convert(self, value):
+ if value is None:
+ return value
+ if isinstance(value, dict):
+ return dict([(self._key_type.Convert(x), self._value_type.Convert(y)) for
+ x, y in value.iteritems()])
+ raise TypeError('%r is not a dictionary.')
+
+ def Serialize(self, value, data_offset, data, handle_offset):
+ s = None
+ if value:
+ keys, values = [], []
+ for key, value in value.iteritems():
+ keys.append(key)
+ values.append(value)
+ s = self.struct(keys=keys, values=values)
+ return self.struct_type.Serialize(s, data_offset, data, handle_offset)
+
+ def Deserialize(self, value, data, handles):
+ s = self.struct_type.Deserialize(value, data, handles)
+ if s:
+ if len(s.keys) != len(s.values):
+ raise serialization.DeserializationException(
+ 'keys and values do not have the same length.')
+ return dict(zip(s.keys, s.values))
+ return None
+
+
class NoneType(SerializableType):
"""Placeholder type, used temporarily until all mojo types are handled."""
« no previous file with comments | « no previous file | mojo/public/tools/bindings/generators/mojom_python_generator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698