* [Qemu-devel] [PATCH 1/5] qapi: Add QAPI_TO_QOBJECT() convenience macro
2017-07-17 15:56 [Qemu-devel] [PATCH 0/5] Easier conversion from qapi to qobject Eric Blake
@ 2017-07-17 15:56 ` Eric Blake
2017-07-17 15:56 ` [Qemu-devel] [PATCH 2/5] nbd: Use simpler QAPI_TO_QOBJECT() Eric Blake
` (4 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Eric Blake @ 2017-07-17 15:56 UTC (permalink / raw)
To: qemu-devel; +Cc: armbru, Michael Roth
We have several callers that want to convert a QAPI C type into
a QObject; right now all of them have to copy the same boilerplate
of creating a visitor. A convenience macro makes this paradigm
easier to type.
Signed-off-by: Eric Blake <eblake@redhat.com>
---
include/qapi/qobject-output-visitor.h | 19 +++++++++++++++++++
qapi/qobject-output-visitor.c | 16 ++++++++++++++++
2 files changed, 35 insertions(+)
diff --git a/include/qapi/qobject-output-visitor.h b/include/qapi/qobject-output-visitor.h
index e5a3490812..f6066ce537 100644
--- a/include/qapi/qobject-output-visitor.h
+++ b/include/qapi/qobject-output-visitor.h
@@ -16,6 +16,8 @@
#include "qapi/visitor.h"
#include "qapi/qmp/qobject.h"
+#include "qapi/error.h"
+#include "qapi-visit.h"
typedef struct QObjectOutputVisitor QObjectOutputVisitor;
@@ -54,4 +56,21 @@ typedef struct QObjectOutputVisitor QObjectOutputVisitor;
*/
Visitor *qobject_output_visitor_new(QObject **result);
+QObject *qapi_to_qobject(const void *src,
+ void (*visit_type)(Visitor *, const char *,
+ void **, Error **),
+ Error **errp);
+
+/*
+ * Create a QObject from a QAPI object @src of the given @type.
+ *
+ * Not usable on QAPI scalars (integers, strings, enums), nor on a
+ * QAPI object that references the 'any' type. @src must not be NULL.
+ */
+#define QAPI_TO_QOBJECT(type, src, err) \
+ (qapi_to_qobject(1 ? (src) : (type *)NULL, \
+ (void (*)(Visitor *, const char *, void**, \
+ Error **))visit_type_ ## type, \
+ err))
+
#endif
diff --git a/qapi/qobject-output-visitor.c b/qapi/qobject-output-visitor.c
index 70be84ccb5..0b1f098fa1 100644
--- a/qapi/qobject-output-visitor.c
+++ b/qapi/qobject-output-visitor.c
@@ -251,3 +251,19 @@ Visitor *qobject_output_visitor_new(QObject **result)
return &v->visitor;
}
+
+QObject *qapi_to_qobject(const void *src,
+ void (*visit_type)(Visitor *, const char *,
+ void **, Error **),
+ Error **errp)
+{
+ Visitor *v;
+ void *s = (void *) src; /* cast away const */
+ QObject *dst = NULL;
+
+ v = qobject_output_visitor_new(&dst);
+ visit_type(v, NULL, &s, &error_abort);
+ visit_complete(v, &dst);
+ visit_free(v);
+ return dst;
+}
--
2.13.3
^ permalink raw reply related [flat|nested] 8+ messages in thread* [Qemu-devel] [PATCH 2/5] nbd: Use simpler QAPI_TO_QOBJECT()
2017-07-17 15:56 [Qemu-devel] [PATCH 0/5] Easier conversion from qapi to qobject Eric Blake
2017-07-17 15:56 ` [Qemu-devel] [PATCH 1/5] qapi: Add QAPI_TO_QOBJECT() convenience macro Eric Blake
@ 2017-07-17 15:56 ` Eric Blake
2017-07-17 15:56 ` [Qemu-devel] [PATCH 3/5] nfs: " Eric Blake
` (3 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Eric Blake @ 2017-07-17 15:56 UTC (permalink / raw)
To: qemu-devel
Cc: armbru, Paolo Bonzini, Kevin Wolf, Max Reitz,
open list:Network Block Dev...
Use the new macro to avoid some boilerplate.
Signed-off-by: Eric Blake <eblake@redhat.com>
---
block/nbd.c | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
diff --git a/block/nbd.c b/block/nbd.c
index a50d24b50a..f90ac76d1f 100644
--- a/block/nbd.c
+++ b/block/nbd.c
@@ -519,7 +519,6 @@ static void nbd_refresh_filename(BlockDriverState *bs, QDict *options)
BDRVNBDState *s = bs->opaque;
QDict *opts = qdict_new();
QObject *saddr_qdict;
- Visitor *ov;
const char *host = NULL, *port = NULL, *path = NULL;
if (s->saddr->type == SOCKET_ADDRESS_TYPE_INET) {
@@ -548,10 +547,9 @@ static void nbd_refresh_filename(BlockDriverState *bs, QDict *options)
"nbd://%s:%s", host, port);
}
- ov = qobject_output_visitor_new(&saddr_qdict);
- visit_type_SocketAddress(ov, NULL, &s->saddr, &error_abort);
- visit_complete(ov, &saddr_qdict);
- visit_free(ov);
+ saddr_qdict = QAPI_TO_QOBJECT(SocketAddress, s->saddr, &error_abort);
+ assert(qobject_type(saddr_qdict) == QTYPE_QDICT);
+
qdict_put_obj(opts, "server", saddr_qdict);
if (s->export) {
--
2.13.3
^ permalink raw reply related [flat|nested] 8+ messages in thread* [Qemu-devel] [PATCH 3/5] nfs: Use simpler QAPI_TO_QOBJECT()
2017-07-17 15:56 [Qemu-devel] [PATCH 0/5] Easier conversion from qapi to qobject Eric Blake
2017-07-17 15:56 ` [Qemu-devel] [PATCH 1/5] qapi: Add QAPI_TO_QOBJECT() convenience macro Eric Blake
2017-07-17 15:56 ` [Qemu-devel] [PATCH 2/5] nbd: Use simpler QAPI_TO_QOBJECT() Eric Blake
@ 2017-07-17 15:56 ` Eric Blake
2017-07-17 15:56 ` [Qemu-devel] [PATCH 4/5] qapi: " Eric Blake
` (2 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Eric Blake @ 2017-07-17 15:56 UTC (permalink / raw)
To: qemu-devel
Cc: armbru, Jeff Cody, Peter Lieven, Kevin Wolf, Max Reitz,
open list:NFS
Use the new macro to avoid some boilerplate.
Signed-off-by: Eric Blake <eblake@redhat.com>
---
block/nfs.c | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
diff --git a/block/nfs.c b/block/nfs.c
index d8db419957..5f3045c1af 100644
--- a/block/nfs.c
+++ b/block/nfs.c
@@ -819,7 +819,6 @@ static void nfs_refresh_filename(BlockDriverState *bs, QDict *options)
NFSClient *client = bs->opaque;
QDict *opts = qdict_new();
QObject *server_qdict;
- Visitor *ov;
qdict_put_str(opts, "driver", "nfs");
@@ -840,9 +839,9 @@ static void nfs_refresh_filename(BlockDriverState *bs, QDict *options)
"nfs://%s%s", client->server->host, client->path);
}
- ov = qobject_output_visitor_new(&server_qdict);
- visit_type_NFSServer(ov, NULL, &client->server, &error_abort);
- visit_complete(ov, &server_qdict);
+ server_qdict = QAPI_TO_QOBJECT(NFSServer, client->server, &error_abort);
+ assert(qobject_type(server_qdict) == QTYPE_QDICT);
+
qdict_put_obj(opts, "server", server_qdict);
qdict_put_str(opts, "path", client->path);
@@ -865,7 +864,6 @@ static void nfs_refresh_filename(BlockDriverState *bs, QDict *options)
qdict_put_int(opts, "debug", client->debug);
}
- visit_free(ov);
qdict_flatten(opts);
bs->full_open_options = opts;
}
--
2.13.3
^ permalink raw reply related [flat|nested] 8+ messages in thread* [Qemu-devel] [PATCH 4/5] qapi: Use simpler QAPI_TO_QOBJECT()
2017-07-17 15:56 [Qemu-devel] [PATCH 0/5] Easier conversion from qapi to qobject Eric Blake
` (2 preceding siblings ...)
2017-07-17 15:56 ` [Qemu-devel] [PATCH 3/5] nfs: " Eric Blake
@ 2017-07-17 15:56 ` Eric Blake
2017-07-17 15:56 ` [Qemu-devel] [PATCH 5/5] blockdev: " Eric Blake
2017-07-17 16:02 ` [Qemu-devel] [PATCH 0/5] Easier conversion from qapi to qobject Eric Blake
5 siblings, 0 replies; 8+ messages in thread
From: Eric Blake @ 2017-07-17 15:56 UTC (permalink / raw)
To: qemu-devel; +Cc: armbru, Kevin Wolf, Max Reitz, open list:Block layer core
Use the new macro to avoid some boilerplate.
Signed-off-by: Eric Blake <eblake@redhat.com>
---
block/qapi.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/block/qapi.c b/block/qapi.c
index 080eb8f115..23e666f108 100644
--- a/block/qapi.c
+++ b/block/qapi.c
@@ -675,14 +675,13 @@ void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
ImageInfoSpecific *info_spec)
{
QObject *obj, *data;
- Visitor *v = qobject_output_visitor_new(&obj);
- visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
- visit_complete(v, &obj);
+ obj = QAPI_TO_QOBJECT(ImageInfoSpecific, info_spec, &error_abort);
+
+ assert(qobject_type(obj) == QTYPE_QDICT);
data = qdict_get(qobject_to_qdict(obj), "data");
dump_qobject(func_fprintf, f, 1, data);
qobject_decref(obj);
- visit_free(v);
}
void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
--
2.13.3
^ permalink raw reply related [flat|nested] 8+ messages in thread* [Qemu-devel] [PATCH 5/5] blockdev: Use simpler QAPI_TO_QOBJECT()
2017-07-17 15:56 [Qemu-devel] [PATCH 0/5] Easier conversion from qapi to qobject Eric Blake
` (3 preceding siblings ...)
2017-07-17 15:56 ` [Qemu-devel] [PATCH 4/5] qapi: " Eric Blake
@ 2017-07-17 15:56 ` Eric Blake
2017-07-17 16:02 ` [Qemu-devel] [PATCH 0/5] Easier conversion from qapi to qobject Eric Blake
5 siblings, 0 replies; 8+ messages in thread
From: Eric Blake @ 2017-07-17 15:56 UTC (permalink / raw)
To: qemu-devel; +Cc: armbru, Kevin Wolf, Max Reitz, open list:Block layer core
Use the new macro to avoid some boilerplate.
Signed-off-by: Eric Blake <eblake@redhat.com>
---
blockdev.c | 13 ++++---------
1 file changed, 4 insertions(+), 9 deletions(-)
diff --git a/blockdev.c b/blockdev.c
index 7f53cc8bb3..bf26c38195 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -3884,35 +3884,30 @@ void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
{
BlockDriverState *bs;
QObject *obj;
- Visitor *v = qobject_output_visitor_new(&obj);
QDict *qdict;
Error *local_err = NULL;
- visit_type_BlockdevOptions(v, NULL, &options, &local_err);
+ obj = QAPI_TO_QOBJECT(BlockdevOptions, options, &local_err);
if (local_err) {
error_propagate(errp, local_err);
- goto fail;
+ return;
}
- visit_complete(v, &obj);
qdict = qobject_to_qdict(obj);
qdict_flatten(qdict);
if (!qdict_get_try_str(qdict, "node-name")) {
error_setg(errp, "'node-name' must be specified for the root node");
- goto fail;
+ return;
}
bs = bds_tree_init(qdict, errp);
if (!bs) {
- goto fail;
+ return;
}
QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
-
-fail:
- visit_free(v);
}
void qmp_blockdev_del(const char *node_name, Error **errp)
--
2.13.3
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [Qemu-devel] [PATCH 0/5] Easier conversion from qapi to qobject
2017-07-17 15:56 [Qemu-devel] [PATCH 0/5] Easier conversion from qapi to qobject Eric Blake
` (4 preceding siblings ...)
2017-07-17 15:56 ` [Qemu-devel] [PATCH 5/5] blockdev: " Eric Blake
@ 2017-07-17 16:02 ` Eric Blake
2017-07-18 15:52 ` Markus Armbruster
5 siblings, 1 reply; 8+ messages in thread
From: Eric Blake @ 2017-07-17 16:02 UTC (permalink / raw)
To: qemu-devel; +Cc: armbru
[-- Attachment #1: Type: text/plain, Size: 1430 bytes --]
On 07/17/2017 10:56 AM, Eric Blake wrote:
> I'm not sure if I'm posted this before, or if it is a cleanup I've
> just been sitting on locally. In all likelihood, it will miss
> tomorrow's freeze due to lack of review time, and thus be 2.11
> material, but we'll see...
Answering myself, it was part of my larger (mostly-unreviewed) attempt
to rip out dynamic JSON parsing [1]. But it is independent enough that
it warrants its own thread for separate review.
[1] https://lists.gnu.org/archive/html/qemu-devel/2016-11/msg05425.html
>
> Eric Blake (5):
> qapi: Add QAPI_TO_QOBJECT() convenience macro
> nbd: Use simpler QAPI_TO_QOBJECT()
> nfs: Use simpler QAPI_TO_QOBJECT()
> qapi: Use simpler QAPI_TO_QOBJECT()
> blockdev: Use simpler QAPI_TO_QOBJECT()
>
> include/qapi/qobject-output-visitor.h | 19 +++++++++++++++++++
> block/nbd.c | 8 +++-----
> block/nfs.c | 8 +++-----
> block/qapi.c | 7 +++----
> blockdev.c | 13 ++++---------
> qapi/qobject-output-visitor.c | 16 ++++++++++++++++
> 6 files changed, 48 insertions(+), 23 deletions(-)
The diffstat is misleading because of the size of the additional
interface and comments.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 619 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread