diff mbox

[2/3] qapi.py: Allow top-level type reference for command definitions

Message ID 1371659287-14331-3-git-send-email-kwolf@redhat.com
State New
Headers show

Commit Message

Kevin Wolf June 19, 2013, 4:28 p.m. UTC
If 'data' for a command definition isn't a dict, but a string, it is
taken as a (struct) type name and the fields of this struct are directly
used as parameters.

This is useful for transactionable commands that can use the same type
definition for both the transaction action and the arguments of the
standalone command.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 scripts/qapi.py | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

Comments

Eric Blake June 21, 2013, 10:30 a.m. UTC | #1
On 06/19/2013 05:28 PM, Kevin Wolf wrote:
> If 'data' for a command definition isn't a dict, but a string, it is
> taken as a (struct) type name and the fields of this struct are directly
> used as parameters.

I like it!  I suspect it may cause conflicts with Amos' work on adding
introspection, but it is still worth doing.

>  
>  def parse_args(typeinfo):
> +    if isinstance(typeinfo, basestring):
> +        struct = find_struct(typeinfo)
> +        assert struct != None
> +        typeinfo = struct['data']
> +

Does this mean that .json files must be written in topological order (in
that we can't use 'data':'Type' unless 'Type' was declared earlier in
the file)?  As the .json file gets larger, I've been wondering if
enforcing alphabetical ordering would make it easier to manage; but if
topological sorting is required, alphabetical sorting might not always
be possible.
Kevin Wolf June 21, 2013, 11 a.m. UTC | #2
Am 21.06.2013 um 12:30 hat Eric Blake geschrieben:
> On 06/19/2013 05:28 PM, Kevin Wolf wrote:
> > If 'data' for a command definition isn't a dict, but a string, it is
> > taken as a (struct) type name and the fields of this struct are directly
> > used as parameters.
> 
> I like it!  I suspect it may cause conflicts with Amos' work on adding
> introspection, but it is still worth doing.
> 
> >  
> >  def parse_args(typeinfo):
> > +    if isinstance(typeinfo, basestring):
> > +        struct = find_struct(typeinfo)
> > +        assert struct != None
> > +        typeinfo = struct['data']
> > +
> 
> Does this mean that .json files must be written in topological order (in
> that we can't use 'data':'Type' unless 'Type' was declared earlier in
> the file)?

No, you have effectively two passes: First the qapi.py function reads in
the file and adds any structs, enums and unions to the respective lists
and returns something like an array of all objects found. The generator
scripts then filter that array for the type of objects they want (e.g.
only commands, or only types) and indirectly call parse_args(). At this
point all types have already been registered.

> As the .json file gets larger, I've been wondering if
> enforcing alphabetical ordering would make it easier to manage; but if
> topological sorting is required, alphabetical sorting might not always
> be possible.

Yeah, possibly. Another thing I was considering is introducing include
files, so that I could for example specify { 'include':
'block/qapi-schema.json' } and have all block-related things separated
in this file.

Kevin
Eric Blake June 21, 2013, 11:12 a.m. UTC | #3
On 06/21/2013 12:00 PM, Kevin Wolf wrote:

>> As the .json file gets larger, I've been wondering if
>> enforcing alphabetical ordering would make it easier to manage; but if
>> topological sorting is required, alphabetical sorting might not always
>> be possible.
> 
> Yeah, possibly. Another thing I was considering is introducing include
> files, so that I could for example specify { 'include':
> 'block/qapi-schema.json' } and have all block-related things separated
> in this file.

Patches for include files have already been proposed:

https://lists.gnu.org/archive/html/qemu-devel/2013-04/msg04078.html

Maybe it's worth isolating that from its larger (and now abandoned?)
series into an independent smaller series?
diff mbox

Patch

diff --git a/scripts/qapi.py b/scripts/qapi.py
index 3a64769..e151659 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -82,6 +82,8 @@  def evaluate(string):
         add_enum(expr_eval['enum'])
     elif expr_eval.has_key('union'):
         add_enum('%sKind' % expr_eval['union'])
+    elif expr_eval.has_key('type'):
+        add_struct(expr_eval)
 
     return expr_eval
 
@@ -107,6 +109,11 @@  def parse_schema(fp):
     return exprs
 
 def parse_args(typeinfo):
+    if isinstance(typeinfo, basestring):
+        struct = find_struct(typeinfo)
+        assert struct != None
+        typeinfo = struct['data']
+
     for member in typeinfo:
         argname = member
         argentry = typeinfo[member]
@@ -176,6 +183,18 @@  def type_name(name):
     return name
 
 enum_types = []
+struct_types = []
+
+def add_struct(definition):
+    global struct_types
+    struct_types.append(definition)
+
+def find_struct(name):
+    global struct_types
+    for struct in struct_types:
+        if struct['type'] == name:
+            return struct
+    return None
 
 def add_enum(name):
     global enum_types