Index: third_party/mojo/src/mojo/public/tools/bindings/generators/mojom_python_generator.py |
diff --git a/third_party/mojo/src/mojo/public/tools/bindings/generators/mojom_python_generator.py b/third_party/mojo/src/mojo/public/tools/bindings/generators/mojom_python_generator.py |
index 1f726b6699a9bb31ba226dc39885df38006be90b..a12f5e1a19a27289ded2ce07c653d984a3419f7d 100644 |
--- a/third_party/mojo/src/mojo/public/tools/bindings/generators/mojom_python_generator.py |
+++ b/third_party/mojo/src/mojo/public/tools/bindings/generators/mojom_python_generator.py |
@@ -146,7 +146,13 @@ def GetFieldType(kind, field=None): |
arguments.append('nullable=True') |
return '_descriptor.MapType(%s)' % ', '.join(arguments) |
- if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): |
+ if mojom.IsUnionKind(kind): |
+ arguments = [ 'lambda: %s' % GetFullyQualifiedName(kind) ] |
+ if mojom.IsNullableKind(kind): |
+ arguments.append('nullable=True') |
+ return '_descriptor.UnionType(%s)' % ', '.join(arguments) |
+ |
+ if mojom.IsStructKind(kind): |
arguments = [ 'lambda: %s' % GetFullyQualifiedName(kind) ] |
if mojom.IsNullableKind(kind): |
arguments.append('nullable=True') |
@@ -169,15 +175,14 @@ def GetFieldType(kind, field=None): |
return _kind_to_type[kind] |
-def GetFieldDescriptor(packed_field): |
- field = packed_field.field |
+def GetFieldDescriptor(field, index, min_version): |
class_name = 'SingleFieldGroup' |
if field.kind == mojom.BOOL: |
class_name = 'FieldDescriptor' |
arguments = [ '%r' % GetNameForElement(field) ] |
arguments.append(GetFieldType(field.kind, field)) |
- arguments.append(str(packed_field.index)) |
- arguments.append(str(packed_field.min_version)) |
+ arguments.append(str(index)) |
+ arguments.append(str(min_version)) |
if field.default: |
if mojom.IsStructKind(field.kind): |
arguments.append('default_value=True') |
@@ -185,12 +190,19 @@ def GetFieldDescriptor(packed_field): |
arguments.append('default_value=%s' % ExpressionToText(field.default)) |
return '_descriptor.%s(%s)' % (class_name, ', '.join(arguments)) |
+def GetStructFieldDescriptor(packed_field): |
+ return GetFieldDescriptor( |
+ packed_field.field, packed_field.index, packed_field.min_version) |
+ |
+def GetUnionFieldDescriptor(field): |
+ return GetFieldDescriptor(field, field.ordinal, 0) |
+ |
def GetFieldGroup(byte): |
if byte.packed_fields[0].field.kind == mojom.BOOL: |
- descriptors = map(GetFieldDescriptor, byte.packed_fields) |
+ descriptors = map(GetStructFieldDescriptor, byte.packed_fields) |
return '_descriptor.BooleanGroup([%s])' % ', '.join(descriptors) |
assert len(byte.packed_fields) == 1 |
- return GetFieldDescriptor(byte.packed_fields[0]) |
+ return GetStructFieldDescriptor(byte.packed_fields[0]) |
def MojomToPythonImport(mojom): |
return mojom.replace('.mojom', '_mojom') |
@@ -200,6 +212,7 @@ class Generator(generator.Generator): |
python_filters = { |
'expression_to_text': ExpressionToText, |
'field_group': GetFieldGroup, |
+ 'union_field_descriptor': GetUnionFieldDescriptor, |
'fully_qualified_name': GetFullyQualifiedName, |
'name': GetNameForElement, |
} |
@@ -213,6 +226,7 @@ class Generator(generator.Generator): |
'module': resolver.ResolveConstants(self.module, ExpressionToText), |
'namespace': self.module.namespace, |
'structs': self.GetStructs(), |
+ 'unions': self.GetUnions(), |
} |
def GenerateFiles(self, args): |