[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v3 19/49] qapi: factor out check_known_keys()
From: |
Marc-André Lureau |
Subject: |
[Qemu-devel] [PATCH v3 19/49] qapi: factor out check_known_keys() |
Date: |
Wed, 21 Mar 2018 12:51:41 +0100 |
The following patches are going to need similar checks from various
code path. This refactoring will report all conflicting keys (instead
of the first one encountered).
Modify unknown-expr-key to check plural form.
Signed-off-by: Marc-André Lureau <address@hidden>
---
scripts/qapi/common.py | 27 ++++++++++++++++++-------
tests/qapi-schema/alternate-base.err | 2 +-
tests/qapi-schema/double-type.err | 2 +-
tests/qapi-schema/enum-missing-data.err | 2 +-
tests/qapi-schema/unknown-expr-key.err | 2 +-
tests/qapi-schema/unknown-expr-key.json | 2 +-
6 files changed, 25 insertions(+), 12 deletions(-)
diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py
index 4d19146064..fdbb5f1823 100644
--- a/scripts/qapi/common.py
+++ b/scripts/qapi/common.py
@@ -879,6 +879,24 @@ def check_struct(expr, info):
allow_metas=['struct'])
+def check_known_keys(info, source, keys, required, optional):
+
+ def pprint(elems):
+ return ', '.join("'" + e + "'" for e in sorted(elems))
+
+ missing = set(required) - set(keys)
+ if missing:
+ raise QAPISemError(info, "%s must have %s key%s"
+ % (source, pprint(missing),
+ 's' if len(missing) > 1 else ''))
+ allowed = set(required + optional)
+ unknown = set(keys) - allowed
+ if unknown:
+ raise QAPISemError(info, "%s has unknown key%s %s (allowed: %s)"
+ % (source, 's' if len(unknown) > 1 else '',
+ pprint(unknown), pprint(allowed)))
+
+
def check_keys(expr_elem, meta, required, optional=[]):
expr = expr_elem['expr']
info = expr_elem['info']
@@ -886,10 +904,9 @@ def check_keys(expr_elem, meta, required, optional=[]):
if not isinstance(name, str):
raise QAPISemError(info, "'%s' key must have a string value" % meta)
required = required + [meta]
+ source = "%s '%s'" % (meta, name)
+ check_known_keys(info, source, expr, required, optional)
for (key, value) in expr.items():
- if key not in required and key not in optional:
- raise QAPISemError(info, "Unknown key '%s' in %s '%s'"
- % (key, meta, name))
if (key == 'gen' or key == 'success-response') and value is not False:
raise QAPISemError(info,
"'%s' of %s '%s' should only use false value"
@@ -900,10 +917,6 @@ def check_keys(expr_elem, meta, required, optional=[]):
% (key, meta, name))
if key == 'if':
check_if(expr, info)
- for key in required:
- if key not in expr:
- raise QAPISemError(info, "Key '%s' is missing from %s '%s'"
- % (key, meta, name))
def check_exprs(exprs):
diff --git a/tests/qapi-schema/alternate-base.err
b/tests/qapi-schema/alternate-base.err
index 30d8a34373..2b09c4c7a3 100644
--- a/tests/qapi-schema/alternate-base.err
+++ b/tests/qapi-schema/alternate-base.err
@@ -1 +1 @@
-tests/qapi-schema/alternate-base.json:4: Unknown key 'base' in alternate 'Alt'
+tests/qapi-schema/alternate-base.json:4: alternate 'Alt' has unknown key
'base' (allowed: 'alternate', 'data', 'if')
diff --git a/tests/qapi-schema/double-type.err
b/tests/qapi-schema/double-type.err
index f9613c6d6b..a8c5637659 100644
--- a/tests/qapi-schema/double-type.err
+++ b/tests/qapi-schema/double-type.err
@@ -1 +1 @@
-tests/qapi-schema/double-type.json:2: Unknown key 'command' in struct 'bar'
+tests/qapi-schema/double-type.json:2: struct 'bar' has unknown key 'command'
(allowed: 'base', 'data', 'if', 'struct')
diff --git a/tests/qapi-schema/enum-missing-data.err
b/tests/qapi-schema/enum-missing-data.err
index ba4873ae69..68e286badc 100644
--- a/tests/qapi-schema/enum-missing-data.err
+++ b/tests/qapi-schema/enum-missing-data.err
@@ -1 +1 @@
-tests/qapi-schema/enum-missing-data.json:2: Key 'data' is missing from enum
'MyEnum'
+tests/qapi-schema/enum-missing-data.json:2: enum 'MyEnum' must have 'data' key
diff --git a/tests/qapi-schema/unknown-expr-key.err
b/tests/qapi-schema/unknown-expr-key.err
index 12f5ed5b43..d9f4e41cac 100644
--- a/tests/qapi-schema/unknown-expr-key.err
+++ b/tests/qapi-schema/unknown-expr-key.err
@@ -1 +1 @@
-tests/qapi-schema/unknown-expr-key.json:2: Unknown key 'bogus' in struct 'bar'
+tests/qapi-schema/unknown-expr-key.json:2: struct 'bar' has unknown keys
'bogus', 'foo' (allowed: 'base', 'data', 'if', 'struct')
diff --git a/tests/qapi-schema/unknown-expr-key.json
b/tests/qapi-schema/unknown-expr-key.json
index 3b2be00cc4..5bcb8efd1d 100644
--- a/tests/qapi-schema/unknown-expr-key.json
+++ b/tests/qapi-schema/unknown-expr-key.json
@@ -1,2 +1,2 @@
# we reject an expression with unknown top-level keys
-{ 'struct': 'bar', 'data': { 'string': 'str'}, 'bogus': { } }
+{ 'struct': 'bar', 'data': { 'string': 'str'}, 'bogus': { }, 'foo': { } }
--
2.16.2.521.g9aa15f885a
- [Qemu-devel] [PATCH v3 10/49] qapi-introspect: add preprocessor conditions to generated QLit, (continued)
- [Qemu-devel] [PATCH v3 10/49] qapi-introspect: add preprocessor conditions to generated QLit, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 09/49] qapi-introspect: modify to_qlit() to append ', ' on level > 0, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 11/49] qapi/commands: add #if conditions to commands, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 12/49] qapi/events: add #if conditions to events, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 13/49] qapi-types: refactor variants handling, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 14/49] qapi-types: add #if conditions to types & visitors, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 15/49] qapi: do not define enumeration value explicitely, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 16/49] qapi: rename QAPISchemaEnumType.values to .members, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 17/49] qapi: change enum visitor to take QAPISchemaMember, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 18/49] tests: modify visit_enum_type() in test-qapi to print members, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 19/49] qapi: factor out check_known_keys(),
Marc-André Lureau <=
- [Qemu-devel] [PATCH v3 20/49] qapi: add a dictionnary form with 'name' key for enum members, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 21/49] qapi: add 'if' to enum members, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 22/49] qapi-event: add 'if' condition to implicit event enum, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 23/49] qapi: rename allow_dict to allow_implicit, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 25/49] qapi: add 'if' to implicit struct members, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 24/49] qapi: add a dictionary form with 'type' key for members, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 26/49] qapi: add an error in case a discriminator is conditionnal, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 27/49] qapi: add 'if' on union members, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 28/49] qapi: add 'if' to alternate members, Marc-André Lureau, 2018/03/21
- [Qemu-devel] [PATCH v3 29/49] qapi: add #if conditions to generated code members, Marc-André Lureau, 2018/03/21