[gxml] Fixed SerializableProperty deserialize



commit d258c353545d27836b88333ba6912e7b84e22277
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue Oct 13 16:18:56 2015 -0500

    Fixed SerializableProperty deserialize

 gxml/SerializableBool.vala                  |    5 ++-
 gxml/SerializableDouble.vala                |    5 ++-
 gxml/SerializableEnum.vala                  |    5 ++-
 gxml/SerializableProperty.vala              |   25 ++++++++++++------
 gxml/SerializableValueList.vala             |   36 ++++++++++++++++++++++++---
 test/SerializablePropertyValueListTest.vala |   18 +++++++++++++
 6 files changed, 75 insertions(+), 19 deletions(-)
---
diff --git a/gxml/SerializableBool.vala b/gxml/SerializableBool.vala
index 1141c3a..b4bde69 100644
--- a/gxml/SerializableBool.vala
+++ b/gxml/SerializableBool.vala
@@ -54,12 +54,13 @@ public class GXml.SerializableBool : SerializableObjectModel, SerializableProper
   public override GXml.Node? deserialize (GXml.Node node)
                                       throws GLib.Error
   {
-    return default_serializable_property_deserialize (node);
+    default_serializable_property_deserialize_property (node);
+    return node;
   }
   public override bool deserialize_property (GXml.Node property_node)
                                               throws GLib.Error
   {
-    default_serializable_property_deserialize (property_node);
+    default_serializable_property_deserialize_property (property_node);
     return true;
   }
   public override string to_string () {
diff --git a/gxml/SerializableDouble.vala b/gxml/SerializableDouble.vala
index 0456587..defee59 100644
--- a/gxml/SerializableDouble.vala
+++ b/gxml/SerializableDouble.vala
@@ -61,12 +61,13 @@ public class GXml.SerializableDouble : SerializableObjectModel, SerializableProp
   public override GXml.Node? deserialize (GXml.Node node)
                                       throws GLib.Error
   {
-    return default_serializable_property_deserialize (node);
+    default_serializable_property_deserialize_property (node);
+    return node;
   }
   public override bool deserialize_property (GXml.Node property_node)
                                               throws GLib.Error
   {
-    default_serializable_property_deserialize (property_node);
+    default_serializable_property_deserialize_property (property_node);
     return true;
   }
   public override string to_string () {
diff --git a/gxml/SerializableEnum.vala b/gxml/SerializableEnum.vala
index 5956934..89de9dd 100644
--- a/gxml/SerializableEnum.vala
+++ b/gxml/SerializableEnum.vala
@@ -92,12 +92,13 @@ public class GXml.SerializableEnum : SerializableObjectModel, SerializableProper
   public override GXml.Node? deserialize (GXml.Node node)
                                       throws GLib.Error
   {
-    return default_serializable_property_deserialize (node);
+    default_serializable_property_deserialize_property (node);
+    return node;
   }
   public override bool deserialize_property (GXml.Node property_node)
                                               throws GLib.Error
   {
-    default_serializable_property_deserialize (property_node);
+    default_serializable_property_deserialize_property (property_node);
     return true;
   }
   public override string to_string () { return _val; }
diff --git a/gxml/SerializableProperty.vala b/gxml/SerializableProperty.vala
index ffe2d3a..84e6695 100644
--- a/gxml/SerializableProperty.vala
+++ b/gxml/SerializableProperty.vala
@@ -98,18 +98,25 @@ public interface GXml.SerializableProperty : Object, Serializable
    * Tryies to deserialize from a { link GXml.Node} searching a { link GXml.Attr}
    * with the name returned by { link GXml.SerializableProperty.get_serializable_property_name}
    */
-  public virtual GXml.Node? default_serializable_property_deserialize (GXml.Node node)
-                                      throws GLib.Error
-    requires (get_serializable_property_name () != null)
+  public virtual bool default_serializable_property_deserialize_property (GXml.Node property_node)
+    throws GLib.Error
   {
     GXml.Attribute attr = null;
-    if (node is GXml.Attribute)
-      attr = (GXml.Attribute) node;
-    if (node is GXml.Element)
-      attr = (GXml.Attribute) ((GXml.Element) node).attrs.get (get_serializable_property_name ());
-    if (attr == null) return node;
+    if (property_node is GXml.Attribute)
+      attr = (GXml.Attribute) property_node;
+    if (property_node is GXml.Element)
+      attr = (GXml.Attribute) ((GXml.Element) property_node).attrs.get (get_serializable_property_name ());
+    if (attr == null) return false;
+    if (get_serializable_property_name () == null) {
+      GLib.warning ("Property name is not set for type: "+this.get_type ().name ());
+      return false;
+    }
+    if (attr.name == null) {
+      GLib.warning ("XML Attribute name is not set, when deserializing to: "+this.get_type ().name ());
+      return false;
+    }
     if (attr.name.down () == get_serializable_property_name ().down ())
       set_serializable_property_value (attr.value);
-    return node;
+    return true;
   }
 }
\ No newline at end of file
diff --git a/gxml/SerializableValueList.vala b/gxml/SerializableValueList.vala
index 86082f2..fd332f4 100644
--- a/gxml/SerializableValueList.vala
+++ b/gxml/SerializableValueList.vala
@@ -34,24 +34,51 @@ public class GXml.SerializableValueList : SerializableObjectModel, SerializableP
   private string _name = null;
   private ArrayList<string> _values = null;
   public SerializableValueList (string name) { _name = name; }
-  public void add_values (string[] vals)
+  /**
+   * Add a list of string values to select from.
+   */
+  public virtual void add_values (string[] vals)
   {
     if (_values == null) _values = new ArrayList<string> ();
     for (int i = 0; i < vals.length; i++) {
       _values.add (vals[i]);
     }
   }
+  /**
+   * Get the string value at a given index. This operation does not change
+   * the actual value.
+   */
   public string? get_value_at (int index)
   {
     if (_values == null) return null;
     if (index < 0 || index >= _values.size) return null;
     return _values.get (index);
   }
+  /**
+   * Sets value to the one at a given position.
+   */
   public void select_value_at (int index)
   {
     _val = get_value_at (index);
   }
-  public string[] get_values () { return _values.to_array (); }
+  /**
+   * Get an array of string values in list.
+   */
+  public virtual string[] get_values () {
+    if (_values == null) return {""};
+    return _values.to_array ();
+  }
+  /**
+   * Checks if the actual value is in the values list.
+   */
+  public bool is_value ()
+  {
+    if (_values == null) return false;
+    foreach (string s in _values) {
+      if (s == _val) return true;
+    }
+    return false;
+  }
   public string get_serializable_property_value () { return _val; }
   public void set_serializable_property_value (string? val) { _val = val; }
   public string get_serializable_property_name () { return _name; }
@@ -69,12 +96,13 @@ public class GXml.SerializableValueList : SerializableObjectModel, SerializableP
   public override GXml.Node? deserialize (GXml.Node node)
                                       throws GLib.Error
   {
-    return default_serializable_property_deserialize (node);
+    default_serializable_property_deserialize_property (node);
+    return node;
   }
   public override bool deserialize_property (GXml.Node property_node)
                                               throws GLib.Error
   {
-    default_serializable_property_deserialize (property_node);
+    default_serializable_property_deserialize_property (property_node);
     return true;
   }
   public override string to_string () { return _val; }
diff --git a/test/SerializablePropertyValueListTest.vala b/test/SerializablePropertyValueListTest.vala
index 21a6d3a..7b74acb 100644
--- a/test/SerializablePropertyValueListTest.vala
+++ b/test/SerializablePropertyValueListTest.vala
@@ -105,5 +105,23 @@ class SerializablePropertyValueListTest : GXmlTest {
         assert_not_reached ();
       }
     });
+    Test.add_func ("/gxml/serializable/ValueList/deserialize",
+    () => {
+      try {
+        var doc1 = new xDocument.from_string ("""<?xml version="1.0"?>
+                       <options values="Temp1"/>""");
+        var vl = new ValueList ();
+        vl.deserialize (doc1);
+        assert (vl.values.get_serializable_property_value () == "Temp1");
+        assert (vl.values.get_values () != null);
+        assert (vl.values.get_values ().length == 1);
+        assert (vl.values.is_value () == false);
+        vl.values.add_values ({"Temp1"});
+        assert (vl.values.is_value () == true);
+      } catch (GLib.Error e) {
+        Test.message (@"ERROR: $(e.message)");
+        assert_not_reached ();
+      }
+    });
   }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]