[libvirt] [PATCH RFC 1/2] util: Add VIR_ENUM_IMPL_LABEL

Cole Robinson posted 2 patches 7 years, 6 months ago
[libvirt] [PATCH RFC 1/2] util: Add VIR_ENUM_IMPL_LABEL
Posted by Cole Robinson 7 years, 6 months ago
This allows passing in a string label describing the enum, which can
be used to autogenerate error messages

Signed-off-by: Cole Robinson <crobinso@redhat.com>
---
 src/util/virutil.c | 20 ++++++++++++++++----
 src/util/virutil.h | 15 ++++++++++-----
 2 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/src/util/virutil.c b/src/util/virutil.c
index a908422feb..6d23a26a74 100644
--- a/src/util/virutil.c
+++ b/src/util/virutil.c
@@ -444,16 +444,22 @@ virParseVersionString(const char *str, unsigned long *version,
 
 int virEnumFromString(const char *const*types,
                       unsigned int ntypes,
-                      const char *type)
+                      const char *type,
+                      const char * const 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, type);
+    }
     return -1;
 }
 
@@ -540,10 +546,16 @@ virFormatIntPretty(unsigned long long val,
 
 const char *virEnumToString(const char *const*types,
                             unsigned int ntypes,
-                            int type)
+                            int type,
+                            const char * const 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/virutil.h b/src/util/virutil.h
index 1ba9635bd9..345c9e053d 100644
--- a/src/util/virutil.h
+++ b/src/util/virutil.h
@@ -76,26 +76,31 @@ char *virIndexToDiskName(int idx, const char *prefix);
 
 int virEnumFromString(const char *const*types,
                       unsigned int ntypes,
-                      const char *type);
+                      const char *type,
+                      const char *errmsg);
 
 const char *virEnumToString(const char *const*types,
                             unsigned int ntypes,
-                            int type);
+                            int type,
+                            const char *errmsg);
 
-# define VIR_ENUM_IMPL(name, lastVal, ...) \
+# define VIR_ENUM_IMPL_LABEL(name, label, lastVal, ...) \
     static const char *const name ## TypeList[] = { __VA_ARGS__ }; \
     verify(ARRAY_CARDINALITY(name ## TypeList) == lastVal); \
     const char *name ## TypeToString(int type) { \
         return virEnumToString(name ## TypeList, \
                                ARRAY_CARDINALITY(name ## TypeList), \
-                               type); \
+                               type, label); \
     } \
     int name ## TypeFromString(const char *type) { \
         return virEnumFromString(name ## TypeList, \
                                  ARRAY_CARDINALITY(name ## TypeList), \
-                                 type); \
+                                 type, label); \
     }
 
+# define VIR_ENUM_IMPL(name, lastVal, ...) \
+    VIR_ENUM_IMPL_LABEL(name, NULL, lastVal, __VA_ARGS__)
+
 # define VIR_ENUM_DECL(name) \
     const char *name ## TypeToString(int type); \
     int name ## TypeFromString(const char*type);
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH RFC 1/2] util: Add VIR_ENUM_IMPL_LABEL
Posted by Michal Privoznik 7 years, 6 months ago
On 07/26/2018 07:49 PM, Cole Robinson wrote:
> This allows passing in a string label describing the enum, which can
> be used to autogenerate error messages
> 
> Signed-off-by: Cole Robinson <crobinso@redhat.com>
> ---
>  src/util/virutil.c | 20 ++++++++++++++++----
>  src/util/virutil.h | 15 ++++++++++-----
>  2 files changed, 26 insertions(+), 9 deletions(-)
> 
> diff --git a/src/util/virutil.c b/src/util/virutil.c
> index a908422feb..6d23a26a74 100644
> --- a/src/util/virutil.c
> +++ b/src/util/virutil.c
> @@ -444,16 +444,22 @@ virParseVersionString(const char *str, unsigned long *version,
>  
>  int virEnumFromString(const char *const*types,
>                        unsigned int ntypes,
> -                      const char *type)
> +                      const char *type,
> +                      const char * const 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) {

If we rewrite all of our enums into _LABEL then @label is always going
to be non-NULL. But we need this check for now. We are not there yet.

> +        virReportError(VIR_ERR_INVALID_ARG,
> +                       _("Unknown '%s' value '%s'"), label, type);

@type can be NULL, therefore NULLSTR(type).

> +    }
>      return -1;
>  }
>  

Michal

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