Chromium Code Reviews| 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 b97938ac52dae25bd11aad0fb21dd0413c072efc..5056072e46f46b4408e732ce76757eacd6fd3265 100644 |
| --- a/mojo/public/python/mojo/bindings/descriptor.py |
| +++ b/mojo/public/python/mojo/bindings/descriptor.py |
| @@ -11,6 +11,7 @@ import itertools |
| import struct |
| # pylint: disable=F0401 |
| +import mojo.bindings.reflection as reflection |
| import mojo.bindings.serialization as serialization |
| import mojo.system |
| @@ -403,6 +404,50 @@ 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.items()]) |
| + raise TypeError('%r is not a dictionary.') |
| + |
| + def Serialize(self, value, data_offset, data, handle_offset): |
| + s = None |
| + if value: |
| + key_values = value.items() |
|
sdefresne
2014/10/10 15:10:26
nit: I think it would be clearer as:
keys, values
qsr
2014/10/10 15:36:05
Done.
|
| + keys = [x[0] for x in key_values] |
| + values = [x[1] for x in key_values] |
| + 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: |
| + return dict(zip(s.keys, s.values)) |
| + return None |
| + |
| + |
| class NoneType(SerializableType): |
| """Placeholder type, used temporarily until all mojo types are handled.""" |