2
0

qobject-input-visitor.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. * Input Visitor
  3. *
  4. * Copyright (C) 2012-2017 Red Hat, Inc.
  5. * Copyright IBM, Corp. 2011
  6. *
  7. * Authors:
  8. * Anthony Liguori <aliguori@us.ibm.com>
  9. *
  10. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  11. * See the COPYING.LIB file in the top-level directory.
  12. *
  13. */
  14. #include "qemu/osdep.h"
  15. #include <math.h>
  16. #include "qapi/error.h"
  17. #include "qapi/qobject-input-visitor.h"
  18. #include "qapi/visitor-impl.h"
  19. #include "qemu/queue.h"
  20. #include "qapi/qmp/qjson.h"
  21. #include "qapi/qmp/qbool.h"
  22. #include "qapi/qmp/qdict.h"
  23. #include "qapi/qmp/qerror.h"
  24. #include "qapi/qmp/qlist.h"
  25. #include "qapi/qmp/qnull.h"
  26. #include "qapi/qmp/qnum.h"
  27. #include "qapi/qmp/qstring.h"
  28. #include "qemu/cutils.h"
  29. #include "qemu/option.h"
  30. typedef struct StackObject {
  31. const char *name; /* Name of @obj in its parent, if any */
  32. QObject *obj; /* QDict or QList being visited */
  33. void *qapi; /* sanity check that caller uses same pointer */
  34. GHashTable *h; /* If @obj is QDict: unvisited keys */
  35. const QListEntry *entry; /* If @obj is QList: unvisited tail */
  36. unsigned index; /* If @obj is QList: list index of @entry */
  37. QSLIST_ENTRY(StackObject) node; /* parent */
  38. } StackObject;
  39. struct QObjectInputVisitor {
  40. Visitor visitor;
  41. /* Root of visit at visitor creation. */
  42. QObject *root;
  43. bool keyval; /* Assume @root made with keyval_parse() */
  44. /* Stack of objects being visited (all entries will be either
  45. * QDict or QList). */
  46. QSLIST_HEAD(, StackObject) stack;
  47. GString *errname; /* Accumulator for full_name() */
  48. };
  49. static QObjectInputVisitor *to_qiv(Visitor *v)
  50. {
  51. return container_of(v, QObjectInputVisitor, visitor);
  52. }
  53. /*
  54. * Find the full name of something @qiv is currently visiting.
  55. * @qiv is visiting something named @name in the stack of containers
  56. * @qiv->stack.
  57. * If @n is zero, return its full name.
  58. * If @n is positive, return the full name of the @n-th container
  59. * counting from the top. The stack of containers must have at least
  60. * @n elements.
  61. * The returned string is valid until the next full_name_nth(@v) or
  62. * destruction of @v.
  63. */
  64. static const char *full_name_nth(QObjectInputVisitor *qiv, const char *name,
  65. int n)
  66. {
  67. StackObject *so;
  68. char buf[32];
  69. if (qiv->errname) {
  70. g_string_truncate(qiv->errname, 0);
  71. } else {
  72. qiv->errname = g_string_new("");
  73. }
  74. QSLIST_FOREACH(so , &qiv->stack, node) {
  75. if (n) {
  76. n--;
  77. } else if (qobject_type(so->obj) == QTYPE_QDICT) {
  78. g_string_prepend(qiv->errname, name ?: "<anonymous>");
  79. g_string_prepend_c(qiv->errname, '.');
  80. } else {
  81. snprintf(buf, sizeof(buf),
  82. qiv->keyval ? ".%u" : "[%u]",
  83. so->index);
  84. g_string_prepend(qiv->errname, buf);
  85. }
  86. name = so->name;
  87. }
  88. assert(!n);
  89. if (name) {
  90. g_string_prepend(qiv->errname, name);
  91. } else if (qiv->errname->str[0] == '.') {
  92. g_string_erase(qiv->errname, 0, 1);
  93. } else if (!qiv->errname->str[0]) {
  94. return "<anonymous>";
  95. }
  96. return qiv->errname->str;
  97. }
  98. static const char *full_name(QObjectInputVisitor *qiv, const char *name)
  99. {
  100. return full_name_nth(qiv, name, 0);
  101. }
  102. static QObject *qobject_input_try_get_object(QObjectInputVisitor *qiv,
  103. const char *name,
  104. bool consume)
  105. {
  106. StackObject *tos;
  107. QObject *qobj;
  108. QObject *ret;
  109. if (QSLIST_EMPTY(&qiv->stack)) {
  110. /* Starting at root, name is ignored. */
  111. assert(qiv->root);
  112. return qiv->root;
  113. }
  114. /* We are in a container; find the next element. */
  115. tos = QSLIST_FIRST(&qiv->stack);
  116. qobj = tos->obj;
  117. assert(qobj);
  118. if (qobject_type(qobj) == QTYPE_QDICT) {
  119. assert(name);
  120. ret = qdict_get(qobject_to(QDict, qobj), name);
  121. if (tos->h && consume && ret) {
  122. bool removed = g_hash_table_remove(tos->h, name);
  123. assert(removed);
  124. }
  125. } else {
  126. assert(qobject_type(qobj) == QTYPE_QLIST);
  127. assert(!name);
  128. if (tos->entry) {
  129. ret = qlist_entry_obj(tos->entry);
  130. if (consume) {
  131. tos->entry = qlist_next(tos->entry);
  132. }
  133. } else {
  134. ret = NULL;
  135. }
  136. if (consume) {
  137. tos->index++;
  138. }
  139. }
  140. return ret;
  141. }
  142. static QObject *qobject_input_get_object(QObjectInputVisitor *qiv,
  143. const char *name,
  144. bool consume, Error **errp)
  145. {
  146. QObject *obj = qobject_input_try_get_object(qiv, name, consume);
  147. if (!obj) {
  148. error_setg(errp, QERR_MISSING_PARAMETER, full_name(qiv, name));
  149. }
  150. return obj;
  151. }
  152. static const char *qobject_input_get_keyval(QObjectInputVisitor *qiv,
  153. const char *name,
  154. Error **errp)
  155. {
  156. QObject *qobj;
  157. QString *qstr;
  158. qobj = qobject_input_get_object(qiv, name, true, errp);
  159. if (!qobj) {
  160. return NULL;
  161. }
  162. qstr = qobject_to(QString, qobj);
  163. if (!qstr) {
  164. switch (qobject_type(qobj)) {
  165. case QTYPE_QDICT:
  166. case QTYPE_QLIST:
  167. error_setg(errp, "Parameters '%s.*' are unexpected",
  168. full_name(qiv, name));
  169. return NULL;
  170. default:
  171. /* Non-string scalar (should this be an assertion?) */
  172. error_setg(errp, "Internal error: parameter %s invalid",
  173. full_name(qiv, name));
  174. return NULL;
  175. }
  176. }
  177. return qstring_get_str(qstr);
  178. }
  179. static const QListEntry *qobject_input_push(QObjectInputVisitor *qiv,
  180. const char *name,
  181. QObject *obj, void *qapi)
  182. {
  183. GHashTable *h;
  184. StackObject *tos = g_new0(StackObject, 1);
  185. QDict *qdict = qobject_to(QDict, obj);
  186. QList *qlist = qobject_to(QList, obj);
  187. const QDictEntry *entry;
  188. assert(obj);
  189. tos->name = name;
  190. tos->obj = obj;
  191. tos->qapi = qapi;
  192. if (qdict) {
  193. h = g_hash_table_new(g_str_hash, g_str_equal);
  194. for (entry = qdict_first(qdict);
  195. entry;
  196. entry = qdict_next(qdict, entry)) {
  197. g_hash_table_insert(h, (void *)qdict_entry_key(entry), NULL);
  198. }
  199. tos->h = h;
  200. } else {
  201. assert(qlist);
  202. tos->entry = qlist_first(qlist);
  203. tos->index = -1;
  204. }
  205. QSLIST_INSERT_HEAD(&qiv->stack, tos, node);
  206. return tos->entry;
  207. }
  208. static void qobject_input_check_struct(Visitor *v, Error **errp)
  209. {
  210. QObjectInputVisitor *qiv = to_qiv(v);
  211. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  212. GHashTableIter iter;
  213. const char *key;
  214. assert(tos && !tos->entry);
  215. g_hash_table_iter_init(&iter, tos->h);
  216. if (g_hash_table_iter_next(&iter, (void **)&key, NULL)) {
  217. error_setg(errp, "Parameter '%s' is unexpected",
  218. full_name(qiv, key));
  219. }
  220. }
  221. static void qobject_input_stack_object_free(StackObject *tos)
  222. {
  223. if (tos->h) {
  224. g_hash_table_unref(tos->h);
  225. }
  226. g_free(tos);
  227. }
  228. static void qobject_input_pop(Visitor *v, void **obj)
  229. {
  230. QObjectInputVisitor *qiv = to_qiv(v);
  231. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  232. assert(tos && tos->qapi == obj);
  233. QSLIST_REMOVE_HEAD(&qiv->stack, node);
  234. qobject_input_stack_object_free(tos);
  235. }
  236. static void qobject_input_start_struct(Visitor *v, const char *name, void **obj,
  237. size_t size, Error **errp)
  238. {
  239. QObjectInputVisitor *qiv = to_qiv(v);
  240. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  241. if (obj) {
  242. *obj = NULL;
  243. }
  244. if (!qobj) {
  245. return;
  246. }
  247. if (qobject_type(qobj) != QTYPE_QDICT) {
  248. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  249. full_name(qiv, name), "object");
  250. return;
  251. }
  252. qobject_input_push(qiv, name, qobj, obj);
  253. if (obj) {
  254. *obj = g_malloc0(size);
  255. }
  256. }
  257. static void qobject_input_end_struct(Visitor *v, void **obj)
  258. {
  259. QObjectInputVisitor *qiv = to_qiv(v);
  260. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  261. assert(qobject_type(tos->obj) == QTYPE_QDICT && tos->h);
  262. qobject_input_pop(v, obj);
  263. }
  264. static void qobject_input_start_list(Visitor *v, const char *name,
  265. GenericList **list, size_t size,
  266. Error **errp)
  267. {
  268. QObjectInputVisitor *qiv = to_qiv(v);
  269. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  270. const QListEntry *entry;
  271. if (list) {
  272. *list = NULL;
  273. }
  274. if (!qobj) {
  275. return;
  276. }
  277. if (qobject_type(qobj) != QTYPE_QLIST) {
  278. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  279. full_name(qiv, name), "array");
  280. return;
  281. }
  282. entry = qobject_input_push(qiv, name, qobj, list);
  283. if (entry && list) {
  284. *list = g_malloc0(size);
  285. }
  286. }
  287. static GenericList *qobject_input_next_list(Visitor *v, GenericList *tail,
  288. size_t size)
  289. {
  290. QObjectInputVisitor *qiv = to_qiv(v);
  291. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  292. assert(tos && qobject_to(QList, tos->obj));
  293. if (!tos->entry) {
  294. return NULL;
  295. }
  296. tail->next = g_malloc0(size);
  297. return tail->next;
  298. }
  299. static void qobject_input_check_list(Visitor *v, Error **errp)
  300. {
  301. QObjectInputVisitor *qiv = to_qiv(v);
  302. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  303. assert(tos && qobject_to(QList, tos->obj));
  304. if (tos->entry) {
  305. error_setg(errp, "Only %u list elements expected in %s",
  306. tos->index + 1, full_name_nth(qiv, NULL, 1));
  307. }
  308. }
  309. static void qobject_input_end_list(Visitor *v, void **obj)
  310. {
  311. QObjectInputVisitor *qiv = to_qiv(v);
  312. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  313. assert(qobject_type(tos->obj) == QTYPE_QLIST && !tos->h);
  314. qobject_input_pop(v, obj);
  315. }
  316. static void qobject_input_start_alternate(Visitor *v, const char *name,
  317. GenericAlternate **obj, size_t size,
  318. Error **errp)
  319. {
  320. QObjectInputVisitor *qiv = to_qiv(v);
  321. QObject *qobj = qobject_input_get_object(qiv, name, false, errp);
  322. if (!qobj) {
  323. *obj = NULL;
  324. return;
  325. }
  326. *obj = g_malloc0(size);
  327. (*obj)->type = qobject_type(qobj);
  328. }
  329. static void qobject_input_type_int64(Visitor *v, const char *name, int64_t *obj,
  330. Error **errp)
  331. {
  332. QObjectInputVisitor *qiv = to_qiv(v);
  333. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  334. QNum *qnum;
  335. if (!qobj) {
  336. return;
  337. }
  338. qnum = qobject_to(QNum, qobj);
  339. if (!qnum || !qnum_get_try_int(qnum, obj)) {
  340. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  341. full_name(qiv, name), "integer");
  342. }
  343. }
  344. static void qobject_input_type_int64_keyval(Visitor *v, const char *name,
  345. int64_t *obj, Error **errp)
  346. {
  347. QObjectInputVisitor *qiv = to_qiv(v);
  348. const char *str = qobject_input_get_keyval(qiv, name, errp);
  349. if (!str) {
  350. return;
  351. }
  352. if (qemu_strtoi64(str, NULL, 0, obj) < 0) {
  353. /* TODO report -ERANGE more nicely */
  354. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  355. full_name(qiv, name), "integer");
  356. }
  357. }
  358. static void qobject_input_type_uint64(Visitor *v, const char *name,
  359. uint64_t *obj, Error **errp)
  360. {
  361. QObjectInputVisitor *qiv = to_qiv(v);
  362. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  363. QNum *qnum;
  364. int64_t val;
  365. if (!qobj) {
  366. return;
  367. }
  368. qnum = qobject_to(QNum, qobj);
  369. if (!qnum) {
  370. goto err;
  371. }
  372. if (qnum_get_try_uint(qnum, obj)) {
  373. return;
  374. }
  375. /* Need to accept negative values for backward compatibility */
  376. if (qnum_get_try_int(qnum, &val)) {
  377. *obj = val;
  378. return;
  379. }
  380. err:
  381. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  382. full_name(qiv, name), "uint64");
  383. }
  384. static void qobject_input_type_uint64_keyval(Visitor *v, const char *name,
  385. uint64_t *obj, Error **errp)
  386. {
  387. QObjectInputVisitor *qiv = to_qiv(v);
  388. const char *str = qobject_input_get_keyval(qiv, name, errp);
  389. if (!str) {
  390. return;
  391. }
  392. if (qemu_strtou64(str, NULL, 0, obj) < 0) {
  393. /* TODO report -ERANGE more nicely */
  394. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  395. full_name(qiv, name), "integer");
  396. }
  397. }
  398. static void qobject_input_type_bool(Visitor *v, const char *name, bool *obj,
  399. Error **errp)
  400. {
  401. QObjectInputVisitor *qiv = to_qiv(v);
  402. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  403. QBool *qbool;
  404. if (!qobj) {
  405. return;
  406. }
  407. qbool = qobject_to(QBool, qobj);
  408. if (!qbool) {
  409. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  410. full_name(qiv, name), "boolean");
  411. return;
  412. }
  413. *obj = qbool_get_bool(qbool);
  414. }
  415. static void qobject_input_type_bool_keyval(Visitor *v, const char *name,
  416. bool *obj, Error **errp)
  417. {
  418. QObjectInputVisitor *qiv = to_qiv(v);
  419. const char *str = qobject_input_get_keyval(qiv, name, errp);
  420. if (!str) {
  421. return;
  422. }
  423. if (!strcmp(str, "on")) {
  424. *obj = true;
  425. } else if (!strcmp(str, "off")) {
  426. *obj = false;
  427. } else {
  428. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  429. full_name(qiv, name), "'on' or 'off'");
  430. }
  431. }
  432. static void qobject_input_type_str(Visitor *v, const char *name, char **obj,
  433. Error **errp)
  434. {
  435. QObjectInputVisitor *qiv = to_qiv(v);
  436. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  437. QString *qstr;
  438. *obj = NULL;
  439. if (!qobj) {
  440. return;
  441. }
  442. qstr = qobject_to(QString, qobj);
  443. if (!qstr) {
  444. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  445. full_name(qiv, name), "string");
  446. return;
  447. }
  448. *obj = g_strdup(qstring_get_str(qstr));
  449. }
  450. static void qobject_input_type_str_keyval(Visitor *v, const char *name,
  451. char **obj, Error **errp)
  452. {
  453. QObjectInputVisitor *qiv = to_qiv(v);
  454. const char *str = qobject_input_get_keyval(qiv, name, errp);
  455. *obj = g_strdup(str);
  456. }
  457. static void qobject_input_type_number(Visitor *v, const char *name, double *obj,
  458. Error **errp)
  459. {
  460. QObjectInputVisitor *qiv = to_qiv(v);
  461. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  462. QNum *qnum;
  463. if (!qobj) {
  464. return;
  465. }
  466. qnum = qobject_to(QNum, qobj);
  467. if (!qnum) {
  468. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  469. full_name(qiv, name), "number");
  470. return;
  471. }
  472. *obj = qnum_get_double(qnum);
  473. }
  474. static void qobject_input_type_number_keyval(Visitor *v, const char *name,
  475. double *obj, Error **errp)
  476. {
  477. QObjectInputVisitor *qiv = to_qiv(v);
  478. const char *str = qobject_input_get_keyval(qiv, name, errp);
  479. double val;
  480. if (!str) {
  481. return;
  482. }
  483. if (qemu_strtod_finite(str, NULL, &val)) {
  484. /* TODO report -ERANGE more nicely */
  485. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  486. full_name(qiv, name), "number");
  487. return;
  488. }
  489. *obj = val;
  490. }
  491. static void qobject_input_type_any(Visitor *v, const char *name, QObject **obj,
  492. Error **errp)
  493. {
  494. QObjectInputVisitor *qiv = to_qiv(v);
  495. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  496. *obj = NULL;
  497. if (!qobj) {
  498. return;
  499. }
  500. *obj = qobject_ref(qobj);
  501. }
  502. static void qobject_input_type_null(Visitor *v, const char *name,
  503. QNull **obj, Error **errp)
  504. {
  505. QObjectInputVisitor *qiv = to_qiv(v);
  506. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  507. *obj = NULL;
  508. if (!qobj) {
  509. return;
  510. }
  511. if (qobject_type(qobj) != QTYPE_QNULL) {
  512. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  513. full_name(qiv, name), "null");
  514. return;
  515. }
  516. *obj = qnull();
  517. }
  518. static void qobject_input_type_size_keyval(Visitor *v, const char *name,
  519. uint64_t *obj, Error **errp)
  520. {
  521. QObjectInputVisitor *qiv = to_qiv(v);
  522. const char *str = qobject_input_get_keyval(qiv, name, errp);
  523. if (!str) {
  524. return;
  525. }
  526. if (qemu_strtosz(str, NULL, obj) < 0) {
  527. /* TODO report -ERANGE more nicely */
  528. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  529. full_name(qiv, name), "size");
  530. }
  531. }
  532. static void qobject_input_optional(Visitor *v, const char *name, bool *present)
  533. {
  534. QObjectInputVisitor *qiv = to_qiv(v);
  535. QObject *qobj = qobject_input_try_get_object(qiv, name, false);
  536. if (!qobj) {
  537. *present = false;
  538. return;
  539. }
  540. *present = true;
  541. }
  542. static void qobject_input_free(Visitor *v)
  543. {
  544. QObjectInputVisitor *qiv = to_qiv(v);
  545. while (!QSLIST_EMPTY(&qiv->stack)) {
  546. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  547. QSLIST_REMOVE_HEAD(&qiv->stack, node);
  548. qobject_input_stack_object_free(tos);
  549. }
  550. qobject_unref(qiv->root);
  551. if (qiv->errname) {
  552. g_string_free(qiv->errname, TRUE);
  553. }
  554. g_free(qiv);
  555. }
  556. static QObjectInputVisitor *qobject_input_visitor_base_new(QObject *obj)
  557. {
  558. QObjectInputVisitor *v = g_malloc0(sizeof(*v));
  559. assert(obj);
  560. v->visitor.type = VISITOR_INPUT;
  561. v->visitor.start_struct = qobject_input_start_struct;
  562. v->visitor.check_struct = qobject_input_check_struct;
  563. v->visitor.end_struct = qobject_input_end_struct;
  564. v->visitor.start_list = qobject_input_start_list;
  565. v->visitor.next_list = qobject_input_next_list;
  566. v->visitor.check_list = qobject_input_check_list;
  567. v->visitor.end_list = qobject_input_end_list;
  568. v->visitor.start_alternate = qobject_input_start_alternate;
  569. v->visitor.optional = qobject_input_optional;
  570. v->visitor.free = qobject_input_free;
  571. v->root = qobject_ref(obj);
  572. return v;
  573. }
  574. Visitor *qobject_input_visitor_new(QObject *obj)
  575. {
  576. QObjectInputVisitor *v = qobject_input_visitor_base_new(obj);
  577. v->visitor.type_int64 = qobject_input_type_int64;
  578. v->visitor.type_uint64 = qobject_input_type_uint64;
  579. v->visitor.type_bool = qobject_input_type_bool;
  580. v->visitor.type_str = qobject_input_type_str;
  581. v->visitor.type_number = qobject_input_type_number;
  582. v->visitor.type_any = qobject_input_type_any;
  583. v->visitor.type_null = qobject_input_type_null;
  584. return &v->visitor;
  585. }
  586. Visitor *qobject_input_visitor_new_keyval(QObject *obj)
  587. {
  588. QObjectInputVisitor *v = qobject_input_visitor_base_new(obj);
  589. v->visitor.type_int64 = qobject_input_type_int64_keyval;
  590. v->visitor.type_uint64 = qobject_input_type_uint64_keyval;
  591. v->visitor.type_bool = qobject_input_type_bool_keyval;
  592. v->visitor.type_str = qobject_input_type_str_keyval;
  593. v->visitor.type_number = qobject_input_type_number_keyval;
  594. v->visitor.type_any = qobject_input_type_any;
  595. v->visitor.type_null = qobject_input_type_null;
  596. v->visitor.type_size = qobject_input_type_size_keyval;
  597. v->keyval = true;
  598. return &v->visitor;
  599. }
  600. Visitor *qobject_input_visitor_new_str(const char *str,
  601. const char *implied_key,
  602. Error **errp)
  603. {
  604. bool is_json = str[0] == '{';
  605. QObject *obj;
  606. QDict *args;
  607. Visitor *v;
  608. if (is_json) {
  609. obj = qobject_from_json(str, errp);
  610. if (!obj) {
  611. return NULL;
  612. }
  613. args = qobject_to(QDict, obj);
  614. assert(args);
  615. v = qobject_input_visitor_new(QOBJECT(args));
  616. } else {
  617. args = keyval_parse(str, implied_key, errp);
  618. if (!args) {
  619. return NULL;
  620. }
  621. v = qobject_input_visitor_new_keyval(QOBJECT(args));
  622. }
  623. qobject_unref(args);
  624. return v;
  625. }