qobject-input-visitor.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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 (!qapi_bool_parse(name, str, obj, NULL)) {
  439. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  440. full_name(qiv, name), "'on' or 'off'");
  441. return false;
  442. }
  443. return true;
  444. }
  445. static bool qobject_input_type_str(Visitor *v, const char *name, char **obj,
  446. Error **errp)
  447. {
  448. QObjectInputVisitor *qiv = to_qiv(v);
  449. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  450. QString *qstr;
  451. *obj = NULL;
  452. if (!qobj) {
  453. return false;
  454. }
  455. qstr = qobject_to(QString, qobj);
  456. if (!qstr) {
  457. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  458. full_name(qiv, name), "string");
  459. return false;
  460. }
  461. *obj = g_strdup(qstring_get_str(qstr));
  462. return true;
  463. }
  464. static bool qobject_input_type_str_keyval(Visitor *v, const char *name,
  465. char **obj, Error **errp)
  466. {
  467. QObjectInputVisitor *qiv = to_qiv(v);
  468. const char *str = qobject_input_get_keyval(qiv, name, errp);
  469. *obj = g_strdup(str);
  470. return !!str;
  471. }
  472. static bool qobject_input_type_number(Visitor *v, const char *name, double *obj,
  473. Error **errp)
  474. {
  475. QObjectInputVisitor *qiv = to_qiv(v);
  476. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  477. QNum *qnum;
  478. if (!qobj) {
  479. return false;
  480. }
  481. qnum = qobject_to(QNum, qobj);
  482. if (!qnum) {
  483. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  484. full_name(qiv, name), "number");
  485. return false;
  486. }
  487. *obj = qnum_get_double(qnum);
  488. return true;
  489. }
  490. static bool qobject_input_type_number_keyval(Visitor *v, const char *name,
  491. double *obj, Error **errp)
  492. {
  493. QObjectInputVisitor *qiv = to_qiv(v);
  494. const char *str = qobject_input_get_keyval(qiv, name, errp);
  495. double val;
  496. if (!str) {
  497. return false;
  498. }
  499. if (qemu_strtod_finite(str, NULL, &val)) {
  500. /* TODO report -ERANGE more nicely */
  501. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  502. full_name(qiv, name), "number");
  503. return false;
  504. }
  505. *obj = val;
  506. return true;
  507. }
  508. static bool qobject_input_type_any(Visitor *v, const char *name, QObject **obj,
  509. Error **errp)
  510. {
  511. QObjectInputVisitor *qiv = to_qiv(v);
  512. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  513. *obj = NULL;
  514. if (!qobj) {
  515. return false;
  516. }
  517. *obj = qobject_ref(qobj);
  518. return true;
  519. }
  520. static bool qobject_input_type_null(Visitor *v, const char *name,
  521. QNull **obj, Error **errp)
  522. {
  523. QObjectInputVisitor *qiv = to_qiv(v);
  524. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  525. *obj = NULL;
  526. if (!qobj) {
  527. return false;
  528. }
  529. if (qobject_type(qobj) != QTYPE_QNULL) {
  530. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  531. full_name(qiv, name), "null");
  532. return false;
  533. }
  534. *obj = qnull();
  535. return true;
  536. }
  537. static bool qobject_input_type_size_keyval(Visitor *v, const char *name,
  538. uint64_t *obj, Error **errp)
  539. {
  540. QObjectInputVisitor *qiv = to_qiv(v);
  541. const char *str = qobject_input_get_keyval(qiv, name, errp);
  542. if (!str) {
  543. return false;
  544. }
  545. if (qemu_strtosz(str, NULL, obj) < 0) {
  546. /* TODO report -ERANGE more nicely */
  547. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  548. full_name(qiv, name), "size");
  549. return false;
  550. }
  551. return true;
  552. }
  553. static void qobject_input_optional(Visitor *v, const char *name, bool *present)
  554. {
  555. QObjectInputVisitor *qiv = to_qiv(v);
  556. QObject *qobj = qobject_input_try_get_object(qiv, name, false);
  557. if (!qobj) {
  558. *present = false;
  559. return;
  560. }
  561. *present = true;
  562. }
  563. static void qobject_input_free(Visitor *v)
  564. {
  565. QObjectInputVisitor *qiv = to_qiv(v);
  566. while (!QSLIST_EMPTY(&qiv->stack)) {
  567. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  568. QSLIST_REMOVE_HEAD(&qiv->stack, node);
  569. qobject_input_stack_object_free(tos);
  570. }
  571. qobject_unref(qiv->root);
  572. if (qiv->errname) {
  573. g_string_free(qiv->errname, TRUE);
  574. }
  575. g_free(qiv);
  576. }
  577. static QObjectInputVisitor *qobject_input_visitor_base_new(QObject *obj)
  578. {
  579. QObjectInputVisitor *v = g_malloc0(sizeof(*v));
  580. assert(obj);
  581. v->visitor.type = VISITOR_INPUT;
  582. v->visitor.start_struct = qobject_input_start_struct;
  583. v->visitor.check_struct = qobject_input_check_struct;
  584. v->visitor.end_struct = qobject_input_end_struct;
  585. v->visitor.start_list = qobject_input_start_list;
  586. v->visitor.next_list = qobject_input_next_list;
  587. v->visitor.check_list = qobject_input_check_list;
  588. v->visitor.end_list = qobject_input_end_list;
  589. v->visitor.start_alternate = qobject_input_start_alternate;
  590. v->visitor.optional = qobject_input_optional;
  591. v->visitor.free = qobject_input_free;
  592. v->root = qobject_ref(obj);
  593. return v;
  594. }
  595. Visitor *qobject_input_visitor_new(QObject *obj)
  596. {
  597. QObjectInputVisitor *v = qobject_input_visitor_base_new(obj);
  598. v->visitor.type_int64 = qobject_input_type_int64;
  599. v->visitor.type_uint64 = qobject_input_type_uint64;
  600. v->visitor.type_bool = qobject_input_type_bool;
  601. v->visitor.type_str = qobject_input_type_str;
  602. v->visitor.type_number = qobject_input_type_number;
  603. v->visitor.type_any = qobject_input_type_any;
  604. v->visitor.type_null = qobject_input_type_null;
  605. return &v->visitor;
  606. }
  607. Visitor *qobject_input_visitor_new_keyval(QObject *obj)
  608. {
  609. QObjectInputVisitor *v = qobject_input_visitor_base_new(obj);
  610. v->visitor.type_int64 = qobject_input_type_int64_keyval;
  611. v->visitor.type_uint64 = qobject_input_type_uint64_keyval;
  612. v->visitor.type_bool = qobject_input_type_bool_keyval;
  613. v->visitor.type_str = qobject_input_type_str_keyval;
  614. v->visitor.type_number = qobject_input_type_number_keyval;
  615. v->visitor.type_any = qobject_input_type_any;
  616. v->visitor.type_null = qobject_input_type_null;
  617. v->visitor.type_size = qobject_input_type_size_keyval;
  618. v->keyval = true;
  619. return &v->visitor;
  620. }
  621. Visitor *qobject_input_visitor_new_str(const char *str,
  622. const char *implied_key,
  623. Error **errp)
  624. {
  625. bool is_json = str[0] == '{';
  626. QObject *obj;
  627. QDict *args;
  628. Visitor *v;
  629. if (is_json) {
  630. obj = qobject_from_json(str, errp);
  631. if (!obj) {
  632. return NULL;
  633. }
  634. args = qobject_to(QDict, obj);
  635. assert(args);
  636. v = qobject_input_visitor_new(QOBJECT(args));
  637. } else {
  638. args = keyval_parse(str, implied_key, NULL, errp);
  639. if (!args) {
  640. return NULL;
  641. }
  642. v = qobject_input_visitor_new_keyval(QOBJECT(args));
  643. }
  644. qobject_unref(args);
  645. return v;
  646. }