]> xenbits.xensource.com Git - libvirt.git/commitdiff
virTypedParamsFilter: Introduce option to filter also by type
authorPeter Krempa <pkrempa@redhat.com>
Fri, 27 Sep 2024 10:56:34 +0000 (12:56 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 1 Oct 2024 10:57:02 +0000 (12:57 +0200)
The only caller of this function is doing some additional filtering so
it's useful if the filtering function was able to do so internally.

Introduce a 'type' parameter which will optionally filter the results by
type and extend the testsuite to cover this scenario.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
src/util/virtypedparam.c
src/util/virtypedparam.h
tests/virtypedparamtest.c

index 86fbaf5e9d4f2eb667d4a99cb6cec7753eb6d394..a080d7ba0f2a2a38323042aa687e1105b012634d 100644 (file)
@@ -391,10 +391,12 @@ virTypedParamsCopy(virTypedParameterPtr *dst,
  * @params: array of typed parameters
  * @nparams: number of parameters in the @params array
  * @name: name of the parameter to find
+ * @type: type of fields to filter (ignored if 0 is passed)
  * @ret: pointer to the returned array
  *
  * Filters @params retaining only the parameters named @name in the
- * resulting array @ret.
+ * resulting array @ret. If @type is non-zero it also filters out parameters
+ * whose type doesn't match @type.
  *
  * Important Caller should free the @ret array but not the items since they are
  * pointing to the @params elements. I.e. callers must not use
@@ -406,6 +408,7 @@ size_t
 virTypedParamsFilter(virTypedParameterPtr params,
                      int nparams,
                      const char *name,
+                     int type,
                      virTypedParameterPtr **ret)
 {
     size_t i;
@@ -414,10 +417,14 @@ virTypedParamsFilter(virTypedParameterPtr params,
     *ret = g_new0(virTypedParameterPtr, nparams);
 
     for (i = 0; i < nparams; i++) {
-        if (STREQ(params[i].field, name)) {
-            (*ret)[n] = &params[i];
-            n++;
-        }
+        if (STRNEQ(params[i].field, name))
+            continue;
+
+        if (type != 0 &&
+            params[i].type != type)
+            continue;
+
+        (*ret)[n++] = &params[i];
     }
 
     return n;
@@ -453,7 +460,7 @@ virTypedParamsGetStringList(virTypedParameterPtr params,
 
     *values = NULL;
 
-    nfiltered = virTypedParamsFilter(params, nparams, name, &filtered);
+    nfiltered = virTypedParamsFilter(params, nparams, name, 0, &filtered);
 
     if (nfiltered == 0)
         return 0;
index afd923aacbcc7c6b0452fad034471b90a96ecc10..774744244a046ae61b2f070a7c8c9236cc7f155c 100644 (file)
@@ -85,6 +85,7 @@ size_t
 virTypedParamsFilter(virTypedParameterPtr params,
                      int nparams,
                      const char *name,
+                     int type,
                      virTypedParameterPtr **ret)
     G_GNUC_WARN_UNUSED_RESULT;
 
index 5ced453be5f2e7533bba64e123b7ad9db1fb7a31..1a8b49383f3162eb2aaa8ae15a07531f3cb3a27a 100644 (file)
@@ -91,13 +91,14 @@ testTypedParamsFilter(const void *opaque G_GNUC_UNUSED)
         { .field = "bar", .type = VIR_TYPED_PARAM_UINT },
         { .field = "foo", .type = VIR_TYPED_PARAM_INT },
         { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
-        { .field = "foo", .type = VIR_TYPED_PARAM_INT }
+        { .field = "foo", .type = VIR_TYPED_PARAM_INT },
+        { .field = "foobar", .type = VIR_TYPED_PARAM_INT },
     };
     virTypedParameterPtr *filtered = NULL;
 
 
     nfiltered = virTypedParamsFilter(params, G_N_ELEMENTS(params),
-                                     "foo", &filtered);
+                                     "foo", 0, &filtered);
     if (nfiltered != 3)
         goto cleanup;
 
@@ -108,7 +109,7 @@ testTypedParamsFilter(const void *opaque G_GNUC_UNUSED)
     VIR_FREE(filtered);
 
     nfiltered = virTypedParamsFilter(params, G_N_ELEMENTS(params),
-                                     "bar", &filtered);
+                                     "bar", VIR_TYPED_PARAM_UINT, &filtered);
 
     if (nfiltered != 2)
         goto cleanup;
@@ -117,6 +118,13 @@ testTypedParamsFilter(const void *opaque G_GNUC_UNUSED)
         if (filtered[i] != &params[i * 2])
             goto cleanup;
     }
+    VIR_FREE(filtered);
+
+    nfiltered = virTypedParamsFilter(params, G_N_ELEMENTS(params),
+                                     "foobar", VIR_TYPED_PARAM_STRING, &filtered);
+
+    if (nfiltered != 1)
+        goto cleanup;
 
     rv = 0;
  cleanup: