提交 22075109 编写于 作者: P Peter Krempa

virQEMUBuildCommandLineJSON: Allow skipping certain keys

Allow reusing this for formatting of netdev_add arguments into -netdev.
We need to be able to skip the 'type' property as it's used without the
prefix by our generator.

Add infrastructure which allows skipping property with a specific name.
Signed-off-by: NPeter Krempa <pkrempa@redhat.com>
Reviewed-by: NEric Blake <eblake@redhat.com>
上级 54e43282
...@@ -36,6 +36,7 @@ VIR_LOG_INIT("util.qemu"); ...@@ -36,6 +36,7 @@ VIR_LOG_INIT("util.qemu");
struct virQEMUCommandLineJSONIteratorData { struct virQEMUCommandLineJSONIteratorData {
const char *prefix; const char *prefix;
virBufferPtr buf; virBufferPtr buf;
const char *skipKey;
virQEMUBuildCommandLineJSONArrayFormatFunc arrayFunc; virQEMUBuildCommandLineJSONArrayFormatFunc arrayFunc;
}; };
...@@ -44,6 +45,7 @@ static int ...@@ -44,6 +45,7 @@ static int
virQEMUBuildCommandLineJSONRecurse(const char *key, virQEMUBuildCommandLineJSONRecurse(const char *key,
virJSONValuePtr value, virJSONValuePtr value,
virBufferPtr buf, virBufferPtr buf,
const char *skipKey,
virQEMUBuildCommandLineJSONArrayFormatFunc arrayFunc, virQEMUBuildCommandLineJSONArrayFormatFunc arrayFunc,
bool nested); bool nested);
...@@ -52,7 +54,8 @@ virQEMUBuildCommandLineJSONRecurse(const char *key, ...@@ -52,7 +54,8 @@ virQEMUBuildCommandLineJSONRecurse(const char *key,
int int
virQEMUBuildCommandLineJSONArrayBitmap(const char *key, virQEMUBuildCommandLineJSONArrayBitmap(const char *key,
virJSONValuePtr array, virJSONValuePtr array,
virBufferPtr buf) virBufferPtr buf,
const char *skipKey G_GNUC_UNUSED)
{ {
ssize_t pos = -1; ssize_t pos = -1;
ssize_t end; ssize_t end;
...@@ -80,7 +83,8 @@ virQEMUBuildCommandLineJSONArrayBitmap(const char *key, ...@@ -80,7 +83,8 @@ virQEMUBuildCommandLineJSONArrayBitmap(const char *key,
int int
virQEMUBuildCommandLineJSONArrayNumbered(const char *key, virQEMUBuildCommandLineJSONArrayNumbered(const char *key,
virJSONValuePtr array, virJSONValuePtr array,
virBufferPtr buf) virBufferPtr buf,
const char *skipKey)
{ {
virJSONValuePtr member; virJSONValuePtr member;
size_t i; size_t i;
...@@ -91,7 +95,7 @@ virQEMUBuildCommandLineJSONArrayNumbered(const char *key, ...@@ -91,7 +95,7 @@ virQEMUBuildCommandLineJSONArrayNumbered(const char *key,
prefix = g_strdup_printf("%s.%zu", key, i); prefix = g_strdup_printf("%s.%zu", key, i);
if (virQEMUBuildCommandLineJSONRecurse(prefix, member, buf, if (virQEMUBuildCommandLineJSONRecurse(prefix, member, buf, skipKey,
virQEMUBuildCommandLineJSONArrayNumbered, virQEMUBuildCommandLineJSONArrayNumbered,
true) < 0) true) < 0)
return 0; return 0;
...@@ -109,15 +113,20 @@ virQEMUBuildCommandLineJSONIterate(const char *key, ...@@ -109,15 +113,20 @@ virQEMUBuildCommandLineJSONIterate(const char *key,
{ {
struct virQEMUCommandLineJSONIteratorData *data = opaque; struct virQEMUCommandLineJSONIteratorData *data = opaque;
if (STREQ_NULLABLE(key, data->skipKey))
return 0;
if (data->prefix) { if (data->prefix) {
g_autofree char *tmpkey = NULL; g_autofree char *tmpkey = NULL;
tmpkey = g_strdup_printf("%s.%s", data->prefix, key); tmpkey = g_strdup_printf("%s.%s", data->prefix, key);
return virQEMUBuildCommandLineJSONRecurse(tmpkey, value, data->buf, return virQEMUBuildCommandLineJSONRecurse(tmpkey, value, data->buf,
data->skipKey,
data->arrayFunc, false); data->arrayFunc, false);
} else { } else {
return virQEMUBuildCommandLineJSONRecurse(key, value, data->buf, return virQEMUBuildCommandLineJSONRecurse(key, value, data->buf,
data->skipKey,
data->arrayFunc, false); data->arrayFunc, false);
} }
} }
...@@ -127,10 +136,11 @@ static int ...@@ -127,10 +136,11 @@ static int
virQEMUBuildCommandLineJSONRecurse(const char *key, virQEMUBuildCommandLineJSONRecurse(const char *key,
virJSONValuePtr value, virJSONValuePtr value,
virBufferPtr buf, virBufferPtr buf,
const char *skipKey,
virQEMUBuildCommandLineJSONArrayFormatFunc arrayFunc, virQEMUBuildCommandLineJSONArrayFormatFunc arrayFunc,
bool nested) bool nested)
{ {
struct virQEMUCommandLineJSONIteratorData data = { key, buf, arrayFunc }; struct virQEMUCommandLineJSONIteratorData data = { key, buf, skipKey, arrayFunc };
virJSONType type = virJSONValueGetType(value); virJSONType type = virJSONValueGetType(value);
virJSONValuePtr elem; virJSONValuePtr elem;
bool tmp; bool tmp;
...@@ -170,14 +180,14 @@ virQEMUBuildCommandLineJSONRecurse(const char *key, ...@@ -170,14 +180,14 @@ virQEMUBuildCommandLineJSONRecurse(const char *key,
return -1; return -1;
} }
if (!arrayFunc || arrayFunc(key, value, buf) < 0) { if (!arrayFunc || arrayFunc(key, value, buf, skipKey) < 0) {
/* fallback, treat the array as a non-bitmap, adding the key /* fallback, treat the array as a non-bitmap, adding the key
* for each member */ * for each member */
for (i = 0; i < virJSONValueArraySize(value); i++) { for (i = 0; i < virJSONValueArraySize(value); i++) {
elem = virJSONValueArrayGet((virJSONValuePtr)value, i); elem = virJSONValueArrayGet((virJSONValuePtr)value, i);
/* recurse to avoid duplicating code */ /* recurse to avoid duplicating code */
if (virQEMUBuildCommandLineJSONRecurse(key, elem, buf, if (virQEMUBuildCommandLineJSONRecurse(key, elem, buf, skipKey,
arrayFunc, true) < 0) arrayFunc, true) < 0)
return -1; return -1;
} }
...@@ -205,6 +215,7 @@ virQEMUBuildCommandLineJSONRecurse(const char *key, ...@@ -205,6 +215,7 @@ virQEMUBuildCommandLineJSONRecurse(const char *key,
* virQEMUBuildCommandLineJSON: * virQEMUBuildCommandLineJSON:
* @value: json object containing the value * @value: json object containing the value
* @buf: otuput buffer * @buf: otuput buffer
* @skipKey: name of key that will be handled separately by caller
* @arrayFunc: array formatter function to allow for different syntax * @arrayFunc: array formatter function to allow for different syntax
* *
* Formats JSON value object into command line parameters suitable for use with * Formats JSON value object into command line parameters suitable for use with
...@@ -215,9 +226,10 @@ virQEMUBuildCommandLineJSONRecurse(const char *key, ...@@ -215,9 +226,10 @@ virQEMUBuildCommandLineJSONRecurse(const char *key,
int int
virQEMUBuildCommandLineJSON(virJSONValuePtr value, virQEMUBuildCommandLineJSON(virJSONValuePtr value,
virBufferPtr buf, virBufferPtr buf,
const char *skipKey,
virQEMUBuildCommandLineJSONArrayFormatFunc array) virQEMUBuildCommandLineJSONArrayFormatFunc array)
{ {
if (virQEMUBuildCommandLineJSONRecurse(NULL, value, buf, array, false) < 0) if (virQEMUBuildCommandLineJSONRecurse(NULL, value, buf, skipKey, array, false) < 0)
return -1; return -1;
virBufferTrim(buf, ","); virBufferTrim(buf, ",");
...@@ -243,7 +255,7 @@ virQEMUBuildObjectCommandlineFromJSONInternal(virBufferPtr buf, ...@@ -243,7 +255,7 @@ virQEMUBuildObjectCommandlineFromJSONInternal(virBufferPtr buf,
if (props) { if (props) {
virBufferAddLit(buf, ","); virBufferAddLit(buf, ",");
if (virQEMUBuildCommandLineJSON(props, buf, if (virQEMUBuildCommandLineJSON(props, buf, NULL,
virQEMUBuildCommandLineJSONArrayBitmap) < 0) virQEMUBuildCommandLineJSONArrayBitmap) < 0)
return -1; return -1;
} }
...@@ -270,7 +282,7 @@ virQEMUBuildDriveCommandlineFromJSON(virJSONValuePtr srcdef) ...@@ -270,7 +282,7 @@ virQEMUBuildDriveCommandlineFromJSON(virJSONValuePtr srcdef)
virBuffer buf = VIR_BUFFER_INITIALIZER; virBuffer buf = VIR_BUFFER_INITIALIZER;
char *ret = NULL; char *ret = NULL;
if (virQEMUBuildCommandLineJSON(srcdef, &buf, if (virQEMUBuildCommandLineJSON(srcdef, &buf, NULL,
virQEMUBuildCommandLineJSONArrayNumbered) < 0) virQEMUBuildCommandLineJSONArrayNumbered) < 0)
goto cleanup; goto cleanup;
......
...@@ -29,16 +29,20 @@ ...@@ -29,16 +29,20 @@
typedef int (*virQEMUBuildCommandLineJSONArrayFormatFunc)(const char *key, typedef int (*virQEMUBuildCommandLineJSONArrayFormatFunc)(const char *key,
virJSONValuePtr array, virJSONValuePtr array,
virBufferPtr buf); virBufferPtr buf,
const char *skipKey);
int virQEMUBuildCommandLineJSONArrayBitmap(const char *key, int virQEMUBuildCommandLineJSONArrayBitmap(const char *key,
virJSONValuePtr array, virJSONValuePtr array,
virBufferPtr buf); virBufferPtr buf,
const char *skipKey);
int virQEMUBuildCommandLineJSONArrayNumbered(const char *key, int virQEMUBuildCommandLineJSONArrayNumbered(const char *key,
virJSONValuePtr array, virJSONValuePtr array,
virBufferPtr buf); virBufferPtr buf,
const char *skipKey);
int virQEMUBuildCommandLineJSON(virJSONValuePtr value, int virQEMUBuildCommandLineJSON(virJSONValuePtr value,
virBufferPtr buf, virBufferPtr buf,
const char *skipKey,
virQEMUBuildCommandLineJSONArrayFormatFunc array); virQEMUBuildCommandLineJSONArrayFormatFunc array);
int virQEMUBuildObjectCommandlineFromJSON(virBufferPtr buf, int virQEMUBuildObjectCommandlineFromJSON(virBufferPtr buf,
......
...@@ -47,7 +47,7 @@ testQemuCommandBuildFromJSON(const void *opaque) ...@@ -47,7 +47,7 @@ testQemuCommandBuildFromJSON(const void *opaque)
return -1; return -1;
} }
if (virQEMUBuildCommandLineJSON(val, &buf, data->arrayfunc) < 0) { if (virQEMUBuildCommandLineJSON(val, &buf, NULL, data->arrayfunc) < 0) {
fprintf(stderr, fprintf(stderr,
"\nvirQEMUBuildCommandlineJSON failed process JSON:\n%s\n", "\nvirQEMUBuildCommandlineJSON failed process JSON:\n%s\n",
data->props); data->props);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册