[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v2 35/54] qapi: add 'if' on union variants
From: |
Marc-André Lureau |
Subject: |
[Qemu-devel] [PATCH v2 35/54] qapi: add 'if' on union variants |
Date: |
Tue, 22 Aug 2017 15:22:36 +0200 |
Signed-off-by: Marc-André Lureau <address@hidden>
---
scripts/qapi.py | 14 +++++++++-----
tests/qapi-schema/qapi-schema-test.json | 6 +++++-
tests/qapi-schema/qapi-schema-test.out | 9 ++++++++-
tests/qapi-schema/test-qapi.py | 3 ++-
4 files changed, 24 insertions(+), 8 deletions(-)
diff --git a/scripts/qapi.py b/scripts/qapi.py
index 946df83854..cd5d151809 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -1397,8 +1397,8 @@ class QAPISchemaObjectTypeVariants(object):
class QAPISchemaObjectTypeVariant(QAPISchemaObjectTypeMember):
role = 'branch'
- def __init__(self, name, typ):
- QAPISchemaObjectTypeMember.__init__(self, name, typ, False)
+ def __init__(self, name, typ, ifcond=None):
+ QAPISchemaObjectTypeMember.__init__(self, name, typ, False, ifcond)
class QAPISchemaAlternateType(QAPISchemaType):
@@ -1647,6 +1647,10 @@ class QAPISchema(object):
return QAPISchemaObjectTypeVariant(case, typ)
def _make_simple_variant(self, case, typ, info):
+ ifcond = None
+ if isinstance(typ, dict):
+ ifcond = typ.get('if')
+ typ = typ['type']
if isinstance(typ, list):
assert len(typ) == 1
typ = self._make_array_type(typ[0], info)
@@ -1654,7 +1658,7 @@ class QAPISchema(object):
typ = self._make_implicit_object_type(
typ, info, None, 'wrapper',
[self._make_member('data', typ, info)], type_entity.ifcond)
- return QAPISchemaObjectTypeVariant(case, typ)
+ return QAPISchemaObjectTypeVariant(case, typ, ifcond)
def _def_union_type(self, expr, info, doc):
name = expr['union']
@@ -1674,8 +1678,8 @@ class QAPISchema(object):
else:
variants = [self._make_simple_variant(key, value, info)
for (key, value) in data.iteritems()]
- typ = self._make_implicit_enum_type(name, info,
- [v.name for v in variants],
+ values = [{'name': v.name, 'if': v.ifcond} for v in variants]
+ typ = self._make_implicit_enum_type(name, info, values,
ifcond)
tag_member = QAPISchemaObjectTypeMember('type', typ, False)
members = [tag_member]
diff --git a/tests/qapi-schema/qapi-schema-test.json
b/tests/qapi-schema/qapi-schema-test.json
index b136e4855f..23dae79f92 100644
--- a/tests/qapi-schema/qapi-schema-test.json
+++ b/tests/qapi-schema/qapi-schema-test.json
@@ -199,9 +199,13 @@
[ 'foo', { 'name' : 'bar', 'if': 'defined(TEST_IF_ENUM_BAR)' } ],
'if': 'defined(TEST_IF_ENUM)' }
-{ 'union': 'TestIfUnion', 'data': { 'foo': 'TestStruct' },
+{ 'union': 'TestIfUnion', 'data':
+ { 'foo': 'TestStruct', 'union_bar': { 'type': 'str', 'if':
'defined(TEST_IF_UNION_BAR)'} },
'if': 'defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)' }
+{ 'command': 'TestIfUnionCmd', 'data': { 'union_cmd_arg': 'TestIfUnion' },
+ 'if': 'defined(TEST_IF_UNION)' }
+
{ 'alternate': 'TestIfAlternate', 'data': { 'foo': 'int', 'bar': 'TestStruct'
},
'if': 'defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)' }
diff --git a/tests/qapi-schema/qapi-schema-test.out
b/tests/qapi-schema/qapi-schema-test.out
index 5d2f18e4aa..12fc5a4425 100644
--- a/tests/qapi-schema/qapi-schema-test.out
+++ b/tests/qapi-schema/qapi-schema-test.out
@@ -73,8 +73,12 @@ object TestIfUnion
member type: TestIfUnionKind optional=False
tag type
case foo: q_obj_TestStruct-wrapper
+ case union_bar: q_obj_str-wrapper if=defined(TEST_IF_UNION_BAR)
if defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)
-enum TestIfUnionKind ['foo']
+command TestIfUnionCmd q_obj_TestIfUnionCmd-arg -> None
+ gen=True success_response=True boxed=False
+ if defined(TEST_IF_UNION)
+enum TestIfUnionKind ['foo', ('union_bar', 'defined(TEST_IF_UNION_BAR)')]
if defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)
object TestStruct
member integer: int optional=False
@@ -204,6 +208,9 @@ object q_obj_TestIfEvent-arg
member foo: TestIfStruct optional=False
member bar: TestIfEnum optional=False if=defined(TEST_IF_EVT_BAR)
if defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)
+object q_obj_TestIfUnionCmd-arg
+ member union_cmd_arg: TestIfUnion optional=False
+ if defined(TEST_IF_UNION)
object q_obj_TestStruct-wrapper
member data: TestStruct optional=False
object q_obj_UserDefFlatUnion2-base
diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py
index 5d2f67a1d3..b360d44df1 100644
--- a/tests/qapi-schema/test-qapi.py
+++ b/tests/qapi-schema/test-qapi.py
@@ -59,7 +59,8 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor):
if variants:
print ' tag %s' % variants.tag_member.name
for v in variants.variants:
- print ' case %s: %s' % (v.name, v.type.name)
+ print ' case %s: %s' % (v.name, v.type.name) + \
+ (' if=%s' % v.ifcond if v.ifcond else '')
@staticmethod
def _print_if(ifcond):
--
2.14.1.146.gd35faa819
- [Qemu-devel] [PATCH v2 43/54] qapi2texi: add 'If:' condition to struct members, (continued)
- [Qemu-devel] [PATCH v2 43/54] qapi2texi: add 'If:' condition to struct members, Marc-André Lureau, 2017/08/22
- [Qemu-devel] [PATCH v2 30/54] qapi: add #if conditions on generated enum values, Marc-André Lureau, 2017/08/22
- [Qemu-devel] [PATCH v2 04/54] qlit: add qobject_form_qlit(), Marc-André Lureau, 2017/08/22
- [Qemu-devel] [PATCH v2 38/54] qapi: add tests for invalid alternate, Marc-André Lureau, 2017/08/22
- [Qemu-devel] [PATCH v2 42/54] qapi2texi: add 'If:' condition to enum values, Marc-André Lureau, 2017/08/22
- [Qemu-devel] [PATCH v2 11/54] quorum: use qapi_enum_parse() in quorum_open, Marc-André Lureau, 2017/08/22
- [Qemu-devel] [PATCH v2 05/54] qapi: generate a literal qobject for introspection, Marc-André Lureau, 2017/08/22
- [Qemu-devel] [PATCH v2 31/54] tests: add some enum members tests, Marc-André Lureau, 2017/08/22
- [Qemu-devel] [PATCH v2 16/54] qapi: add a test for invalid 'if', Marc-André Lureau, 2017/08/22
- [Qemu-devel] [PATCH v2 35/54] qapi: add 'if' on union variants,
Marc-André Lureau <=
- [Qemu-devel] [PATCH v2 19/54] qapi: add #if/#endif helpers, Marc-André Lureau, 2017/08/22
- [Qemu-devel] [PATCH v2 50/54] qapi: make rtc-reset-reinjection depend on TARGET_I386, Marc-André Lureau, 2017/08/22
- [Qemu-devel] [PATCH v2 17/54] qapi: add 'if' condition on entity objects, Marc-André Lureau, 2017/08/22
- [Qemu-devel] [PATCH v2 33/54] qapi: add some struct member tests, Marc-André Lureau, 2017/08/22
- [Qemu-devel] [PATCH v2 45/54] qapi: add conditions to VNC type/commands/events on the schema, Marc-André Lureau, 2017/08/22
- [Qemu-devel] [PATCH v2 51/54] qapi: make s390 commands depend on TARGET_S390X, Marc-André Lureau, 2017/08/22