[gxml] GomObject: use of nick to set/get properties



commit 4ba2509d410516d3d8ce2e168127ea5552052b33
Author: Daniel Espinosa <esodan gmail com>
Date:   Fri Nov 4 15:10:31 2016 -0600

    GomObject: use of nick to set/get properties
    
    Changed behavior for get_property() and set_property()
    to use nick case insensitive
    
    Serializable properties are taken from a list of
    nick names of them.

 gxml/GomObject.vala            |   38 +++++++++++++++++++++-----------------
 gxml/XParser.vala              |    7 +++----
 test/GomSerializationTest.vala |   14 +++++++++++---
 3 files changed, 35 insertions(+), 24 deletions(-)
---
diff --git a/gxml/GomObject.vala b/gxml/GomObject.vala
index 05d83e8..804655e 100644
--- a/gxml/GomObject.vala
+++ b/gxml/GomObject.vala
@@ -41,32 +41,31 @@ public interface GXml.GomObject : GLib.Object,
   public virtual bool use_nick_name () { return true; }
 
   /**
-   * Returns a hash table with key as property's name and value as property's
-   * nick, of all with a prefix "::" which will be used when getting
-   * and setting properties' values.
+   * Returns a hash table with key as property's nick with out "::" if have it
+   * and value as property's name, for all nicks with a prefix "::". Nick name,
+   * without "::" will be used on serialization to an attribute's name.
    */
-  public virtual HashTable<string,string> get_properties_map () {
-    var l = new HashTable<string,string> (str_hash, str_equal);
+  public virtual List<string> get_properties_list () {
+    var l = new List<string> ();
     foreach (ParamSpec spec in this.get_class ().list_properties ()) {
       if ("::" in spec.get_nick ()) {
         GLib.message ("Name: "+spec.name+ " Nick: "+spec.get_nick ());
-        l.insert (spec.name, spec.get_nick ());
+        l.append (spec.get_nick ().replace ("::",""));
       }
     }
     return l;
   }
   /**
-   * Returns a hash table with key as property's name and value as property's
-   * nick, of all with a prefix "::" which will be used when getting
-   * and setting properties' values.
+   * Returns property's name based on given nick. This function is no
+   * case sensitive.
    */
   public virtual string? find_property_name (string nick) {
     foreach (ParamSpec spec in this.get_class ().list_properties ()) {
       if ("::" in spec.get_nick ()) {
-        string name = nick.replace ("::","");
-        if (name == nick) {
+        string name = spec.get_nick ().replace ("::","");
+        if (name.down () == nick.down ()) {
           GLib.message ("Name: "+spec.name+ " Nick: "+spec.get_nick ());
-          return name;
+          return spec.name;
         }
       }
     }
@@ -90,11 +89,14 @@ public interface GXml.GomObject : GLib.Object,
    */
   public virtual string? get_attribute (string name) {
     GLib.message ("GomObject: attribute: "+name);
-    var prop = get_class ().find_property (name); // FIXME: Find by nick and lower case
+    string pname = find_property_name (name);
+    if (pname == null) return null;
+    GLib.message ("GomObject: found attribute: "+pname);
+    var prop = get_class ().find_property (pname);
     if (prop != null) {
-      GLib.message ("Found attribute");
+      GLib.message ("Found attribute: "+prop.name);
       var v = Value(prop.value_type);
-      get_property (name, ref v);
+      get_property (prop.name, ref v);
       if (prop.value_type == typeof(SerializableProperty)) {
         SerializableProperty so = (Object) v as SerializableProperty;
         if (so == null) return null;
@@ -137,13 +139,15 @@ public interface GXml.GomObject : GLib.Object,
    * this object, see {@link set_child}
    */
   public virtual bool set_attribute (string name, string val) {
+    GLib.message ("GomObject: searching attribute to set: "+name);
     string pname = find_property_name (name);
     if (pname == null) return false;
+    GLib.message ("GomObject: setting attribute: "+name);
     var prop = get_class ().find_property (pname);
     if (prop != null) {
       var v = Value (prop.value_type);
       if (prop.value_type == typeof(SerializableProperty)) {
-        get_property (name, ref v);
+        get_property (prop.name, ref v);
         SerializableProperty so = (Object) v as SerializableProperty;
         if (so == null) return false;
         so.set_serializable_property_value (val);
@@ -202,7 +206,7 @@ public interface GXml.GomObject : GLib.Object,
     if (prop != null) {
       if (prop.value_type == typeof(DomElement)) {
         var vo = Value(prop.value_type);
-        get_property (name, ref vo);
+        get_property (prop.name, ref vo);
         return (DomElement) ((Object) vo);
       }
     }
diff --git a/gxml/XParser.vala b/gxml/XParser.vala
index 90ee083..109a375 100644
--- a/gxml/XParser.vala
+++ b/gxml/XParser.vala
@@ -371,12 +371,11 @@ public class GXml.XParser : Object, GXml.Parser {
         tw.flush ();
     }
     // GomObject serialisation
-    var opm = (node as GomObject).get_properties_map ();
-    foreach (string pk in opm.get_keys ()) {
+    var lp = (node as GomObject).get_properties_list ();
+    foreach (string pk in lp) {
       string v = (node as GomObject).get_attribute (pk);
       if (v == null) continue;
-      string pn = opm.lookup (pk);
-      size += tw.write_attribute (pn.replace ("::",""), v);
+      size += tw.write_attribute (pk, v);
       size += tw.end_attribute ();
       if (size > 1500)
         tw.flush ();
diff --git a/test/GomSerializationTest.vala b/test/GomSerializationTest.vala
index ac806a9..19a52e9 100644
--- a/test/GomSerializationTest.vala
+++ b/test/GomSerializationTest.vala
@@ -95,17 +95,25 @@ class GomSerializationTest : GXmlTest  {
       var t = new Taxes ();
       string s = t.to_string ();
       assert (s != null);
-      assert ("<Taxes monthRate=\"0\" Month=\"january\" TaxFree=\"false\"/>" in s);
+      GLib.message ("DOC:"+s);
+      assert ("<Taxes " in s);
+      assert ("monthRate=\"0\"" in s);
+      assert ("Month=\"january\"" in s);
+      assert ("TaxFree=\"false\"" in s);
       t.month_rate = 16.5;
       assert ("16.5" in "%.2f".printf (t.month_rate));
-      assert ("16.5" in t.get_attribute ("month-rate"));
+      assert ("16.5" in t.get_attribute ("monthrate"));
       t.month = Taxes.Month.FEBRUARY;
       assert (t.month == Taxes.Month.FEBRUARY);
       assert (t.get_attribute ("month") == "february");
       t.tax_free = true;
       assert (t.tax_free == true);
-      assert (t.get_attribute ("tax-free") == "true");
+      assert (t.get_attribute ("taxfree") == "true");
       s = t.to_string ();
+      assert ("<Taxes " in s);
+      assert ("monthRate=\"16.5\"" in s);
+      assert ("Month=\"february\"" in s);
+      assert ("TaxFree=\"true\"" in s);
       GLib.message ("DOC:"+s);
     });
     Test.add_func ("/gxml/gom-serialization/read/properties", () => {


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