Blame SOURCES/0006-storage-domain-Factor-out-property-value-setting-fro.patch

7cfb7a
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
7cfb7a
From: "Eduardo Lima (Etrunko)" <etrunko@redhat.com>
7cfb7a
Date: Wed, 10 May 2017 15:45:36 -0300
7cfb7a
Subject: [PATCH] storage-domain: Factor out property value setting from
7cfb7a
 ovirt_resource_parse_xml()
7cfb7a
7cfb7a
Instead of using chained if/else blocks, use a switch for the type. With
7cfb7a
this small refactor we  prepare to move this function to ovirt-utils so
7cfb7a
that it can be reused by other objects.
7cfb7a
7cfb7a
Signed-off-by: Eduardo Lima (Etrunko) <etrunko@redhat.com>
7cfb7a
---
7cfb7a
 govirt/ovirt-storage-domain.c | 74 ++++++++++++++++++++++-------------
7cfb7a
 1 file changed, 47 insertions(+), 27 deletions(-)
7cfb7a
7cfb7a
diff --git a/govirt/ovirt-storage-domain.c b/govirt/ovirt-storage-domain.c
7cfb7a
index e7308bb..4087d75 100644
7cfb7a
--- a/govirt/ovirt-storage-domain.c
7cfb7a
+++ b/govirt/ovirt-storage-domain.c
7cfb7a
@@ -277,6 +277,51 @@ OvirtStorageDomain *ovirt_storage_domain_new(void)
7cfb7a
     return OVIRT_STORAGE_DOMAIN(domain);
7cfb7a
 }
7cfb7a
 
7cfb7a
+static gboolean
7cfb7a
+_set_property_value_from_type(GValue *value,
7cfb7a
+                              GType type,
7cfb7a
+                              const char *value_str,
7cfb7a
+                              RestXmlNode *node)
7cfb7a
+{
7cfb7a
+    gboolean ret = TRUE;
7cfb7a
+
7cfb7a
+    if (g_type_is_a(type, OVIRT_TYPE_RESOURCE)) {
7cfb7a
+        GObject *resource_value = g_initable_new(type, NULL, NULL, "xml-node", node, NULL);
7cfb7a
+        g_value_set_object(value, resource_value);
7cfb7a
+        goto end;
7cfb7a
+    }
7cfb7a
+
7cfb7a
+    /* All other types require valid value_str */
7cfb7a
+    if (value_str == NULL)
7cfb7a
+        return FALSE;
7cfb7a
+
7cfb7a
+    if (G_TYPE_IS_ENUM(type)) {
7cfb7a
+        int enum_value = ovirt_utils_genum_get_value(type, value_str, 0);
7cfb7a
+        g_value_set_enum(value, enum_value);
7cfb7a
+        goto end;
7cfb7a
+    }
7cfb7a
+
7cfb7a
+    switch(type) {
7cfb7a
+    case G_TYPE_BOOLEAN: {
7cfb7a
+        gboolean bool_value = ovirt_utils_boolean_from_string(value_str);
7cfb7a
+        g_value_set_boolean(value, bool_value);
7cfb7a
+        break;
7cfb7a
+    }
7cfb7a
+    case G_TYPE_UINT64: {
7cfb7a
+        guint64 int64_value = g_ascii_strtoull(value_str, NULL, 0);
7cfb7a
+        g_value_set_uint64(value, int64_value);
7cfb7a
+        break;
7cfb7a
+    }
7cfb7a
+    default: {
7cfb7a
+        g_warning("Unexpected type '%s' with value '%s'", g_type_name(type), value_str);
7cfb7a
+        ret = FALSE;
7cfb7a
+    }
7cfb7a
+    }
7cfb7a
+
7cfb7a
+end:
7cfb7a
+    return ret;
7cfb7a
+}
7cfb7a
+
7cfb7a
 typedef struct {
7cfb7a
     const char *xml_node;
7cfb7a
     GType type;
7cfb7a
@@ -296,34 +341,9 @@ ovirt_resource_parse_xml(OvirtResource *resource,
7cfb7a
         GValue value = { 0, };
7cfb7a
 
7cfb7a
         value_str = ovirt_rest_xml_node_get_content_from_path(node, elements->xml_node);
7cfb7a
-        if (value_str == NULL) {
7cfb7a
-            continue;
7cfb7a
-        }
7cfb7a
         g_value_init(&value, elements->type);
7cfb7a
-
7cfb7a
-        if (G_TYPE_IS_ENUM(elements->type)) {
7cfb7a
-            int enum_value;
7cfb7a
-            enum_value = ovirt_utils_genum_get_value(elements->type,
7cfb7a
-                                                     value_str, 0);
7cfb7a
-            g_value_set_enum(&value, enum_value);
7cfb7a
-        } else if (elements->type == G_TYPE_BOOLEAN) {
7cfb7a
-            gboolean bool_value;
7cfb7a
-
7cfb7a
-            bool_value = ovirt_utils_boolean_from_string(value_str);
7cfb7a
-            g_value_set_boolean(&value, bool_value);
7cfb7a
-        } else if (elements->type == G_TYPE_UINT64) {
7cfb7a
-            guint64 int64_value;
7cfb7a
-
7cfb7a
-            int64_value = g_ascii_strtoull(value_str, NULL, 0);
7cfb7a
-            g_value_set_uint64(&value, int64_value);
7cfb7a
-        } else if (g_type_is_a(elements->type, OVIRT_TYPE_RESOURCE)) {
7cfb7a
-            GObject *resource_value;
7cfb7a
-
7cfb7a
-            resource_value = g_initable_new(elements->type, NULL, NULL,
7cfb7a
-                                            "xml-node", node, NULL);
7cfb7a
-            g_value_set_object(&value, resource_value);
7cfb7a
-        }
7cfb7a
-        g_object_set_property(G_OBJECT(resource), elements->prop_name, &value);
7cfb7a
+        if (_set_property_value_from_type(&value, elements->type, value_str, node))
7cfb7a
+            g_object_set_property(G_OBJECT(resource), elements->prop_name, &value);
7cfb7a
         g_value_unset(&value);
7cfb7a
     }
7cfb7a