[PATCH 0/2] util: qemu: Perform the 'skipKey' handling only on the top level object

Semantically we need to handle one of the keys in the top level object spearately, thus skipping it in nested objects doesn't make sense. Peter Krempa (2): virqemu: Don't strip the requested key from nested objects util: qemu: Remove 'skipKey' argument from virQEMUBuildCommandLineJSONArrayFormatFunc prototype src/util/virqemu.c | 21 +++++++++------------ src/util/virqemu.h | 12 ++++-------- 2 files changed, 13 insertions(+), 20 deletions(-) -- 2.38.1

Skipping of a specific key is needed only for the top level object to specially handle the object type. We must not pass it to any recursed printing of nested objects as skipping keys there might be surprising and also is unhandlable later when formatting the commandline. Until now this did not pose a problem but was discovered when adding a new netdev backend which has a nested config object which also has the 'type' key which was being skipped. Modern usage will prefer JSON directly but fix the commandline generator to prevent surprises. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- src/util/virqemu.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/util/virqemu.c b/src/util/virqemu.c index 9dc0eab386..15602e30db 100644 --- a/src/util/virqemu.c +++ b/src/util/virqemu.c @@ -163,8 +163,7 @@ virQEMUBuildCommandLineJSONIterate(const char *key, if (data->prefix) key = tmpkey = g_strdup_printf("%s.%s", data->prefix, key); - return virQEMUBuildCommandLineJSONRecurse(key, value, data->buf, - data->skipKey, + return virQEMUBuildCommandLineJSONRecurse(key, value, data->buf, NULL, data->arrayFunc, false); } @@ -222,14 +221,14 @@ virQEMUBuildCommandLineJSONRecurse(const char *key, return -1; } - if (arrayFunc(key, value, buf, skipKey) < 0) { + if (arrayFunc(key, value, buf, NULL) < 0) { /* fallback, treat the array as a non-bitmap, adding the key * for each member */ for (i = 0; i < virJSONValueArraySize(value); i++) { elem = virJSONValueArrayGet((virJSONValue *)value, i); /* recurse to avoid duplicating code */ - if (virQEMUBuildCommandLineJSONRecurse(key, elem, buf, skipKey, + if (virQEMUBuildCommandLineJSONRecurse(key, elem, buf, NULL, arrayFunc, true) < 0) return -1; } @@ -257,7 +256,8 @@ virQEMUBuildCommandLineJSONRecurse(const char *key, * virQEMUBuildCommandLineJSON: * @value: json object containing the value * @buf: otuput buffer - * @skipKey: name of key that will be handled separately by caller + * @skipKey: name of key inside the top level object that will be handled + * separately by caller * @arrayFunc: array formatter function to allow for different syntax * * Formats JSON value object into command line parameters suitable for use with -- 2.38.1

Since we really only need to handle key skipping in the top level object the caller doesn't at this point even pass it to the array formatting helper function. Remove the unused argument. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- src/util/virqemu.c | 13 +++++-------- src/util/virqemu.h | 12 ++++-------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/util/virqemu.c b/src/util/virqemu.c index 15602e30db..b9e57a6f4c 100644 --- a/src/util/virqemu.c +++ b/src/util/virqemu.c @@ -53,8 +53,7 @@ virQEMUBuildCommandLineJSONRecurse(const char *key, int virQEMUBuildCommandLineJSONArrayBitmap(const char *key, virJSONValue *array, - virBuffer *buf, - const char *skipKey G_GNUC_UNUSED) + virBuffer *buf) { ssize_t pos = -1; ssize_t end; @@ -90,8 +89,7 @@ virQEMUBuildCommandLineJSONArrayBitmap(const char *key, int virQEMUBuildCommandLineJSONArrayNumbered(const char *key, virJSONValue *array, - virBuffer *buf, - const char *skipKey) + virBuffer *buf) { virJSONValue *member; size_t i; @@ -102,7 +100,7 @@ virQEMUBuildCommandLineJSONArrayNumbered(const char *key, member = virJSONValueArrayGet((virJSONValue *) array, i); prefix = g_strdup_printf("%s.%zu", key, i); - if (virQEMUBuildCommandLineJSONRecurse(prefix, member, buf, skipKey, + if (virQEMUBuildCommandLineJSONRecurse(prefix, member, buf, NULL, virQEMUBuildCommandLineJSONArrayNumbered, true) < 0) return 0; @@ -127,8 +125,7 @@ virQEMUBuildCommandLineJSONArrayNumbered(const char *key, int virQEMUBuildCommandLineJSONArrayObjectsStr(const char *key, virJSONValue *array, - virBuffer *buf, - const char *skipKey G_GNUC_UNUSED) + virBuffer *buf) { g_auto(virBuffer) tmp = VIR_BUFFER_INITIALIZER; size_t i; @@ -221,7 +218,7 @@ virQEMUBuildCommandLineJSONRecurse(const char *key, return -1; } - if (arrayFunc(key, value, buf, NULL) < 0) { + if (arrayFunc(key, value, buf) < 0) { /* fallback, treat the array as a non-bitmap, adding the key * for each member */ for (i = 0; i < virJSONValueArraySize(value); i++) { diff --git a/src/util/virqemu.h b/src/util/virqemu.h index 472f24de53..be083d7545 100644 --- a/src/util/virqemu.h +++ b/src/util/virqemu.h @@ -28,20 +28,16 @@ typedef int (*virQEMUBuildCommandLineJSONArrayFormatFunc)(const char *key, virJSONValue *array, - virBuffer *buf, - const char *skipKey); + virBuffer *buf); int virQEMUBuildCommandLineJSONArrayObjectsStr(const char *key, virJSONValue *array, - virBuffer *buf, - const char *skipKey); + virBuffer *buf); int virQEMUBuildCommandLineJSONArrayBitmap(const char *key, virJSONValue *array, - virBuffer *buf, - const char *skipKey); + virBuffer *buf); int virQEMUBuildCommandLineJSONArrayNumbered(const char *key, virJSONValue *array, - virBuffer *buf, - const char *skipKey); + virBuffer *buf); int virQEMUBuildCommandLineJSON(virJSONValue *value, virBuffer *buf, -- 2.38.1

On 12/19/22 10:03, Peter Krempa wrote:
Semantically we need to handle one of the keys in the top level object spearately, thus skipping it in nested objects doesn't make sense.
Peter Krempa (2): virqemu: Don't strip the requested key from nested objects util: qemu: Remove 'skipKey' argument from virQEMUBuildCommandLineJSONArrayFormatFunc prototype
src/util/virqemu.c | 21 +++++++++------------ src/util/virqemu.h | 12 ++++-------- 2 files changed, 13 insertions(+), 20 deletions(-)
Reviewed-by: Michal Privoznik <mprivozn@redhat.com> Michal
participants (2)
-
Michal Prívozník
-
Peter Krempa