Signed-off-by: Marc-André Lureau <marcandre.lur...@redhat.com> --- 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