Skip to contentSkip to conversation

qapi-schema: support alternates with array type

PATCH Applied Paolo Bonzini 2 participants 10 messages Mar 21, 2022 – Apr 18, 2022 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover qapi-schema: support alternates with array type

As suggested in the review of the statistics subsystem.

Paolo Bonzini (3):
qapi-schema: support alternates with array type
qapi-schema: test: add a qapi-schema-test for array alternates
qapi-schema: test: add a unit test for parsing array alternates

 scripts/qapi/expr.py                          |  2 +-
 scripts/qapi/schema.py                        |  4 ++
 tests/qapi-schema/alternate-array.err         |  2 -
 tests/qapi-schema/alternate-array.json        |  2 -
 tests/qapi-schema/alternate-array.out         | 18 ++++++++
 .../qapi-schema/alternate-conflict-lists.err  |  2 +
 .../qapi-schema/alternate-conflict-lists.json |  6 +++
 .../qapi-schema/alternate-conflict-lists.out  |  0
 tests/qapi-schema/meson.build                 |  1 +
 tests/qapi-schema/qapi-schema-test.json       |  1 +
 tests/qapi-schema/qapi-schema-test.out        |  4 ++
 tests/unit/test-qobject-input-visitor.c       | 43 +++++++++++++++++++
 12 files changed, 79 insertions(+), 5 deletions(-)
 create mode 100644 tests/qapi-schema/alternate-conflict-lists.err
 create mode 100644 tests/qapi-schema/alternate-conflict-lists.json
 create mode 100644 tests/qapi-schema/alternate-conflict-lists.out
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/3 qapi-schema: support alternates with array type

Detect array types as alternate branches, and turn the JSON list into a QAPISchemaArrayType. Array types in an alternate are represented with QTYPE_QLIST in the type field.

---
 scripts/qapi/expr.py                   |  2 +-
 scripts/qapi/schema.py                 |  4 ++++
 tests/qapi-schema/alternate-array.err  |  2 --
 tests/qapi-schema/alternate-array.json |  2 --
 tests/qapi-schema/alternate-array.out  | 18 ++++++++++++++++++
 5 files changed, 23 insertions(+), 5 deletions(-)
scripts/qapi/expr.py +1 -1
index 3cb389e875..48578e1698 100644
@@ -554,7 +554,7 @@ def check_alternate(expr: _JSONObject, info: QAPISourceInfo) -> None:
         check_name_lower(key, info, source)
         check_keys(value, info, source, ['type'], ['if'])
         check_if(value, info, source)
-        check_type(value['type'], info, source)
+        check_type(value['type'], info, source, allow_array=True)
 
 
 def check_command(expr: _JSONObject, info: QAPISourceInfo) -> None:
scripts/qapi/schema.py +4 -0
index b7b3fc0ce4..3728340c37 100644
@@ -243,6 +243,7 @@ def alternate_qtype(self):
             'number':  'QTYPE_QNUM',
             'int':     'QTYPE_QNUM',
             'boolean': 'QTYPE_QBOOL',
+            'array':   'QTYPE_QLIST',
             'object':  'QTYPE_QDICT'
         }
         return json2qtype.get(self.json_type())
@@ -1069,6 +1070,9 @@ def _def_struct_type(self, expr, info, doc):
             None))
 
     def _make_variant(self, case, typ, ifcond, info):
+        if isinstance(typ, list):
+            assert len(typ) == 1
+            typ = self._make_array_type(typ[0], info)
         return QAPISchemaVariant(case, info, typ, ifcond)
 
     def _def_union_type(self, expr, info, doc):
tests/qapi-schema/alternate-array.err +0 -2
index b1aa1f4e8d..e69de29bb2 100644
@@ -1,2 +0,0 @@
-alternate-array.json: In alternate 'Alt':
-alternate-array.json:5: 'data' member 'two' cannot be an array
tests/qapi-schema/alternate-array.json +0 -2
index f241aac122..b878a2db77 100644
@@ -1,5 +1,3 @@
-# we do not allow array branches in alternates
-# TODO: should we support this?
 { 'struct': 'One',
   'data': { 'name': 'str' } }
 { 'alternate': 'Alt',
tests/qapi-schema/alternate-array.out +18 -0
index e69de29bb2..a657d85738 100644
@@ -0,0 +1,18 @@
+module ./builtin
+object q_empty
+enum QType
+    prefix QTYPE
+    member none
+    member qnull
+    member qnum
+    member qstring
+    member qdict
+    member qlist
+    member qbool
+module alternate-array.json
+object One
+    member name: str optional=False
+alternate Alt
+    tag type
+    case one: One
+    case two: intList
Signature · 1 line
2.35.1
Reviewed-by on v1 1/3
#2 lore raw
Quoted 5 lines · Paolo Bonzini

Detect array types as alternate branches, and turn the JSON list into a QAPISchemaArrayType. Array types in an alternate are represented with QTYPE_QLIST in the type field.

I double-checked the generated code; it looks good to me.

Thanks for implementing this, and extra thanks for the tests!

#3 lore raw
Quoted 3 lines · Markus Armbruster

I double-checked the generated code; it looks good to me.

Thanks for implementing this, and extra thanks for the tests!

Heh, the problem is having to build the infrastructure for tests. If it's already there, patches like this are actually easier to write with tests than without!...

Paolo

#4 lore raw

PATCH 2/3 qapi-schema: test: add a qapi-schema-test for array alternates

In reply to Paolo Bonzini

Check that conflicts among array alternates are detected correctly.

---
 tests/qapi-schema/alternate-conflict-lists.err  | 2 ++
 tests/qapi-schema/alternate-conflict-lists.json | 6 +++++
 tests/qapi-schema/alternate-conflict-lists.out  | 0
 tests/qapi-schema/meson.build                   | 1 +
 4 files changed, 8 insertions(+)
 create mode 100644 tests/qapi-schema/alternate-conflict-lists.err
 create mode 100644 tests/qapi-schema/alternate-conflict-lists.json
 create mode 100644 tests/qapi-schema/alternate-conflict-lists.out
tests/qapi-schema/alternate-conflict-lists.err new file +2 -0
new file mode 100644
index 0000000000..288b75a2ff
@@ -0,0 +1,2 @@
+alternate-conflict-lists.json: In alternate 'Alt':
+alternate-conflict-lists.json:5: branch 'two' can't be distinguished from 'one'
tests/qapi-schema/alternate-conflict-lists.json new file +6 -0
new file mode 100644
index 0000000000..90710da115
@@ -0,0 +1,6 @@
+# Two lists conflict even if their inner types would be compatible
+{ 'struct': 'One',
+  'data': { 'name': 'str' } }
+{ 'alternate': 'Alt',
+  'data': { 'one': [ 'int' ],
+            'two': [ 'str' ] } }
tests/qapi-schema/alternate-conflict-lists.out new file +0 -0
new file mode 100644
index 0000000000..e69de29bb2
tests/qapi-schema/meson.build +1 -0
index caf0791ba8..c18dd7d02f 100644
@@ -11,6 +11,7 @@ schemas = [
   'alternate-conflict-dict.json',
   'alternate-conflict-enum-bool.json',
   'alternate-conflict-enum-int.json',
+  'alternate-conflict-lists.json',
   'alternate-conflict-string.json',
   'alternate-conflict-bool-string.json',
   'alternate-conflict-num-string.json',
Signature · 1 line
2.35.1
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] qapi-schema: test: add a qapi-schema-test for array alternates

Quoted 19 lines · Paolo Bonzini

Check that conflicts among array alternates are detected correctly.

---
 tests/qapi-schema/alternate-conflict-lists.err  | 2 ++
 tests/qapi-schema/alternate-conflict-lists.json | 6 +++++
 tests/qapi-schema/alternate-conflict-lists.out  | 0
 tests/qapi-schema/meson.build                   | 1 +
 4 files changed, 8 insertions(+)
 create mode 100644 tests/qapi-schema/alternate-conflict-lists.err
 create mode 100644 tests/qapi-schema/alternate-conflict-lists.json
 create mode 100644 tests/qapi-schema/alternate-conflict-lists.out
tests/qapi-schema/alternate-conflict-lists.err new file +0 -0
new file mode 100644
index 0000000000..288b75a2ff

tests/qapi-schema/alternate-conflict-lists.err line 2

Quoted 2 lines
tests/qapi-schema/alternate-conflict-lists.err new file +2 -0
@@ -0,0 +1,2 @@
+alternate-conflict-lists.json: In alternate 'Alt':
+alternate-conflict-lists.json:5: branch 'two' can't be distinguished from 'one'

Needs a trivial fixup:

-alternate-conflict-lists.json:5: branch 'two' can't be distinguished from 'one' +alternate-conflict-lists.json:4: branch 'two' can't be distinguished from 'one'

Happy to apply it in my tree.

Quoted 20 lines
tests/qapi-schema/alternate-conflict-lists.json new file +6 -0
new file mode 100644
index 0000000000..90710da115
@@ -0,0 +1,6 @@
+# Two lists conflict even if their inner types would be compatible
+{ 'struct': 'One',
+  'data': { 'name': 'str' } }
+{ 'alternate': 'Alt',
+  'data': { 'one': [ 'int' ],
+            'two': [ 'str' ] } }
tests/qapi-schema/alternate-conflict-lists.out new file +0 -0
new file mode 100644
index 0000000000..e69de29bb2
tests/qapi-schema/meson.build +0 -0
index caf0791ba8..c18dd7d02f 100644

tests/qapi-schema/meson.build schemas = [ · line 17

Quoted 7 lines
tests/qapi-schema/meson.build +1 -0
@@ -11,6 +11,7 @@ schemas = [
   'alternate-conflict-dict.json',
   'alternate-conflict-enum-bool.json',
   'alternate-conflict-enum-int.json',
+  'alternate-conflict-lists.json',
   'alternate-conflict-string.json',
   'alternate-conflict-bool-string.json',
   'alternate-conflict-num-string.json',

With the fixup:

#6 lore raw

PATCH 3/3 qapi-schema: test: add a unit test for parsing array alternates

In reply to Paolo Bonzini
---
 tests/qapi-schema/qapi-schema-test.json |  1 +
 tests/qapi-schema/qapi-schema-test.out  |  4 +++
 tests/unit/test-qobject-input-visitor.c | 43 +++++++++++++++++++++++++
 3 files changed, 48 insertions(+)
tests/qapi-schema/qapi-schema-test.json +1 -0
index 43b8697002..ba7302f42b 100644
@@ -119,6 +119,7 @@
 { 'alternate': 'AltEnumNum', 'data': { 'e': 'EnumOne', 'n': 'number' } }
 { 'alternate': 'AltNumEnum', 'data': { 'n': 'number', 'e': 'EnumOne' } }
 { 'alternate': 'AltEnumInt', 'data': { 'e': 'EnumOne', 'i': 'int' } }
+{ 'alternate': 'AltListInt', 'data': { 'l': ['int'], 'i': 'int' } }
 
 # for testing use of 'str' within alternates
 { 'alternate': 'AltStrObj', 'data': { 's': 'str', 'o': 'TestStruct' } }
tests/qapi-schema/qapi-schema-test.out +4 -0
index 1f9585fa9b..043d75c655 100644
@@ -121,6 +121,10 @@ alternate AltEnumInt
     tag type
     case e: EnumOne
     case i: int
+alternate AltListInt
+    tag type
+    case l: intList
+    case i: int
 alternate AltStrObj
     tag type
     case s: str
tests/unit/test-qobject-input-visitor.c +43 -0

67 lines not on this page · raw message

Signature · 1 line
2.35.1
Reviewed-by on v1 3/3
#7 lore raw

Re: [PATCH 3/3] qapi-schema: test: add a unit test for parsing array alternates

Quoted 70 lines · Paolo Bonzini
---
 tests/qapi-schema/qapi-schema-test.json |  1 +
 tests/qapi-schema/qapi-schema-test.out  |  4 +++
 tests/unit/test-qobject-input-visitor.c | 43 +++++++++++++++++++++++++
 3 files changed, 48 insertions(+)
tests/qapi-schema/qapi-schema-test.json +1 -0
index 43b8697002..ba7302f42b 100644
@@ -119,6 +119,7 @@
 { 'alternate': 'AltEnumNum', 'data': { 'e': 'EnumOne', 'n': 'number' } }
 { 'alternate': 'AltNumEnum', 'data': { 'n': 'number', 'e': 'EnumOne' } }
 { 'alternate': 'AltEnumInt', 'data': { 'e': 'EnumOne', 'i': 'int' } }
+{ 'alternate': 'AltListInt', 'data': { 'l': ['int'], 'i': 'int' } }
 
 # for testing use of 'str' within alternates
 { 'alternate': 'AltStrObj', 'data': { 's': 'str', 'o': 'TestStruct' } }
tests/qapi-schema/qapi-schema-test.out +4 -0
index 1f9585fa9b..043d75c655 100644
@@ -121,6 +121,10 @@ alternate AltEnumInt
     tag type
     case e: EnumOne
     case i: int
+alternate AltListInt
+    tag type
+    case l: intList
+    case i: int
 alternate AltStrObj
     tag type
     case s: str
tests/unit/test-qobject-input-visitor.c +14 -0
index 6f59a7f432..2af002dd82 100644
@@ -776,6 +776,7 @@ static void test_visitor_in_alternate_number(TestInputVisitorData *data,
     AltEnumNum *aen;
     AltNumEnum *ans;
     AltEnumInt *asi;
+    AltListInt *ali;
 
     /* Parsing an int */
 
@@ -802,6 +803,12 @@ static void test_visitor_in_alternate_number(TestInputVisitorData *data,
     g_assert_cmpint(asi->u.i, ==, 42);
     qapi_free_AltEnumInt(asi);
 
+    v = visitor_input_test_init(data, "42");
+    visit_type_AltListInt(v, NULL, &ali, &error_abort);
+    g_assert_cmpint(ali->type, ==, QTYPE_QNUM);
+    g_assert_cmpint(ali->u.i, ==, 42);
+    qapi_free_AltListInt(ali);
+
     /* Parsing a double */
 
     v = visitor_input_test_init(data, "42.5");
@@ -827,6 +834,40 @@ static void test_visitor_in_alternate_number(TestInputVisitorData *data,
     qapi_free_AltEnumInt(asi);
 }
 
+static void test_visitor_in_alternate_list(TestInputVisitorData *data,
+                                 const void *unused)
+{
+    intList *item;
+    Visitor *v;
+    AltListInt *ali;
+    int i;

tests/unit/test-qobject-input-visitor.c static void test_visitor_in_alternate_number(TestInputVisitorData *data, · line 853

Quoted 10 lines
tests/unit/test-qobject-input-visitor.c +10 -0
+
+    v = visitor_input_test_init(data, "[ 42, 43, 44 ]");
+    visit_type_AltListInt(v, NULL, &ali, &error_abort);
+    g_assert(ali != NULL);
+
+    g_assert_cmpint(ali->type, ==, QTYPE_QLIST);
+    for (i = 0, item = ali->u.l; item; item = item->next, i++) {
+        char string[12];
+
+        snprintf(string, sizeof(string), "string%d", i);

This appears to be unused. Can drop in my tree.

Quoted 21 lines
tests/unit/test-qobject-input-visitor.c +17 -0
+        g_assert_cmpint(item->value, ==, 42 + i);
+    }
+
+    qapi_free_AltListInt(ali);
+    ali = NULL;
+
+    /* An empty list is valid */
+    v = visitor_input_test_init(data, "[]");
+    visit_type_AltListInt(v, NULL, &ali, &error_abort);
+    g_assert(ali != NULL);
+
+    g_assert_cmpint(ali->type, ==, QTYPE_QLIST);
+    g_assert(!ali->u.l);
+    qapi_free_AltListInt(ali);
+    ali = NULL;
+}
+
 static void input_visitor_test_add(const char *testpath,
                                    const void *user_data,
                                    void (*test_func)(TestInputVisitorData *data,

tests/unit/test-qobject-input-visitor.c int main(int argc, char **argv) · line 1236

Quoted 8 lines
tests/unit/test-qobject-input-visitor.c +2 -0
@@ -1188,6 +1229,8 @@ int main(int argc, char **argv)
                            NULL, test_visitor_in_wrong_type);
     input_visitor_test_add("/visitor/input/alternate-number",
                            NULL, test_visitor_in_alternate_number);
+    input_visitor_test_add("/visitor/input/alternate-list",
+                           NULL, test_visitor_in_alternate_list);
     input_visitor_test_add("/visitor/input/fail/struct",
                            NULL, test_visitor_in_fail_struct);
     input_visitor_test_add("/visitor/input/fail/struct-nested",

Other than that:

#9 lore raw
Quoted 5 lines · Markus Armbruster
Quoted 1 line · Paolo Bonzini

As suggested in the review of the statistics subsystem.

Queued for 7.1, thanks!

Thanks, I will follow up with the stats patches after these land.

Paolo