qobject-input-visitor.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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 bool 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. return false;
  220. }
  221. return true;
  222. }
  223. static void qobject_input_stack_object_free(StackObject *tos)
  224. {
  225. if (tos->h) {
  226. g_hash_table_unref(tos->h);
  227. }
  228. g_free(tos);
  229. }
  230. static void qobject_input_pop(Visitor *v, void **obj)
  231. {
  232. QObjectInputVisitor *qiv = to_qiv(v);
  233. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  234. assert(tos && tos->qapi == obj);
  235. QSLIST_REMOVE_HEAD(&qiv->stack, node);
  236. qobject_input_stack_object_free(tos);
  237. }
  238. static bool qobject_input_start_struct(Visitor *v, const char *name, void **obj,
  239. size_t size, Error **errp)
  240. {
  241. QObjectInputVisitor *qiv = to_qiv(v);
  242. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  243. if (obj) {
  244. *obj = NULL;
  245. }
  246. if (!qobj) {
  247. return false;
  248. }
  249. if (qobject_type(qobj) != QTYPE_QDICT) {
  250. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  251. full_name(qiv, name), "object");
  252. return false;
  253. }
  254. qobject_input_push(qiv, name, qobj, obj);
  255. if (obj) {
  256. *obj = g_malloc0(size);
  257. }
  258. return true;
  259. }
  260. static void qobject_input_end_struct(Visitor *v, void **obj)
  261. {
  262. QObjectInputVisitor *qiv = to_qiv(v);
  263. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  264. assert(qobject_type(tos->obj) == QTYPE_QDICT && tos->h);
  265. qobject_input_pop(v, obj);
  266. }
  267. static bool qobject_input_start_list(Visitor *v, const char *name,
  268. GenericList **list, size_t size,
  269. Error **errp)
  270. {
  271. QObjectInputVisitor *qiv = to_qiv(v);
  272. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  273. const QListEntry *entry;
  274. if (list) {
  275. *list = NULL;
  276. }
  277. if (!qobj) {
  278. return false;
  279. }
  280. if (qobject_type(qobj) != QTYPE_QLIST) {
  281. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  282. full_name(qiv, name), "array");
  283. return false;
  284. }
  285. entry = qobject_input_push(qiv, name, qobj, list);
  286. if (entry && list) {
  287. *list = g_malloc0(size);
  288. }
  289. return true;
  290. }
  291. static GenericList *qobject_input_next_list(Visitor *v, GenericList *tail,
  292. size_t size)
  293. {
  294. QObjectInputVisitor *qiv = to_qiv(v);
  295. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  296. assert(tos && qobject_to(QList, tos->obj));
  297. if (!tos->entry) {
  298. return NULL;
  299. }
  300. tail->next = g_malloc0(size);
  301. return tail->next;
  302. }
  303. static bool qobject_input_check_list(Visitor *v, Error **errp)
  304. {
  305. QObjectInputVisitor *qiv = to_qiv(v);
  306. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  307. assert(tos && qobject_to(QList, tos->obj));
  308. if (tos->entry) {
  309. error_setg(errp, "Only %u list elements expected in %s",
  310. tos->index + 1, full_name_nth(qiv, NULL, 1));
  311. return false;
  312. }
  313. return true;
  314. }
  315. static void qobject_input_end_list(Visitor *v, void **obj)
  316. {
  317. QObjectInputVisitor *qiv = to_qiv(v);
  318. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  319. assert(qobject_type(tos->obj) == QTYPE_QLIST && !tos->h);
  320. qobject_input_pop(v, obj);
  321. }
  322. static bool qobject_input_start_alternate(Visitor *v, const char *name,
  323. GenericAlternate **obj, size_t size,
  324. Error **errp)
  325. {
  326. QObjectInputVisitor *qiv = to_qiv(v);
  327. QObject *qobj = qobject_input_get_object(qiv, name, false, errp);
  328. if (!qobj) {
  329. *obj = NULL;
  330. return false;
  331. }
  332. *obj = g_malloc0(size);
  333. (*obj)->type = qobject_type(qobj);
  334. return true;
  335. }
  336. static bool qobject_input_type_int64(Visitor *v, const char *name, int64_t *obj,
  337. Error **errp)
  338. {
  339. QObjectInputVisitor *qiv = to_qiv(v);
  340. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  341. QNum *qnum;
  342. if (!qobj) {
  343. return false;
  344. }
  345. qnum = qobject_to(QNum, qobj);
  346. if (!qnum || !qnum_get_try_int(qnum, obj)) {
  347. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  348. full_name(qiv, name), "integer");
  349. return false;
  350. }
  351. return true;
  352. }
  353. static bool qobject_input_type_int64_keyval(Visitor *v, const char *name,
  354. int64_t *obj, Error **errp)
  355. {
  356. QObjectInputVisitor *qiv = to_qiv(v);
  357. const char *str = qobject_input_get_keyval(qiv, name, errp);
  358. if (!str) {
  359. return false;
  360. }
  361. if (qemu_strtoi64(str, NULL, 0, obj) < 0) {
  362. /* TODO report -ERANGE more nicely */
  363. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  364. full_name(qiv, name), "integer");
  365. return false;
  366. }
  367. return true;
  368. }
  369. static bool qobject_input_type_uint64(Visitor *v, const char *name,
  370. uint64_t *obj, Error **errp)
  371. {
  372. QObjectInputVisitor *qiv = to_qiv(v);
  373. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  374. QNum *qnum;
  375. int64_t val;
  376. if (!qobj) {
  377. return false;
  378. }
  379. qnum = qobject_to(QNum, qobj);
  380. if (!qnum) {
  381. goto err;
  382. }
  383. if (qnum_get_try_uint(qnum, obj)) {
  384. return true;
  385. }
  386. /* Need to accept negative values for backward compatibility */
  387. if (qnum_get_try_int(qnum, &val)) {
  388. *obj = val;
  389. return true;
  390. }
  391. err:
  392. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  393. full_name(qiv, name), "uint64");
  394. return false;
  395. }
  396. static bool qobject_input_type_uint64_keyval(Visitor *v, const char *name,
  397. uint64_t *obj, Error **errp)
  398. {
  399. QObjectInputVisitor *qiv = to_qiv(v);
  400. const char *str = qobject_input_get_keyval(qiv, name, errp);
  401. if (!str) {
  402. return false;
  403. }
  404. if (qemu_strtou64(str, NULL, 0, obj) < 0) {
  405. /* TODO report -ERANGE more nicely */
  406. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  407. full_name(qiv, name), "integer");
  408. return false;
  409. }
  410. return true;
  411. }
  412. static bool qobject_input_type_bool(Visitor *v, const char *name, bool *obj,
  413. Error **errp)
  414. {
  415. QObjectInputVisitor *qiv = to_qiv(v);
  416. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  417. QBool *qbool;
  418. if (!qobj) {
  419. return false;
  420. }
  421. qbool = qobject_to(QBool, qobj);
  422. if (!qbool) {
  423. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  424. full_name(qiv, name), "boolean");
  425. return false;
  426. }
  427. *obj = qbool_get_bool(qbool);
  428. return true;
  429. }
  430. static bool qobject_input_type_bool_keyval(Visitor *v, const char *name,
  431. bool *obj, Error **errp)
  432. {
  433. QObjectInputVisitor *qiv = to_qiv(v);
  434. const char *str = qobject_input_get_keyval(qiv, name, errp);
  435. if (!str) {
  436. return false;
  437. }
  438. if (!strcmp(str, "on")) {
  439. *obj = true;
  440. } else if (!strcmp(str, "off")) {
  441. *obj = false;
  442. } else {
  443. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  444. full_name(qiv, name), "'on' or 'off'");
  445. return false;
  446. }
  447. return true;
  448. }
  449. static bool qobject_input_type_str(Visitor *v, const char *name, char **obj,
  450. Error **errp)
  451. {
  452. QObjectInputVisitor *qiv = to_qiv(v);
  453. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  454. QString *qstr;
  455. *obj = NULL;
  456. if (!qobj) {
  457. return false;
  458. }
  459. qstr = qobject_to(QString, qobj);
  460. if (!qstr) {
  461. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  462. full_name(qiv, name), "string");
  463. return false;
  464. }
  465. *obj = g_strdup(qstring_get_str(qstr));
  466. return true;
  467. }
  468. static bool qobject_input_type_str_keyval(Visitor *v, const char *name,
  469. char **obj, Error **errp)
  470. {
  471. QObjectInputVisitor *qiv = to_qiv(v);
  472. const char *str = qobject_input_get_keyval(qiv, name, errp);
  473. *obj = g_strdup(str);
  474. return !!str;
  475. }
  476. static bool qobject_input_type_number(Visitor *v, const char *name, double *obj,
  477. Error **errp)
  478. {
  479. QObjectInputVisitor *qiv = to_qiv(v);
  480. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  481. QNum *qnum;
  482. if (!qobj) {
  483. return false;
  484. }
  485. qnum = qobject_to(QNum, qobj);
  486. if (!qnum) {
  487. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  488. full_name(qiv, name), "number");
  489. return false;
  490. }
  491. *obj = qnum_get_double(qnum);
  492. return true;
  493. }
  494. static bool qobject_input_type_number_keyval(Visitor *v, const char *name,
  495. double *obj, Error **errp)
  496. {
  497. QObjectInputVisitor *qiv = to_qiv(v);
  498. const char *str = qobject_input_get_keyval(qiv, name, errp);
  499. double val;
  500. if (!str) {
  501. return false;
  502. }
  503. if (qemu_strtod_finite(str, NULL, &val)) {
  504. /* TODO report -ERANGE more nicely */
  505. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  506. full_name(qiv, name), "number");
  507. return false;
  508. }
  509. *obj = val;
  510. return true;
  511. }
  512. static bool qobject_input_type_any(Visitor *v, const char *name, QObject **obj,
  513. Error **errp)
  514. {
  515. QObjectInputVisitor *qiv = to_qiv(v);
  516. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  517. *obj = NULL;
  518. if (!qobj) {
  519. return false;
  520. }
  521. *obj = qobject_ref(qobj);
  522. return true;
  523. }
  524. static bool qobject_input_type_null(Visitor *v, const char *name,
  525. QNull **obj, Error **errp)
  526. {
  527. QObjectInputVisitor *qiv = to_qiv(v);
  528. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  529. *obj = NULL;
  530. if (!qobj) {
  531. return false;
  532. }
  533. if (qobject_type(qobj) != QTYPE_QNULL) {
  534. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  535. full_name(qiv, name), "null");
  536. return false;
  537. }
  538. *obj = qnull();
  539. return true;
  540. }
  541. static bool qobject_input_type_size_keyval(Visitor *v, const char *name,
  542. uint64_t *obj, Error **errp)
  543. {
  544. QObjectInputVisitor *qiv = to_qiv(v);
  545. const char *str = qobject_input_get_keyval(qiv, name, errp);
  546. if (!str) {
  547. return false;
  548. }
  549. if (qemu_strtosz(str, NULL, obj) < 0) {
  550. /* TODO report -ERANGE more nicely */
  551. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  552. full_name(qiv, name), "size");
  553. return false;
  554. }
  555. return true;
  556. }
  557. static void qobject_input_optional(Visitor *v, const char *name, bool *present)
  558. {
  559. QObjectInputVisitor *qiv = to_qiv(v);
  560. QObject *qobj = qobject_input_try_get_object(qiv, name, false);
  561. if (!qobj) {
  562. *present = false;
  563. return;
  564. }
  565. *present = true;
  566. }
  567. static void qobject_input_free(Visitor *v)
  568. {
  569. QObjectInputVisitor *qiv = to_qiv(v);
  570. while (!QSLIST_EMPTY(&qiv->stack)) {
  571. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  572. QSLIST_REMOVE_HEAD(&qiv->stack, node);
  573. qobject_input_stack_object_free(tos);
  574. }
  575. qobject_unref(qiv->root);
  576. if (qiv->errname) {
  577. g_string_free(qiv->errname, TRUE);
  578. }
  579. g_free(qiv);
  580. }
  581. static QObjectInputVisitor *qobject_input_visitor_base_new(QObject *obj)
  582. {
  583. QObjectInputVisitor *v = g_malloc0(sizeof(*v));
  584. assert(obj);
  585. v->visitor.type = VISITOR_INPUT;
  586. v->visitor.start_struct = qobject_input_start_struct;
  587. v->visitor.check_struct = qobject_input_check_struct;
  588. v->visitor.end_struct = qobject_input_end_struct;
  589. v->visitor.start_list = qobject_input_start_list;
  590. v->visitor.next_list = qobject_input_next_list;
  591. v->visitor.check_list = qobject_input_check_list;
  592. v->visitor.end_list = qobject_input_end_list;
  593. v->visitor.start_alternate = qobject_input_start_alternate;
  594. v->visitor.optional = qobject_input_optional;
  595. v->visitor.free = qobject_input_free;
  596. v->root = qobject_ref(obj);
  597. return v;
  598. }
  599. Visitor *qobject_input_visitor_new(QObject *obj)
  600. {
  601. QObjectInputVisitor *v = qobject_input_visitor_base_new(obj);
  602. v->visitor.type_int64 = qobject_input_type_int64;
  603. v->visitor.type_uint64 = qobject_input_type_uint64;
  604. v->visitor.type_bool = qobject_input_type_bool;
  605. v->visitor.type_str = qobject_input_type_str;
  606. v->visitor.type_number = qobject_input_type_number;
  607. v->visitor.type_any = qobject_input_type_any;
  608. v->visitor.type_null = qobject_input_type_null;
  609. return &v->visitor;
  610. }
  611. Visitor *qobject_input_visitor_new_keyval(QObject *obj)
  612. {
  613. QObjectInputVisitor *v = qobject_input_visitor_base_new(obj);
  614. v->visitor.type_int64 = qobject_input_type_int64_keyval;
  615. v->visitor.type_uint64 = qobject_input_type_uint64_keyval;
  616. v->visitor.type_bool = qobject_input_type_bool_keyval;
  617. v->visitor.type_str = qobject_input_type_str_keyval;
  618. v->visitor.type_number = qobject_input_type_number_keyval;
  619. v->visitor.type_any = qobject_input_type_any;
  620. v->visitor.type_null = qobject_input_type_null;
  621. v->visitor.type_size = qobject_input_type_size_keyval;
  622. v->keyval = true;
  623. return &v->visitor;
  624. }
  625. Visitor *qobject_input_visitor_new_str(const char *str,
  626. const char *implied_key,
  627. Error **errp)
  628. {
  629. bool is_json = str[0] == '{';
  630. QObject *obj;
  631. QDict *args;
  632. Visitor *v;
  633. if (is_json) {
  634. obj = qobject_from_json(str, errp);
  635. if (!obj) {
  636. return NULL;
  637. }
  638. args = qobject_to(QDict, obj);
  639. assert(args);
  640. v = qobject_input_visitor_new(QOBJECT(args));
  641. } else {
  642. args = keyval_parse(str, implied_key, errp);
  643. if (!args) {
  644. return NULL;
  645. }
  646. v = qobject_input_visitor_new_keyval(QOBJECT(args));
  647. }
  648. qobject_unref(args);
  649. return v;
  650. }