[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v3 26/50] qapi: add 'if' on union variants
From: |
Marc-André Lureau |
Subject: |
[Qemu-devel] [PATCH v3 26/50] qapi: add 'if' on union variants |
Date: |
Mon, 11 Sep 2017 13:05:59 +0200 |
Signed-off-by: Marc-André Lureau <address@hidden>
---
scripts/qapi.py | 15 ++++++++++-----
tests/qapi-schema/qapi-schema-test.json | 7 ++++++-
tests/qapi-schema/qapi-schema-test.out | 8 ++++++++
tests/qapi-schema/test-qapi.py | 5 ++++-
4 files changed, 28 insertions(+), 7 deletions(-)
diff --git a/scripts/qapi.py b/scripts/qapi.py
index 15711f96fa..2f14edfa1f 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -1412,8 +1412,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):
@@ -1674,13 +1674,18 @@ class QAPISchema(object):
return QAPISchemaObjectTypeVariant(case, typ)
def _make_simple_variant(self, case, typ, info):
+ ifcond = None
+ if isinstance(typ, dict):
+ check_unknown_keys(info, typ, {'type', 'if'})
+ ifcond = typ.get('if')
+ typ = typ['type']
if isinstance(typ, list):
assert len(typ) == 1
typ = self._make_array_type(typ[0], info)
typ = self._make_implicit_object_type(
typ, info, None, self.lookup_type(typ).ifcond,
'wrapper', [self._make_member('data', typ, info)])
- return QAPISchemaObjectTypeVariant(case, typ)
+ return QAPISchemaObjectTypeVariant(case, typ, ifcond)
def _def_union_type(self, expr, info, doc):
name = expr['union']
@@ -1700,8 +1705,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, ifcond,
- [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, ifcond, values)
tag_member = QAPISchemaObjectTypeMember('type', typ, False)
members = [tag_member]
self._def_entity(
diff --git a/tests/qapi-schema/qapi-schema-test.json
b/tests/qapi-schema/qapi-schema-test.json
index 5cfccabb3d..895e80a978 100644
--- a/tests/qapi-schema/qapi-schema-test.json
+++ b/tests/qapi-schema/qapi-schema-test.json
@@ -200,9 +200,14 @@
[ '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 6df4e49c69..ee009c5626 100644
--- a/tests/qapi-schema/qapi-schema-test.out
+++ b/tests/qapi-schema/qapi-schema-test.out
@@ -87,9 +87,14 @@ 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)
+command TestIfUnionCmd q_obj_TestIfUnionCmd-arg -> None
+ gen=True success_response=True boxed=False
+ if defined(TEST_IF_UNION)
enum TestIfUnionKind
member foo:
+ member union_bar: if=defined(TEST_IF_UNION_BAR)
if defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)
object TestStruct
member integer: int optional=False
@@ -235,6 +240,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 a86c3b5ee1..87a8efff78 100644
--- a/tests/qapi-schema/test-qapi.py
+++ b/tests/qapi-schema/test-qapi.py
@@ -65,7 +65,10 @@ 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 v.ifcond:
+ print 'if=%s' % v.ifcond,
+ print
@staticmethod
def _print_if(ifcond):
--
2.14.1.146.gd35faa819
- [Qemu-devel] [PATCH v3 15/50] qapi-types: refactor variants handling, (continued)
- [Qemu-devel] [PATCH v3 15/50] qapi-types: refactor variants handling, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 19/50] qapi: add 'if' to enum members, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 18/50] qapi: change enum visitor to take QAPISchemaMember, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 21/50] qapi: add #if conditions on generated enum members, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 20/50] qapi-event: add 'if' condition to generated enum, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 23/50] qapi: add 'if' to struct members and implicit objects members, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 22/50] tests: add some enum members tests, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 24/50] qapi: add some struct member tests, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 25/50] qapi: add #if conditions to generated struct members, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 27/50] qapi: add #if conditions to generated variants, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 26/50] qapi: add 'if' on union variants,
Marc-André Lureau <=
- [Qemu-devel] [PATCH v3 28/50] qapi: add 'if' to alternate variant, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 30/50] qapi: add #if conditions to generated alternate variants, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 29/50] qapi: add tests for invalid alternate, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 32/50] qapi2texi: add 'If:' section to generated documentation, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 31/50] docs: document schema configuration, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 33/50] qapi2texi: add 'If:' condition to enum values, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 34/50] qapi2texi: add 'If:' condition to struct members, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 35/50] qapi2texi: add condition to variants, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 36/50] qapi: add conditions to VNC type/commands/events on the schema, Marc-André Lureau, 2017/09/11
- [Qemu-devel] [PATCH v3 37/50] qapi: add conditions to SPICE type/commands/events on the schema, Marc-André Lureau, 2017/09/11