Patchwork [qapi] Cannot use list of strings

login
register
mail settings
Submitter Amos Kong
Date April 16, 2013, 10:13 a.m.
Message ID <20130416101322.GA9337@t430s.nay.redhat.com>
Download mbox | patch
Permalink /patch/236909/
State New
Headers show

Comments

Amos Kong - April 16, 2013, 10:13 a.m.
On Tue, Apr 16, 2013 at 10:54:56AM +0200, Paolo Bonzini wrote:
> Il 16/04/2013 10:49, Stefan Hajnoczi ha scritto:
> >> > Tried using a list of strings as an argument to a command, but the generated
> >> > code references the 'strList' type, which does not exist.
> >> > 
> >> > Is a specialized version for "['str']" missing, or should I define my own type
> >> > with a single field of 'str' type?
> > akong just hit this too.

That thread: http://marc.info/?l=qemu-devel&m=136572276530702&w=3

> > I think it's a question for aliguori, luiz, or mdroth.
> 
> Laszlo defined and used String for this purpose:

Eric said String list contains additional JSON structure.
At least, it works.


 
> ##
> # @String
> #
> # A fat type wrapping 'str', to be embedded in lists.
> #
> # Since 1.2
> ##
> { 'type': 'String',
>   'data': {
>     'str': 'str' } }
Eric Blake - April 16, 2013, 12:36 p.m.
On 04/16/2013 04:13 AM, Amos Kong wrote:
> Eric said String list contains additional JSON structure.
> At least, it works.

Using the fat 'String' wrapper works, but requires more effort to decode.

> 
> ===================== using StringList
>     '*unicast':        ['String'],
>     '*multicast':      ['String']
> 
> {
>     "return": [
>         {
>             "name": "virtio-net-pci.0", 
>             "multicast": [
>                 {
>                     "str": "01:80:c2:00:00:21"
>                 }, 
>                 {
>                     "str": "00:00:00:00:00:00"
>                 }
>             ]

Libvirt can live with this, even though it is more work, if you can't
figure out how to make 'str' work.  I don't know enough about the JSON
generator to quickly dive in and figure out why you can't make an array
of native types, though.

>         }, 
>         ....
>     ]
> }
> ======================== using strList
>     '*unicast':        ['str'],
>     '*multicast':      ['str']
> 
> Eric, is it expected format?
> 
> {
>     "return": [
>         {
>             "name": "virtio-net-pci.0", 
>             "multicast": [
>                     "str": "01:80:c2:00:00:21",
>                     "str": "00:00:00:00:00:00"
>             ]

No.  This is not valid JSON.  ':' is only allowed inside {}.  The point
of an array is to either have an array of objects (as in [
{'name':value}, {'name':value} ]) or an array of native types (as in [
value, value ]).  The ideal solution is for the output QMP to look like:

    "multicast": [
        "01:80:c2:00:00:21",
        "00:00:00:00:00:00"
    ]

which seems like it should work as '*multicast':['str'], if you can
figure out how to make the generator play along.

Patch

===================== using StringList
    '*unicast':        ['String'],
    '*multicast':      ['String']

{
    "return": [
        {
            "name": "virtio-net-pci.0", 
            "multicast": [
                {
                    "str": "01:80:c2:00:00:21"
                }, 
                {
                    "str": "00:00:00:00:00:00"
                }
            ]
        }, 
        ....
    ]
}
======================== using strList
    '*unicast':        ['str'],
    '*multicast':      ['str']

Eric, is it expected format?

{
    "return": [
        {
            "name": "virtio-net-pci.0", 
            "multicast": [
                    "str": "01:80:c2:00:00:21",
                    "str": "00:00:00:00:00:00"
            ]
        },
        ....
    ]
}
========================

I changed qapi scripts to define struct, struct list,
visit/free functions for 'str'. But it conflicts with
existed str functions. I hadn't find a solution.

Do we need to defind strList & visit functions in qapi-core files?
not generated by scripts.


diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py
index 9e19920..5167d85 100644
--- a/scripts/qapi-types.py
+++ b/scripts/qapi-types.py
@@ -282,6 +282,10 @@  fdecl.write(mcgen('''
 exprs = parse_schema(sys.stdin)
 exprs = filter(lambda expr: not expr.has_key('gen'), exprs)

+ret = "\n"
+ret += generate_fwd_struct('str', {'str': 'str'})
+fdecl.write(ret)
+
 for expr in exprs:
     ret = "\n"
     if expr.has_key('type'):
@@ -319,6 +323,15 @@  for expr in exprs:
         continue
     fdecl.write(ret)

+ret = "\n"
+ret += generate_struct('str', "", {'str': 'str'}) + "\n"
+ret += generate_type_cleanup_decl('str' + "List")
+fdef.write(generate_type_cleanup('str' + "List") + "\n")
+ret += generate_type_cleanup_decl('str')
+fdef.write(generate_type_cleanup('str') + "\n")
+
+fdecl.write(ret)
+
 fdecl.write('''
 #endif
 ''')
diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py
index a276540..089dda7 100644
--- a/scripts/qapi-visit.py
+++ b/scripts/qapi-visit.py
@@ -354,6 +354,13 @@  for expr in exprs:
         ret += generate_enum_declaration(expr['enum'], expr['data'])
         fdecl.write(ret)

+ret = generate_visit_struct('str', {'str': 'str'})
+ret += generate_visit_list('str', {'str': 'str'})
+fdef.write(ret)
+
+ret = generate_declaration('str', {'str': 'str'})
+fdecl.write(ret)
+
 fdecl.write('''
 #endif
 ''')