qobject-input-visitor.c 20 KB

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