Skip to content

Commit 3cf42b8

Browse files
elmarcoebblake
authored andcommitted
qlit: add qobject_from_qlit()
Instantiate a QObject* from a literal QLitObject. LitObject only supports int64_t for now. uint64_t and double aren't implemented. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Reviewed-by: Markus Armbruster <armbru@redhat.com> Message-Id: <20180305172951.2150-4-marcandre.lureau@redhat.com> Signed-off-by: Eric Blake <eblake@redhat.com>
1 parent 3d96ea4 commit 3cf42b8

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

include/qapi/qmp/qlit.h

+2
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,6 @@ struct QLitDictEntry {
5050

5151
bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs);
5252

53+
QObject *qobject_from_qlit(const QLitObject *qlit);
54+
5355
#endif /* QLIT_H */

qobject/qlit.c

+37
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "qapi/qmp/qnum.h"
2222
#include "qapi/qmp/qdict.h"
2323
#include "qapi/qmp/qstring.h"
24+
#include "qapi/qmp/qnull.h"
2425

2526
static bool qlit_equal_qdict(const QLitObject *lhs, const QDict *qdict)
2627
{
@@ -86,3 +87,39 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
8687

8788
return false;
8889
}
90+
91+
QObject *qobject_from_qlit(const QLitObject *qlit)
92+
{
93+
switch (qlit->type) {
94+
case QTYPE_QNULL:
95+
return QOBJECT(qnull());
96+
case QTYPE_QNUM:
97+
return QOBJECT(qnum_from_int(qlit->value.qnum));
98+
case QTYPE_QSTRING:
99+
return QOBJECT(qstring_from_str(qlit->value.qstr));
100+
case QTYPE_QDICT: {
101+
QDict *qdict = qdict_new();
102+
QLitDictEntry *e;
103+
104+
for (e = qlit->value.qdict; e->key; e++) {
105+
qdict_put_obj(qdict, e->key, qobject_from_qlit(&e->value));
106+
}
107+
return QOBJECT(qdict);
108+
}
109+
case QTYPE_QLIST: {
110+
QList *qlist = qlist_new();
111+
QLitObject *e;
112+
113+
for (e = qlit->value.qlist; e->type != QTYPE_NONE; e++) {
114+
qlist_append_obj(qlist, qobject_from_qlit(e));
115+
}
116+
return QOBJECT(qlist);
117+
}
118+
case QTYPE_QBOOL:
119+
return QOBJECT(qbool_from_bool(qlit->value.qbool));
120+
default:
121+
assert(0);
122+
}
123+
124+
return NULL;
125+
}

tests/check-qlit.c

+28
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010
#include "qemu/osdep.h"
1111

12+
#include "qapi/qmp/qbool.h"
1213
#include "qapi/qmp/qdict.h"
1314
#include "qapi/qmp/qlist.h"
1415
#include "qapi/qmp/qlit.h"
16+
#include "qapi/qmp/qnum.h"
1517
#include "qapi/qmp/qstring.h"
1618

1719
static QLitObject qlit = QLIT_QDICT(((QLitDictEntry[]) {
@@ -63,11 +65,37 @@ static void qlit_equal_qobject_test(void)
6365
qobject_decref(qobj);
6466
}
6567

68+
static void qobject_from_qlit_test(void)
69+
{
70+
QObject *obj, *qobj = qobject_from_qlit(&qlit);
71+
QDict *qdict;
72+
QList *bee;
73+
74+
qdict = qobject_to_qdict(qobj);
75+
g_assert_cmpint(qdict_get_int(qdict, "foo"), ==, 42);
76+
g_assert_cmpstr(qdict_get_str(qdict, "bar"), ==, "hello world");
77+
g_assert(qobject_type(qdict_get(qdict, "baz")) == QTYPE_QNULL);
78+
79+
bee = qdict_get_qlist(qdict, "bee");
80+
obj = qlist_pop(bee);
81+
g_assert_cmpint(qnum_get_int(qobject_to_qnum(obj)), ==, 43);
82+
qobject_decref(obj);
83+
obj = qlist_pop(bee);
84+
g_assert_cmpint(qnum_get_int(qobject_to_qnum(obj)), ==, 44);
85+
qobject_decref(obj);
86+
obj = qlist_pop(bee);
87+
g_assert(qbool_get_bool(qobject_to_qbool(obj)));
88+
qobject_decref(obj);
89+
90+
qobject_decref(qobj);
91+
}
92+
6693
int main(int argc, char **argv)
6794
{
6895
g_test_init(&argc, &argv, NULL);
6996

7097
g_test_add_func("/qlit/equal_qobject", qlit_equal_qobject_test);
98+
g_test_add_func("/qlit/qobject_from_qlit", qobject_from_qlit_test);
7199

72100
return g_test_run();
73101
}

0 commit comments

Comments
 (0)