@@ -829,6 +829,8 @@ def check_alternate(expr, info):
check_type(info, "Member '%s' of alternate '%s'" % (key, name),
value,
allow_metas=['built-in', 'union', 'struct', 'enum'])
+ if isinstance(value, dict):
+ value = value['type']
qtype = find_alternate_member_qtype(value)
if not qtype:
raise QAPISemError(info, "Alternate '%s' member '%s' cannot use "
@@ -1643,7 +1645,11 @@ class QAPISchema(object):
expr.get('if'))
def _make_variant(self, case, typ):
- return QAPISchemaObjectTypeVariant(case, typ)
+ ifcond = None
+ if isinstance(typ, dict):
+ ifcond = typ.get('if')
+ typ = typ['type']
+ return QAPISchemaObjectTypeVariant(case, typ, ifcond)
def _make_simple_variant(self, case, typ, info):
ifcond = None
@@ -162,7 +162,7 @@ const QLitObject %(c_name)s = %(c_string)s;
def visit_alternate_type(self, name, info, variants, ifcond):
self._gen_qlit(name, 'alternate',
- {'members': [{'type': self._use_type(m.type)}
+ {'members': [({'type': self._use_type(m.type)}, m.ifcond)
for m in variants.variants]}, ifcond)
def visit_command(self, name, info, arg_type, ret_type,
@@ -351,6 +351,7 @@ qapi-schema += alternate-conflict-dict.json
qapi-schema += alternate-conflict-enum-bool.json
qapi-schema += alternate-conflict-enum-int.json
qapi-schema += alternate-conflict-string.json
+qapi-schema += alternate-dict-invalid.json
qapi-schema += alternate-empty.json
qapi-schema += alternate-nested.json
qapi-schema += alternate-unknown.json
new file mode 100644
@@ -0,0 +1 @@
+tests/qapi-schema/alternate-dict-invalid.json:2: Member 'two' of alternate 'Alt' should be a type name
new file mode 100644
@@ -0,0 +1 @@
+1
new file mode 100644
@@ -0,0 +1,4 @@
+# invalid field dictionnary, missing type
+{ 'alternate': 'Alt',
+ 'data': { 'one': 'str',
+ 'two': { 'if': 'foo' } } }
new file mode 100644
@@ -206,9 +206,13 @@
{ 'command': 'TestIfUnionCmd', 'data': { 'union_cmd_arg': 'TestIfUnion' },
'if': 'defined(TEST_IF_UNION)' }
-{ 'alternate': 'TestIfAlternate', 'data': { 'foo': 'int', 'bar': 'TestStruct' },
+{ 'alternate': 'TestIfAlternate', 'data':
+ { 'foo': 'int', 'alt_bar': { 'type': 'TestStruct', 'if': 'defined(TEST_IF_ALT_BAR)'} },
'if': 'defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)' }
+{ 'command': 'TestIfAlternateCmd', 'data': { 'alt_cmd_arg': 'TestIfAlternate' },
+ 'if': 'defined(TEST_IF_ALT)' }
+
{ 'command': 'TestIfCmd', 'data':
{ 'foo': 'TestIfStruct', 'bar': { 'type': 'TestIfEnum', 'if': 'TEST_IF_CMD_BAR' } },
'if': 'defined(TEST_IF_CMD) && defined(TEST_IF_STRUCT)' }
@@ -55,8 +55,11 @@ enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool']
alternate TestIfAlternate
tag type
case foo: int
- case bar: TestStruct
+ case alt_bar: TestStruct if=defined(TEST_IF_ALT_BAR)
if defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)
+command TestIfAlternateCmd q_obj_TestIfAlternateCmd-arg -> None
+ gen=True success_response=True boxed=False
+ if defined(TEST_IF_ALT)
command TestIfCmd q_obj_TestIfCmd-arg -> None
gen=True success_response=True boxed=False
if defined(TEST_IF_CMD) && defined(TEST_IF_STRUCT)
@@ -200,6 +203,9 @@ object q_obj_EVENT_D-arg
member b: str optional=False
member c: str optional=True
member enum3: EnumOne optional=True
+object q_obj_TestIfAlternateCmd-arg
+ member alt_cmd_arg: TestIfAlternate optional=False
+ if defined(TEST_IF_ALT)
object q_obj_TestIfCmd-arg
member foo: TestIfStruct optional=False
member bar: TestIfEnum optional=False if=TEST_IF_CMD_BAR
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> --- scripts/qapi.py | 8 +++++++- scripts/qapi-introspect.py | 2 +- tests/Makefile.include | 1 + tests/qapi-schema/alternate-dict-invalid.err | 1 + tests/qapi-schema/alternate-dict-invalid.exit | 1 + tests/qapi-schema/alternate-dict-invalid.json | 4 ++++ tests/qapi-schema/alternate-dict-invalid.out | 0 tests/qapi-schema/qapi-schema-test.json | 6 +++++- tests/qapi-schema/qapi-schema-test.out | 8 +++++++- 9 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 tests/qapi-schema/alternate-dict-invalid.err create mode 100644 tests/qapi-schema/alternate-dict-invalid.exit create mode 100644 tests/qapi-schema/alternate-dict-invalid.json create mode 100644 tests/qapi-schema/alternate-dict-invalid.out