qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH] qmp: add support for mixed typed input visitor


From: Daniel P. Berrange
Subject: [Qemu-devel] [PATCH] qmp: add support for mixed typed input visitor
Date: Thu, 14 Jul 2016 15:16:10 +0100

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 <address@hidden>
---

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 via email to

[Prev in Thread] Current Thread [Next in Thread]