diff mbox

QMP: allow dot separated dict path arguments in qmp-shell

Message ID 1392115505-21969-1-git-send-email-famz@redhat.com
State New
Headers show

Commit Message

Fam Zheng Feb. 11, 2014, 10:45 a.m. UTC
As another convinience to allow using commands that expect a dict as
argument, this patch adds support for foo.bar=value syntax, similar to
command line argument style:

  (QEMU) blockdev-add options.driver=file options.id=drive1 options.filename=...

Signed-off-by: Fam Zheng <famz@redhat.com>

--
Applies on top of Stefan's patch

    [PATCH] QMP: allow JSON dict arguments in qmp-shell

Signed-off-by: Fam Zheng <famz@redhat.com>
---
 scripts/qmp/qmp-shell | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

Comments

Stefan Hajnoczi Feb. 11, 2014, 1:28 p.m. UTC | #1
On Tue, Feb 11, 2014 at 06:45:05PM +0800, Fam Zheng wrote:
> diff --git a/scripts/qmp/qmp-shell b/scripts/qmp/qmp-shell
> index d374b35..9c84551 100755
> --- a/scripts/qmp/qmp-shell
> +++ b/scripts/qmp/qmp-shell
> @@ -112,7 +112,14 @@ class QMPShell(qmp.QEMUMonitorProtocol):
>                      value = json.loads(opt[1])
>                  else:
>                      value = opt[1]
> -            qmpcmd['arguments'][opt[0]] = value
> +            optpath = opt[0].split('.')
> +            parent = qmpcmd['arguments']
> +            for p in optpath[:-1]:
> +                if not p in parent:
> +                    d = dict()
> +                    parent[p] = d
> +                parent = d

d is a stale reference when the path component already exists (e.g.
a.b.c=1 a.d=2).  Since 'a' already exists when processing 'a.d' we'll
the value of d will actually be the a.b dict!

I think you need the following instead:

for p in optpath[:-1]:
    d = parent.get(p, {})
    parent[p] = d
    parent = d
Fam Zheng Feb. 12, 2014, 12:56 a.m. UTC | #2
On Tue, 02/11 14:28, Stefan Hajnoczi wrote:
> On Tue, Feb 11, 2014 at 06:45:05PM +0800, Fam Zheng wrote:
> > diff --git a/scripts/qmp/qmp-shell b/scripts/qmp/qmp-shell
> > index d374b35..9c84551 100755
> > --- a/scripts/qmp/qmp-shell
> > +++ b/scripts/qmp/qmp-shell
> > @@ -112,7 +112,14 @@ class QMPShell(qmp.QEMUMonitorProtocol):
> >                      value = json.loads(opt[1])
> >                  else:
> >                      value = opt[1]
> > -            qmpcmd['arguments'][opt[0]] = value
> > +            optpath = opt[0].split('.')
> > +            parent = qmpcmd['arguments']
> > +            for p in optpath[:-1]:
> > +                if not p in parent:
> > +                    d = dict()
> > +                    parent[p] = d
> > +                parent = d
> 
> d is a stale reference when the path component already exists (e.g.
> a.b.c=1 a.d=2).  Since 'a' already exists when processing 'a.d' we'll
> the value of d will actually be the a.b dict!
> 
> I think you need the following instead:
> 
> for p in optpath[:-1]:
>     d = parent.get(p, {})
>     parent[p] = d
>     parent = d

Yes, thanks. And we should check the contradictive case "foo=a foo.bar=b". Will
send v2.

Fam
diff mbox

Patch

diff --git a/scripts/qmp/qmp-shell b/scripts/qmp/qmp-shell
index d374b35..9c84551 100755
--- a/scripts/qmp/qmp-shell
+++ b/scripts/qmp/qmp-shell
@@ -112,7 +112,14 @@  class QMPShell(qmp.QEMUMonitorProtocol):
                     value = json.loads(opt[1])
                 else:
                     value = opt[1]
-            qmpcmd['arguments'][opt[0]] = value
+            optpath = opt[0].split('.')
+            parent = qmpcmd['arguments']
+            for p in optpath[:-1]:
+                if not p in parent:
+                    d = dict()
+                    parent[p] = d
+                parent = d
+            parent[optpath[-1]] = value
         return qmpcmd
 
     def _execute_cmd(self, cmdline):