qemu: qapi: Use declarative approach for meta-type parsers in virQEMUQAPISchemaTraverse

Introduce an array of callbacks for given 'meta-type' of the QAPI schema
structure rather than using code to select it. This will simplify
extension for the other meta-types which are not handled yet.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2019-04-10 14:22:14 +02:00
parent 0b49f891be
commit c3944f8fe7

View File

@ -164,12 +164,30 @@ virQEMUQAPISchemaTraverseCommand(virJSONValuePtr cur,
} }
typedef int (*virQEMUQAPISchemaTraverseFunc)(virJSONValuePtr cur,
struct virQEMUQAPISchemaTraverseContext *ctxt);
struct virQEMUQAPISchemaTraverseMetaType {
const char *metatype;
virQEMUQAPISchemaTraverseFunc func;
};
static const struct virQEMUQAPISchemaTraverseMetaType traverseMetaType[] = {
{ "object", virQEMUQAPISchemaTraverseObject },
{ "array", virQEMUQAPISchemaTraverseArray },
{ "command", virQEMUQAPISchemaTraverseCommand },
{ "event", virQEMUQAPISchemaTraverseCommand },
};
static int static int
virQEMUQAPISchemaTraverse(const char *baseName, virQEMUQAPISchemaTraverse(const char *baseName,
struct virQEMUQAPISchemaTraverseContext *ctxt) struct virQEMUQAPISchemaTraverseContext *ctxt)
{ {
virJSONValuePtr cur; virJSONValuePtr cur;
const char *metatype; const char *metatype;
size_t i;
if (!(cur = virHashLookup(ctxt->schema, baseName))) if (!(cur = virHashLookup(ctxt->schema, baseName)))
return 0; return 0;
@ -182,16 +200,9 @@ virQEMUQAPISchemaTraverse(const char *baseName,
if (!(metatype = virJSONValueObjectGetString(cur, "meta-type"))) if (!(metatype = virJSONValueObjectGetString(cur, "meta-type")))
return 0; return 0;
if (STREQ(metatype, "array")) { for (i = 0; i < ARRAY_CARDINALITY(traverseMetaType); i++) {
return virQEMUQAPISchemaTraverseArray(cur, ctxt); if (STREQ(metatype, traverseMetaType[i].metatype))
} else if (STREQ(metatype, "object")) { return traverseMetaType[i].func(cur, ctxt);
return virQEMUQAPISchemaTraverseObject(cur, ctxt);
} else if (STREQ(metatype, "command") ||
STREQ(metatype, "event")) {
return virQEMUQAPISchemaTraverseCommand(cur, ctxt);
} else {
/* alternates, basic types and enums can't be entered */
return 0;
} }
return 0; return 0;