[gxml/serialization: 5/10] Fixes to Serializable.deserialize() * Moved string_to_gvalue to Serializable interface * Serializabl



commit 6ff61bc4284342842339c2411394053c2ed6c28a
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue Jul 23 00:07:22 2013 -0500

    Fixes to Serializable.deserialize()
    * Moved string_to_gvalue to Serializable interface
    * Serializable.serialize_property() deprecates use of ParamSpec

 gxml/Serializable.vala     |  148 ++++++++++++++++++++++++++++++++++++++------
 gxml/Serialization.vala    |  108 +-------------------------------
 test/SerializableTest.vala |   17 +++++
 3 files changed, 148 insertions(+), 125 deletions(-)
---
diff --git a/gxml/Serializable.vala b/gxml/Serializable.vala
index 6a86ca7..777f15c 100644
--- a/gxml/Serializable.vala
+++ b/gxml/Serializable.vala
@@ -134,7 +134,8 @@ namespace GXml {
                        return serialized_xml_node;
                }
 
-               public virtual DomNode? deserialize (DomNode node) throws DomError
+               public virtual DomNode? deserialize (DomNode node)
+                                                    throws DomError
                {
                        Document doc;
                        if (node is Document) {
@@ -147,15 +148,16 @@ namespace GXml {
                                serialized_xml_node = (Element) node;
                        else
                                serialized_xml_node = (Element) doc.document_element;
-                       if (serialized_xml_node.node_name == this.get_type().name() 
-                           || serialized_xml_node.node_name == (this.get_type().name()).down ())
+                       return_val_if_fail (serialized_xml_node.node_name.down () == 
get_type().name().down(), null);
+                       foreach (Attr attr in serialized_xml_node.attributes.get_values ())
                        {
-                               foreach (Attr att in serialized_xml_node.attributes.get_values ())
+                               deserialize_property (attr);
+                       }
+                       if (serialized_xml_node.has_child_nodes ())
+                       {
+                               foreach (DomNode n in serialized_xml_node.child_nodes)
                                {
-                                       var spec = find_property_spec (att.name);
-                                       if (spec != null) {
-                                               deserialize_property (spec, att);
-                                       }
+                                       deserialize_property (n);
                                }
                                if (serialized_xml_node.content != null)
                                        serialized_xml_node_value = serialized_xml_node.content;
@@ -193,13 +195,14 @@ namespace GXml {
                 * letting them get name from spec
                 * @todo: consider returning { link GLib.Value} as out param
                 */
-               public virtual bool deserialize_property (GLib.ParamSpec spec,
-                                                         GXml.DomNode property_node)
+               public virtual bool deserialize_property (GXml.DomNode property_node)
                                                          throws Error
                {
                        bool ret = false;
                        var prop = find_property_spec (property_node.node_name);
                        if (prop == null) {
+                               GLib.message ("Found Unknown property: " + property_node.node_name);
+                               // FIXME: Event emit
                                unknown_serializable_property.set (property_node.node_name, property_node);
                                return false;
                        }
@@ -212,18 +215,21 @@ namespace GXml {
                                return true;
                        }
                        Value val = Value (prop.value_type);
-                       if (Value.type_transformable (typeof (DomNode), prop.value_type)) {
+                       if (Value.type_transformable (typeof (DomNode), prop.value_type))
+                       {
                                Value tmp = Value (typeof (DomNode));
                                tmp.set_object (property_node);
                                ret = tmp.transform (ref val);
                        }
                        else {
-                               if (property_node is GXml.Attr) {
-                                       if (Value.type_transformable (typeof (string), prop.value_type)) {
-                                               Value ptmp = Value (typeof (string));
-                                               ptmp.set_string (property_node.node_value);
+                               if (property_node is GXml.Attr && !ret)
+                               {
+                                       Value ptmp = Value (typeof (string));
+                                       ptmp.set_string (property_node.node_value);
+                                       if (Value.type_transformable (typeof (string), prop.value_type))
                                                ret = ptmp.transform (ref val);
-                                       }
+                                       else
+                                               ret = string_to_gvalue (property_node.node_value, ref val);
                                }
                        }
                        if (ret) {
@@ -315,10 +321,11 @@ namespace GXml {
                 */
                public virtual GLib.ParamSpec? find_property_spec (string property_name) {
                        init_properties ();
-                       if (!ignored_serializable_properties.contains (property_name)) {
-                               return get_class ().find_property (property_name);
+                       string pn = property_name.down ();
+                       if (ignored_serializable_properties.contains (pn)) {
+                               return null;
                        }
-                       return null;
+                       return get_class ().find_property (pn);
                }
 
                /**
@@ -459,5 +466,108 @@ namespace GXml {
                                ((GLib.Object)this).set_property (spec.name, val);
                        }
                }
+                               /* TODO:
+                * - can't seem to pass delegates on struct methods to another function :(
+                * - no easy string_to_gvalue method in GValue :(
+                */
+
+               /**
+                * Transforms a string into another type hosted by { link GLib.Value}.
+                *
+                * A utility function that handles converting a string
+                * representation of a value into the type specified by the
+                * supplied #GValue dest.  A #GXmlSerializationError will be
+                * set if the string cannot be parsed into the desired type.
+                *
+                * @param str the string to transform into the given #GValue object
+                * @param dest the #GValue out parameter that will contain the parsed value from the string
+                * @return `true` if parsing succeeded, otherwise `false`
+                */
+               /*
+                * @todo: what do functions written in Vala return in C when
+                * they throw an exception?  NULL/0/FALSE?
+                */
+               public static bool string_to_gvalue (string str, ref GLib.Value dest)
+                               throws SerializationError
+               {
+                       Type t = dest.type ();
+                       GLib.Value dest2 = Value (t);
+                       bool ret = false;
+
+                       if (t == typeof (int64)) {
+                               int64 val;
+                               if (ret = int64.try_parse (str, out val)) {
+                                       dest2.set_int64 (val);
+                               }
+                       } else if (t == typeof (int)) {
+                               int64 val;
+                               if (ret = int64.try_parse (str, out val)) {
+                                       dest2.set_int ((int)val);
+                               }
+                       } else if (t == typeof (long)) {
+                               int64 val;
+                               if (ret = int64.try_parse (str, out val)) {
+                                       dest2.set_long ((long)val);
+                               }
+                       } else if (t == typeof (uint)) {
+                               uint64 val;
+                               if (ret = uint64.try_parse (str, out val)) {
+                                       dest2.set_uint ((uint)val);
+                               }
+                       } else if (t == typeof (ulong)) {
+                               uint64 val;
+                               if (ret = uint64.try_parse (str, out val)) {
+                                       dest2.set_ulong ((ulong)val);
+                               }
+                       } else if ((int)t == 20) { // gboolean
+                               bool val = (str == "TRUE");
+                               dest2.set_boolean (val); // TODO: huh, investigate why the type is gboolean 
and not bool coming out but is going in
+                               ret = true;
+                       } else if (t == typeof (bool)) {
+                               bool val;
+                               if (ret = bool.try_parse (str, out val)) {
+                                       dest2.set_boolean (val);
+                               }
+                       } else if (t == typeof (float)) {
+                               double val;
+                               if (ret = double.try_parse (str, out val)) {
+                                       dest2.set_float ((float)val);
+                               }
+                       } else if (t == typeof (double)) {
+                               double val;
+                               if (ret = double.try_parse (str, out val)) {
+                                       dest2.set_double (val);
+                               }
+                       } else if (t == typeof (string)) {
+                               dest2.set_string (str);
+                               ret = true;
+                       } else if (t == typeof (char)) {
+                               int64 val;
+                               if (ret = int64.try_parse (str, out val)) {
+                                       dest2.set_char ((char)val);
+                               }
+                       } else if (t == typeof (uchar)) {
+                               int64 val;
+                               if (ret = int64.try_parse (str, out val)) {
+                                       dest2.set_uchar ((uchar)val);
+                               }
+                       } else if (t == Type.BOXED) {
+                       } else if (t.is_enum ()) {
+                               int64 val;
+                               if (ret = int64.try_parse (str, out val)) {
+                                       dest2.set_enum ((int)val);
+                               }
+                       } else if (t.is_flags ()) {
+                       } else if (t.is_object ()) {
+                       } else {
+                       }
+
+                       if (ret == true) {
+                               dest = dest2;
+                               return true;
+                       } else {
+                               throw new SerializationError.UNSUPPORTED_TYPE ("%s/%s", t.name (), 
t.to_string ());
+                       }
+               }
        }
 }
diff --git a/gxml/Serialization.vala b/gxml/Serialization.vala
index 6a04eb0..70a1555 100644
--- a/gxml/Serialization.vala
+++ b/gxml/Serialization.vala
@@ -323,7 +323,7 @@ namespace GXml {
                        val = Value (type);
                        if (GLib.Value.type_transformable (type, typeof (string))) {
                                try {
-                                       string_to_gvalue (prop_elem.content, ref val);
+                                       Serializable.string_to_gvalue (prop_elem.content, ref val);
                                        transformed = true;
                                } catch (SerializationError e) {
                                        throw new SerializationError.UNSUPPORTED_TYPE ("string_to_gvalue 
should transform it but failed");
@@ -466,7 +466,7 @@ namespace GXml {
                                                bool serialized = false;
 
                                                if (serializable != null) {
-                                                       serialized = serializable.deserialize_property (spec, 
prop_elem); // TODO: consider rearranging these or the ones in Serializer to match
+                                                       serialized = serializable.deserialize_property 
(prop_elem); // TODO: consider rearranging these or the ones in Serializer to match
                                                }
                                                if (!serialized) {
                                                        Serialization.deserialize_property (spec, prop_elem, 
out val);
@@ -492,110 +492,6 @@ namespace GXml {
 
                        return obj;
                }
-
-               /* TODO:
-                * - can't seem to pass delegates on struct methods to another function :(
-                * - no easy string_to_gvalue method in GValue :(
-                */
-
-               /**
-                * Transforms a string into another type hosted by { link GLib.Value}.
-                *
-                * A utility function that handles converting a string
-                * representation of a value into the type specified by the
-                * supplied #GValue dest.  A #GXmlSerializationError will be
-                * set if the string cannot be parsed into the desired type.
-                *
-                * @param str the string to transform into the given #GValue object
-                * @param dest the #GValue out parameter that will contain the parsed value from the string
-                * @return `true` if parsing succeeded, otherwise `false`
-                */
-               /*
-                * @todo: what do functions written in Vala return in C when
-                * they throw an exception?  NULL/0/FALSE?
-                */
-               public static bool string_to_gvalue (string str, ref GLib.Value dest)
-                               throws SerializationError
-               {
-                       Type t = dest.type ();
-                       GLib.Value dest2 = Value (t);
-                       bool ret = false;
-
-                       if (t == typeof (int64)) {
-                               int64 val;
-                               if (ret = int64.try_parse (str, out val)) {
-                                       dest2.set_int64 (val);
-                               }
-                       } else if (t == typeof (int)) {
-                               int64 val;
-                               if (ret = int64.try_parse (str, out val)) {
-                                       dest2.set_int ((int)val);
-                               }
-                       } else if (t == typeof (long)) {
-                               int64 val;
-                               if (ret = int64.try_parse (str, out val)) {
-                                       dest2.set_long ((long)val);
-                               }
-                       } else if (t == typeof (uint)) {
-                               uint64 val;
-                               if (ret = uint64.try_parse (str, out val)) {
-                                       dest2.set_uint ((uint)val);
-                               }
-                       } else if (t == typeof (ulong)) {
-                               uint64 val;
-                               if (ret = uint64.try_parse (str, out val)) {
-                                       dest2.set_ulong ((ulong)val);
-                               }
-                       } else if ((int)t == 20) { // gboolean
-                               bool val = (str == "TRUE");
-                               dest2.set_boolean (val); // TODO: huh, investigate why the type is gboolean 
and not bool coming out but is going in
-                               ret = true;
-                       } else if (t == typeof (bool)) {
-                               bool val;
-                               if (ret = bool.try_parse (str, out val)) {
-                                       dest2.set_boolean (val);
-                               }
-                       } else if (t == typeof (float)) {
-                               double val;
-                               if (ret = double.try_parse (str, out val)) {
-                                       dest2.set_float ((float)val);
-                               }
-                       } else if (t == typeof (double)) {
-                               double val;
-                               if (ret = double.try_parse (str, out val)) {
-                                       dest2.set_double (val);
-                               }
-                       } else if (t == typeof (string)) {
-                               dest2.set_string (str);
-                               ret = true;
-                       } else if (t == typeof (char)) {
-                               int64 val;
-                               if (ret = int64.try_parse (str, out val)) {
-                                       dest2.set_char ((char)val);
-                               }
-                       } else if (t == typeof (uchar)) {
-                               int64 val;
-                               if (ret = int64.try_parse (str, out val)) {
-                                       dest2.set_uchar ((uchar)val);
-                               }
-                       } else if (t == Type.BOXED) {
-                       } else if (t.is_enum ()) {
-                               int64 val;
-                               if (ret = int64.try_parse (str, out val)) {
-                                       dest2.set_enum ((int)val);
-                               }
-                       } else if (t.is_flags ()) {
-                       } else if (t.is_object ()) {
-                       } else {
-                       }
-
-                       if (ret == true) {
-                               dest = dest2;
-                               return true;
-                       } else {
-                               throw new SerializationError.UNSUPPORTED_TYPE ("%s/%s", t.name (), 
t.to_string ());
-                       }
-               }
        }
 }
 
diff --git a/test/SerializableTest.vala b/test/SerializableTest.vala
index 76c625d..c6cce05 100644
--- a/test/SerializableTest.vala
+++ b/test/SerializableTest.vala
@@ -323,6 +323,23 @@ class Package : ObjectModel
        }
 }
 
+const string XML_COMPUTER_FILE = 
+"""<?xml version="1.0"?>
+<computer manufacturer="ThecnicalGroup" model="YH576G" cores="4" ghz="2.8"/>""";
+
+const string XML_MANUAL_FILE =
+"""<?xml version="1.0"?>
+<manual document="Specification" pages="3">This is an Specification file</manual>""";
+
+
+const string XML_PACKAGE_FILE =
+"""<?xml version="1.0"?>
+<PACKAGE source="Mexico/Central" destiny="Japan">
+<manual document="Specification" pages="3">This is an Specification file</manual>
+<Computer manufacturer="LanCorop" model="Lap39120" cores="16" ghz="3.5"/>
+</PACKAGE>""";
+
+
 class SerializableTest : GXmlTest
 {
        public static void add_tests () {


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