diff mbox series

[v2,1/3] util: enum: Add 'label' arg to virEnum*String

Message ID 023e3a93a9fcab311b5db29956831c2fedd8e591.1555363505.git.crobinso@redhat.com
State New
Headers show
Series Add 'label' arg to VIR_ENUM_IMPL | expand

Commit Message

Cole Robinson April 15, 2019, 9:26 p.m. UTC
This will allow VIR_ENUM_IMPL calls to register a string label which
will be used to raise errors from  virEnum*String functions.

For example, if virDomainVirtType may use label='domain type', sp
virEnumFromString called with bad value='zzz' will raise:

  invalid argument: Unknown 'domain type' value 'zzz'

Signed-off-by: Cole Robinson <crobinso@redhat.com>

---
 src/util/virenum.c | 21 +++++++++++++++++----
 src/util/virenum.h | 12 ++++++++----
 2 files changed, 25 insertions(+), 8 deletions(-)

-- 
2.21.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
diff mbox series

Patch

diff --git a/src/util/virenum.c b/src/util/virenum.c
index 26093bd795..6ae8d9fb2a 100644
--- a/src/util/virenum.c
+++ b/src/util/virenum.c
@@ -18,6 +18,7 @@ 
 #include <config.h>
 
 #include "virenum.h"
+#include "virerror.h"
 #include "virstring.h"
 
 #define VIR_FROM_THIS VIR_FROM_NONE
@@ -60,16 +61,22 @@  virTristateSwitchFromBool(bool val)
 int
 virEnumFromString(const char * const *types,
                   unsigned int ntypes,
-                  const char *type)
+                  const char *type,
+                  const char *label)
 {
     size_t i;
     if (!type)
-        return -1;
+        goto error;
 
     for (i = 0; i < ntypes; i++)
         if (STREQ(types[i], type))
             return i;
 
+ error:
+    if (label) {
+        virReportError(VIR_ERR_INVALID_ARG,
+                       _("Unknown '%s' value '%s'"), label, NULLSTR(type));
+    }
     return -1;
 }
 
@@ -77,10 +84,16 @@  virEnumFromString(const char * const *types,
 const char *
 virEnumToString(const char * const *types,
                 unsigned int ntypes,
-                int type)
+                int type,
+                const char *label)
 {
-    if (type < 0 || type >= ntypes)
+    if (type < 0 || type >= ntypes) {
+        if (label) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Unknown '%s' internal value %d"), label, type);
+        }
         return NULL;
+    }
 
     return types[type];
 }
diff --git a/src/util/virenum.h b/src/util/virenum.h
index 3ae1a70b72..706ae752d8 100644
--- a/src/util/virenum.h
+++ b/src/util/virenum.h
@@ -24,24 +24,28 @@ 
 int
 virEnumFromString(const char * const *types,
                   unsigned int ntypes,
-                  const char *type);
+                  const char *type,
+                  const char *label);
 
 const char *
 virEnumToString(const char * const *types,
                 unsigned int ntypes,
-                int type);
+                int type,
+                const char *label);
 
 # define VIR_ENUM_IMPL(name, lastVal, ...) \
     static const char *const name ## TypeList[] = { __VA_ARGS__ }; \
     const char *name ## TypeToString(int type) { \
         return virEnumToString(name ## TypeList, \
                                ARRAY_CARDINALITY(name ## TypeList), \
-                               type); \
+                               type, \
+                               NULL); \
     } \
     int name ## TypeFromString(const char *type) { \
         return virEnumFromString(name ## TypeList, \
                                  ARRAY_CARDINALITY(name ## TypeList), \
-                                 type); \
+                                 type, \
+                                 NULL); \
     } \
     verify(ARRAY_CARDINALITY(name ## TypeList) == lastVal)