qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrange" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH] qmp: add support for mixed typed input visitor
Date: Thu, 14 Jul 2016 15:16:10 +0100	[thread overview]
Message-ID: <1468505770-20694-1-git-send-email-berrange@redhat.com> (raw)

Add a qmp_mixed_input_visitor_new() method which returns
a QMP input visitor that accepts either strings or the
native data types.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---

NB, just a demo - this should have tests added before submitting
for real.

 include/qapi/qmp-input-visitor.h | 22 +++++++++
 qapi/qmp-input-visitor.c         | 99 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 121 insertions(+)

diff --git a/include/qapi/qmp-input-visitor.h b/include/qapi/qmp-input-visitor.h
index 33439b7..6bf5a59 100644
--- a/include/qapi/qmp-input-visitor.h
+++ b/include/qapi/qmp-input-visitor.h
@@ -61,4 +61,26 @@ Visitor *qmp_input_visitor_new(QObject *obj, bool strict);
  */
 Visitor *qmp_string_input_visitor_new(QObject *obj, bool strict);
 
+/**
+ * qmp_mixed_input_visitor_new:
+ * @obj: the input object to visit
+ * @strict: whether to require that all input keys are consumed
+ *
+ * Create a new input visitor that converts QMP to QAPI.
+ *
+ * Any scalar values in the @obj input data structure can either be
+ * represented as the native data type, or as strings. ie if visiting
+ * a boolean, the value can be a QBoolean or a QString whose contents
+ * represent a valid boolean.
+ *
+ * If @strict is set to true, then an error will be reported if any
+ * dict keys are not consumed during visitation.
+ *
+ * The returned input visitor should be released by calling
+ * qmp_input_visitor_cleanup when no longer required.
+ *
+ * Returns: a new input visitor
+ */
+Visitor *qmp_mixed_input_visitor_new(QObject *obj, bool strict);
+
 #endif
diff --git a/qapi/qmp-input-visitor.c b/qapi/qmp-input-visitor.c
index 307155f..2d7e6e6 100644
--- a/qapi/qmp-input-visitor.c
+++ b/qapi/qmp-input-visitor.c
@@ -280,6 +280,19 @@ static void qmp_input_type_int64_str(Visitor *v, const char *name, int64_t *obj,
     *obj = ret;
 }
 
+static void qmp_input_type_int64_mixed(Visitor *v, const char *name, int64_t *obj,
+                                       Error **errp)
+{
+    QmpInputVisitor *qiv = to_qiv(v);
+    QObject *qobj = qmp_input_get_object(qiv, name, true);
+
+    if (qobj && qobj->type == QTYPE_QSTRING) {
+        qmp_input_type_int64_str(v, name, obj, errp);
+    } else {
+        qmp_input_type_int64(v, name, obj, errp);
+    }
+}
+
 static void qmp_input_type_uint64(Visitor *v, const char *name, uint64_t *obj,
                                   Error **errp)
 {
@@ -305,6 +318,19 @@ static void qmp_input_type_uint64_str(Visitor *v, const char *name,
     parse_option_number(name, qstr ? qstr->string : NULL, obj, errp);
 }
 
+static void qmp_input_type_uint64_mixed(Visitor *v, const char *name,
+                                        uint64_t *obj, Error **errp)
+{
+    QmpInputVisitor *qiv = to_qiv(v);
+    QObject *qobj = qmp_input_get_object(qiv, name, true);
+
+    if (qobj && qobj->type == QTYPE_QSTRING) {
+        qmp_input_type_uint64_str(v, name, obj, errp);
+    } else {
+        qmp_input_type_uint64(v, name, obj, errp);
+    }
+}
+
 static void qmp_input_type_bool(Visitor *v, const char *name, bool *obj,
                                 Error **errp)
 {
@@ -329,6 +355,19 @@ static void qmp_input_type_bool_str(Visitor *v, const char *name, bool *obj,
     parse_option_bool(name, qstr ? qstr->string : NULL, obj, errp);
 }
 
+static void qmp_input_type_bool_mixed(Visitor *v, const char *name,
+                                      bool *obj, Error **errp)
+{
+    QmpInputVisitor *qiv = to_qiv(v);
+    QObject *qobj = qmp_input_get_object(qiv, name, true);
+
+    if (qobj && qobj->type == QTYPE_QSTRING) {
+        qmp_input_type_bool_str(v, name, obj, errp);
+    } else {
+        qmp_input_type_bool(v, name, obj, errp);
+    }
+}
+
 static void qmp_input_type_str(Visitor *v, const char *name, char **obj,
                                Error **errp)
 {
@@ -388,6 +427,19 @@ static void qmp_input_type_number_str(Visitor *v, const char *name, double *obj,
                "number");
 }
 
+static void qmp_input_type_number_mixed(Visitor *v, const char *name,
+                                        double *obj, Error **errp)
+{
+    QmpInputVisitor *qiv = to_qiv(v);
+    QObject *qobj = qmp_input_get_object(qiv, name, true);
+
+    if (qobj && qobj->type == QTYPE_QSTRING) {
+        qmp_input_type_number_str(v, name, obj, errp);
+    } else {
+        qmp_input_type_number(v, name, obj, errp);
+    }
+}
+
 static void qmp_input_type_any(Visitor *v, const char *name, QObject **obj,
                                Error **errp)
 {
@@ -434,6 +486,21 @@ static void qmp_input_type_size_str(Visitor *v, const char *name, uint64_t *obj,
                "size");
 }
 
+
+static void qmp_input_type_size_mixed(Visitor *v, const char *name,
+                                      uint64_t *obj, Error **errp)
+{
+    QmpInputVisitor *qiv = to_qiv(v);
+    QObject *qobj = qmp_input_get_object(qiv, name, true);
+
+    if (qobj && qobj->type == QTYPE_QSTRING) {
+        qmp_input_type_size_str(v, name, obj, errp);
+    } else {
+        qmp_input_type_uint64(v, name, obj, errp);
+    }
+}
+
+
 static void qmp_input_optional(Visitor *v, const char *name, bool *present)
 {
     QmpInputVisitor *qiv = to_qiv(v);
@@ -517,3 +584,35 @@ Visitor *qmp_string_input_visitor_new(QObject *obj, bool strict)
 
     return &v->visitor;
 }
+
+Visitor *qmp_mixed_input_visitor_new(QObject *obj, bool strict)
+{
+    QmpInputVisitor *v;
+
+    v = g_malloc0(sizeof(*v));
+
+    v->visitor.type = VISITOR_INPUT;
+    v->visitor.start_struct = qmp_input_start_struct;
+    v->visitor.check_struct = qmp_input_check_struct;
+    v->visitor.end_struct = qmp_input_pop;
+    v->visitor.start_list = qmp_input_start_list;
+    v->visitor.next_list = qmp_input_next_list;
+    v->visitor.end_list = qmp_input_pop;
+    v->visitor.start_alternate = qmp_input_start_alternate;
+    v->visitor.type_int64 = qmp_input_type_int64_mixed;
+    v->visitor.type_uint64 = qmp_input_type_uint64_mixed;
+    v->visitor.type_bool = qmp_input_type_bool_mixed;
+    v->visitor.type_str = qmp_input_type_str;
+    v->visitor.type_number = qmp_input_type_number_mixed;
+    v->visitor.type_any = qmp_input_type_any;
+    v->visitor.type_null = qmp_input_type_null;
+    v->visitor.type_size = qmp_input_type_size_mixed;
+    v->visitor.optional = qmp_input_optional;
+    v->visitor.free = qmp_input_free;
+    v->strict = strict;
+
+    v->root = obj;
+    qobject_incref(obj);
+
+    return &v->visitor;
+}
-- 
2.7.4

             reply	other threads:[~2016-07-14 15:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-14 14:16 Daniel P. Berrange [this message]
2016-07-14 14:23 ` [Qemu-devel] [PATCH] qmp: add support for mixed typed input visitor Eric Blake
2016-07-14 14:39   ` Daniel P. Berrange
2016-07-14 16:28     ` Eric Blake
2016-07-15 12:48       ` Markus Armbruster
2016-07-20  9:02         ` Markus Armbruster

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1468505770-20694-1-git-send-email-berrange@redhat.com \
    --to=berrange@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).