[gxml] Added support of SerializableProperty to SerializableObjectModel
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml] Added support of SerializableProperty to SerializableObjectModel
- Date: Wed, 21 Oct 2015 21:44:53 +0000 (UTC)
commit 74f18675f5c0d2c6defb2c5622d24d3ef2936a37
Author: Daniel Espinosa <esodan gmail com>
Date: Wed Oct 21 16:44:35 2015 -0500
Added support of SerializableProperty to SerializableObjectModel
* Now SerializableProperty implementations don't need to set the attribute's name, it is provided
by SerializableObjectModel at serialize/deserialize calls
* Is no longer necessary to create a SerializableProperty instance to serialize/deserialize, this will
save lot of memory in non initialized objects and provides same functionality to avoid them on
serialization
* SerializableProperty simplified, just required serialization/deserialization methods, with ist default
implemention are available, then implementations are simplified to.
* Now is necesary to remove requirement of name on SerializableProperty implementations
gxml/SerializableBool.vala | 22 ----------
gxml/SerializableDouble.vala | 22 ----------
gxml/SerializableEnum.vala | 22 ----------
gxml/SerializableObjectModel.vala | 27 +++++++++---
gxml/SerializableProperty.vala | 59 ++++++++++++++++-----------
gxml/SerializableValueList.vala | 36 +----------------
test/SerializablePropertyDoubleTest.vala | 1 +
test/SerializablePropertyFloatTest.vala | 1 +
test/SerializablePropertyIntTest.vala | 1 +
test/SerializablePropertyValueListTest.vala | 56 +++++++++++++++++++++++++-
10 files changed, 114 insertions(+), 133 deletions(-)
---
diff --git a/gxml/SerializableBool.vala b/gxml/SerializableBool.vala
index b4bde69..364cbc5 100644
--- a/gxml/SerializableBool.vala
+++ b/gxml/SerializableBool.vala
@@ -41,28 +41,6 @@ public class GXml.SerializableBool : SerializableObjectModel, SerializableProper
}
public string get_serializable_property_name () { return _name; }
public void set_serializable_property_name (string name) { _name = name; }
- public override GXml.Node? serialize (GXml.Node node) throws GLib.Error
- {
- return default_serializable_property_serialize (node);
- }
- public override GXml.Node? serialize_property (GXml.Node element,
- GLib.ParamSpec prop)
- throws GLib.Error
- {
- return default_serializable_property_serialize_property (element, prop);
- }
- public override GXml.Node? deserialize (GXml.Node node)
- throws GLib.Error
- {
- 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 (property_node);
- return true;
- }
public override string to_string () {
if (_val != null) return (bool.parse (_val)).to_string ();
return false.to_string ();
diff --git a/gxml/SerializableDouble.vala b/gxml/SerializableDouble.vala
index defee59..38703f4 100644
--- a/gxml/SerializableDouble.vala
+++ b/gxml/SerializableDouble.vala
@@ -48,28 +48,6 @@ public class GXml.SerializableDouble : SerializableObjectModel, SerializableProp
}
public string get_serializable_property_name () { return _name; }
public void set_serializable_property_name (string name) { _name = name; }
- public override GXml.Node? serialize (GXml.Node node) throws GLib.Error
- {
- return default_serializable_property_serialize (node);
- }
- public override GXml.Node? serialize_property (GXml.Node element,
- GLib.ParamSpec prop)
- throws GLib.Error
- {
- return default_serializable_property_serialize_property (element, prop);
- }
- public override GXml.Node? deserialize (GXml.Node node)
- throws GLib.Error
- {
- 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 (property_node);
- return true;
- }
public override string to_string () {
if (_val != null) return (double.parse (_val)).to_string ();
return "";
diff --git a/gxml/SerializableEnum.vala b/gxml/SerializableEnum.vala
index 89de9dd..64c1df7 100644
--- a/gxml/SerializableEnum.vala
+++ b/gxml/SerializableEnum.vala
@@ -79,27 +79,5 @@ public class GXml.SerializableEnum : SerializableObjectModel, SerializableProper
public void set_serializable_property_value (string? val) { _val = val; }
public string get_serializable_property_name () { return _name; }
public void set_serializable_property_name (string name) { _name = name; }
- public override GXml.Node? serialize (GXml.Node node) throws GLib.Error
- {
- return default_serializable_property_serialize (node);
- }
- public override GXml.Node? serialize_property (GXml.Node element,
- GLib.ParamSpec prop)
- throws GLib.Error
- {
- return default_serializable_property_serialize_property (element, prop);
- }
- public override GXml.Node? deserialize (GXml.Node node)
- throws GLib.Error
- {
- 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 (property_node);
- return true;
- }
public override string to_string () { return _val; }
}
\ No newline at end of file
diff --git a/gxml/SerializableObjectModel.vala b/gxml/SerializableObjectModel.vala
index 7c9d26a..3465148 100644
--- a/gxml/SerializableObjectModel.vala
+++ b/gxml/SerializableObjectModel.vala
@@ -213,9 +213,13 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
{
var v = Value (typeof (Object));
get_property (prop.name, ref v);
- var obj = (Serializable) v.get_object ();
- if (obj != null)
- return obj.serialize (element);
+ var obj = v.get_object ();
+ if (obj != null) {
+ if (obj is SerializableProperty)
+ return ((SerializableProperty) obj).serialize_property (element, prop, property_use_nick ());
+ if (obj is Serializable)
+ return ((Serializable) obj).serialize (element);
+ }
}
Value oval;
if (prop.value_type.is_a (Type.ENUM))
@@ -407,13 +411,22 @@ public abstract class GXml.SerializableObjectModel : Object, Serializable
#endif
Value vobj = Value (typeof(Object));
get_property (prop.name, ref vobj);
- if (vobj.get_object () == null) {
+ GLib.Object object = null;
+ object = vobj.get_object ();
+ if (object == null) {
var obj = Object.new (prop.value_type);
- ((Serializable) obj).deserialize (property_node);
+ if (obj is SerializableProperty)
+ ((SerializableProperty) obj).deserialize_property (property_node, prop, property_use_nick ());
+ else
+ ((Serializable) obj).deserialize (property_node);
set_property (prop.name, obj);
}
- else
- ((Serializable) vobj.get_object ()).deserialize (property_node);
+ else {
+ if (object is SerializableProperty)
+ ((SerializableProperty) object).deserialize_property (property_node, prop, property_use_nick ());
+ else
+ ((Serializable) object).deserialize (property_node);
+ }
return true;
}
else {
diff --git a/gxml/SerializableProperty.vala b/gxml/SerializableProperty.vala
index 8a5a074..564de9d 100644
--- a/gxml/SerializableProperty.vala
+++ b/gxml/SerializableProperty.vala
@@ -52,24 +52,24 @@ public interface GXml.SerializableProperty : Object, Serializable
*/
public abstract void set_serializable_property_name (string name);
/**
- * Default serialization method to add a { link GXml.Attr} to a { link GXml.Element}
+ * 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},
+ * if not set, then { link GLib.ParamSpec} name should used. If { param nick} is set to true,
+ * then { link GLib.ParamSpec} nick is used as name.
+ */
+ public virtual bool deserialize_property (GXml.Node property_node, ParamSpec prop, bool nick)
+ throws GLib.Error
+ { return default_serializable_property_deserialize_property (property_node, prop, nick); }
+ /**
+ * Serialization method to add a { link GXml.Attr} to a { link GXml.Element}, using { link ParamSpec}
+ * name or nick, if { param nick} is set to true, as the attribute's name.
*
* If { link GXml.SerializableProperty.get_serializable_property_value} returns { link null}
- * given { link GXml.Node} is not modified.
- *
- * Implementators should call this method instead of { link Serializable.default_serialize}.
+ * given { link GXml.Node} should not be modified.
*/
- public virtual GXml.Node? default_serializable_property_serialize (GXml.Node node) throws GLib.Error
- requires (get_serializable_property_name () != null)
- {
- if (get_serializable_property_value () == null) return node;
- if (node is GXml.Attribute && node.name == get_serializable_property_name ()) {
- ((GXml.Attribute) node).value = get_serializable_property_value ();
- return node;
- }
- ((GXml.Element) node).set_attr (get_serializable_property_name (), get_serializable_property_value ());
- return node;
- }
+ public virtual GXml.Node? serialize_property (GXml.Node property_node, ParamSpec prop, bool nick)
+ throws GLib.Error
+ { return default_serializable_property_serialize_property (property_node, prop,nick); }
/**
* Default serialization method to add a { link GXml.Attr} to a { link GXml.Element}
*
@@ -79,26 +79,30 @@ public interface GXml.SerializableProperty : Object, Serializable
* Implementators should override { link Serializable.serialize_property} to call
* this method on serialization.
*/
- public virtual GXml.Node? default_serializable_property_serialize_property (GXml.Node element,
- GLib.ParamSpec prop)
+ public GXml.Node? default_serializable_property_serialize_property (GXml.Node element,
+ GLib.ParamSpec prop, bool nick)
throws GLib.Error
{
if (get_serializable_property_value () == null) return element;
string name = "";
- if (property_use_nick () &&
+ Test.message ("Use nick: "+nick.to_string ());
+ if (nick &&
prop.get_nick () != null &&
prop.get_nick () != "")
name = prop.get_nick ();
else
name = prop.get_name ();
+ Test.message ("Property to set:"+name+" - with value: "+get_serializable_property_value ());
((GXml.Element) element).set_attr (name, get_serializable_property_value ());
return element;
}
/**
* 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}
+ * with the name returned by { link GXml.SerializableProperty.get_serializable_property_name},
+ * if not set { link GLib.ParamSpec} name is used.
*/
- public virtual bool default_serializable_property_deserialize_property (GXml.Node property_node)
+ public bool default_serializable_property_deserialize_property (GXml.Node property_node,
+ ParamSpec prop, bool nick)
throws GLib.Error
{
GXml.Attribute attr = null;
@@ -112,15 +116,22 @@ public interface GXml.SerializableProperty : Object, Serializable
#endif
return false;
}
- if (get_serializable_property_name () == null) {
- GLib.warning ("Property name is not set for type: "+this.get_type ().name ());
- return false;
+ if (get_serializable_property_name () != null) {
+ if (attr.name.down () == get_serializable_property_name ().down ())
+ set_serializable_property_value (attr.value);
}
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 ())
+ string name = "";
+ if (nick &&
+ prop.get_nick () != null &&
+ prop.get_nick () != "")
+ name = prop.get_nick ();
+ else
+ name = prop.get_name ();
+ if (attr.name.down () == name.down ())
set_serializable_property_value (attr.value);
return true;
}
diff --git a/gxml/SerializableValueList.vala b/gxml/SerializableValueList.vala
index 76dbbfc..98286fd 100644
--- a/gxml/SerializableValueList.vala
+++ b/gxml/SerializableValueList.vala
@@ -62,7 +62,7 @@ public class GXml.SerializableValueList : SerializableObjectModel, SerializableP
* If no values where defined at construction time and no values
* have been added, then this will return an empty list.
*/
- public SerializableValueList (string name) { _name = name; }
+ public SerializableValueList.with_name (string name) { _name = name; }
/**
* Add a list of string values to select from.
*
@@ -112,39 +112,5 @@ public class GXml.SerializableValueList : SerializableObjectModel, SerializableP
public void set_serializable_property_value (string? val) { _val = val; }
public string get_serializable_property_name () { return _name; }
public void set_serializable_property_name (string name) { _name = name; }
- public override GXml.Node? serialize (GXml.Node node) throws GLib.Error
- {
-#if DEBUG
- GLib.message ("Serializing SerializableProperty: "+get_type ().name ());
-#endif
- return default_serializable_property_serialize (node);
- }
- public override GXml.Node? serialize_property (GXml.Node element,
- GLib.ParamSpec prop)
- throws GLib.Error
- {
-#if DEBUG
- GLib.message ("Serializing on property - SerializableProperty: "+get_type ().name ());
-#endif
- return default_serializable_property_serialize_property (element, prop);
- }
- public override GXml.Node? deserialize (GXml.Node node)
- throws GLib.Error
- {
-#if DEBUG
- GLib.message ("Deserializing SerializableProperty: "+get_type ().name ());
-#endif
- default_serializable_property_deserialize_property (node);
- return node;
- }
- public override bool deserialize_property (GXml.Node property_node)
- throws GLib.Error
- {
-#if DEBUG
- GLib.message ("Deserializing on property - SerializableProperty: "+get_type ().name ());
-#endif
- default_serializable_property_deserialize_property (property_node);
- return true;
- }
public override string to_string () { return _val; }
}
\ No newline at end of file
diff --git a/test/SerializablePropertyDoubleTest.vala b/test/SerializablePropertyDoubleTest.vala
index 4ba5638..21a0ec4 100644
--- a/test/SerializablePropertyDoubleTest.vala
+++ b/test/SerializablePropertyDoubleTest.vala
@@ -32,6 +32,7 @@ class SerializablePropertyDoubleTest : GXmlTest {
public string name { get; set; }
public override string node_name () { return "DoubleNode"; }
public override string to_string () { return get_type ().name (); }
+ public override bool property_use_nick () { return true; }
}
public static void add_tests () {
Test.add_func ("/gxml/serializable/Double/basic",
diff --git a/test/SerializablePropertyFloatTest.vala b/test/SerializablePropertyFloatTest.vala
index 4d15b64..31c18f8 100644
--- a/test/SerializablePropertyFloatTest.vala
+++ b/test/SerializablePropertyFloatTest.vala
@@ -32,6 +32,7 @@ class SerializablePropertyFloatTest : GXmlTest {
public string name { get; set; }
public override string node_name () { return "FloatNode"; }
public override string to_string () { return get_type ().name (); }
+ public override bool property_use_nick () { return true; }
}
public static void add_tests () {
Test.add_func ("/gxml/serializable/Float/basic",
diff --git a/test/SerializablePropertyIntTest.vala b/test/SerializablePropertyIntTest.vala
index ef7660b..22817d5 100644
--- a/test/SerializablePropertyIntTest.vala
+++ b/test/SerializablePropertyIntTest.vala
@@ -32,6 +32,7 @@ class SerializablePropertyIntTest : GXmlTest {
public string name { get; set; }
public override string node_name () { return "IntNode"; }
public override string to_string () { return get_type ().name (); }
+ public override bool property_use_nick () { return true; }
}
public static void add_tests () {
Test.add_func ("/gxml/serializable/Int/basic",
diff --git a/test/SerializablePropertyValueListTest.vala b/test/SerializablePropertyValueListTest.vala
index 15b3b29..df7f939 100644
--- a/test/SerializablePropertyValueListTest.vala
+++ b/test/SerializablePropertyValueListTest.vala
@@ -27,7 +27,7 @@ using GXml;
class SerializablePropertyValueListTest : GXmlTest {
public class ValueList : SerializableObjectModel
{
- public SerializableValueList values { get; set; default = new SerializableValueList ("values"); }
+ public SerializableValueList values { get; set; }
public int integer { get; set; default = 0; }
public string name { get; set; }
public override string node_name () { return "ValueList"; }
@@ -68,6 +68,7 @@ class SerializablePropertyValueListTest : GXmlTest {
var i1 = element1.get_attribute_node ("integer");
assert (i1.value == "0");
// Adding values
+ vl.values = new SerializableValueList ();
var v = vl.values.get_value_at (0);
assert (v == null);
vl.values.add_values ({"Temp1","Temp2"});
@@ -124,5 +125,58 @@ class SerializablePropertyValueListTest : GXmlTest {
assert_not_reached ();
}
});
+ Test.add_func ("/gxml/serializable/ValueList/no-name",
+ () => {
+ try {
+ var vl = new ValueList ();
+ vl.values = new SerializableValueList ();
+ var doc1 = new xDocument ();
+ vl.serialize (doc1);
+ Test.message ("XML1:\n"+doc1.to_string ());
+ var element1 = doc1.document_element;
+ var evl1 = element1.get_attribute_node ("values");
+ assert (evl1 == null);
+ var s1 = element1.get_attribute_node ("name");
+ assert (s1 == null);
+ var i1 = element1.get_attribute_node ("integer");
+ assert (i1.value == "0");
+ // Adding values
+ var v = vl.values.get_value_at (0);
+ assert (v == null);
+ vl.values.add_values ({"Temp1","Temp2"});
+ v = vl.values.get_value_at (0);
+ assert (v == "Temp1");
+ v = vl.values.get_value_at (1);
+ assert (v == "Temp2");
+ var doc2 = new xDocument ();
+ vl.serialize (doc2);
+ Test.message ("XML2:\n"+doc2.to_string ());
+ var element2 = doc2.document_element;
+ var evl2 = element2.get_attribute_node ("values");
+ assert (evl2 == null);
+ // Select a value
+ vl.values.select_value_at (1);
+ v = vl.values.get_serializable_property_value ();
+ assert (v == "Temp2");
+ var doc3 = new xDocument ();
+ vl.serialize (doc3);
+ Test.message ("XML3:\n"+doc3.to_string ());
+ var element3 = doc3.document_element;
+ var evl3 = element3.get_attribute_node ("values");
+ assert (evl3 != null);
+ assert (evl3.value == "Temp2");
+ // Set value to null/ignore
+ vl.values.set_serializable_property_value (null);
+ var doc4 = new xDocument ();
+ vl.serialize (doc4);
+ Test.message ("XML4:\n"+doc4.to_string ());
+ var element4 = doc4.document_element;
+ var evl4 = element4.get_attribute_node ("values");
+ assert (evl4 == null);
+ } 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]