[gxml] GomObject instantiate object using nicks



commit 87a6af6256a1e54533b4cca4ace1e1c38ecbc5b1
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Feb 2 15:52:12 2017 -0600

    GomObject instantiate object using nicks

 gxml/GomObject.vala            |   15 +++++++++++----
 test/GomSerializationTest.vala |   27 ++++++++++++++++++++++++++-
 2 files changed, 37 insertions(+), 5 deletions(-)
---
diff --git a/gxml/GomObject.vala b/gxml/GomObject.vala
index bf8317e..657fc50 100644
--- a/gxml/GomObject.vala
+++ b/gxml/GomObject.vala
@@ -77,12 +77,20 @@ public interface GXml.GomObject : GLib.Object,
    * Returns a {@link GomObject} or a {@link GomCollection} property's
    * {@link ParamSpec} based on given name. This method is
    * case insensitive.
+   *
+   * This method will check if nick's name is equal than given name
+   * in order to avoid use canonical names like "your-name" if your
+   * property is your_name; so you can se nick to "YourName" to find
+   * and instantiate it.
    */
   public virtual ParamSpec? find_object_property_name (string pname) {
     foreach (ParamSpec spec in this.get_class ().list_properties ()) {
-      message ("Prop: '"+spec.name+"' : "+spec.value_type.is_a (typeof (GomObject)).to_string ());
-      message ("Search for: "+pname+" :"+(spec.name.down () == pname.down ()).to_string ());
-      if (spec.name.down () == pname.down ()) {
+      string name = pname.down ();
+      if ("::" in name) name = name.replace ("::","");
+      string nick = spec.get_nick ().down ();
+      if ("::" in nick) nick = nick.replace ("::","");
+      string sname = spec.name.down ();
+      if (sname == name || nick == name) {
         if (spec.value_type.is_a (typeof (GomObject))
             || spec.value_type.is_a (typeof (GomCollection))) {
 #if DEBUG
@@ -329,7 +337,6 @@ public interface GXml.GomObject : GLib.Object,
    public bool create_instance_property (string name) {
       var prop = find_object_property_name (name);
       if (prop == null) return false;
-      message ("Found: "+prop.name);
       Value v = Value (prop.value_type);
       Object obj;
       if (prop.value_type.is_a (typeof (GomCollection))) {
diff --git a/test/GomSerializationTest.vala b/test/GomSerializationTest.vala
index c1e34fa..1bd17c2 100644
--- a/test/GomSerializationTest.vala
+++ b/test/GomSerializationTest.vala
@@ -232,9 +232,11 @@ class GomSerializationTest : GXmlTest  {
   public class BookStand : GomElement {
     [Description (nick="::Classification")]
     public string classification { get; set; default = "Science"; }
-    [Description (nick="::DimensionX")]
     public Dimension dimension_x { get; set; }
+    [Description (nick="::DimensionY")]
     public DimensionY dimension_y { get; set; }
+    [Description (nick="DimensionZ")]
+    public DimensionZ dimension_z { get; set; }
     public Registers registers { get; set; }
     public Books books { get; set; }
     construct {
@@ -265,6 +267,11 @@ class GomSerializationTest : GXmlTest  {
         dtype = "y";
       }
     }
+    public class DimensionZ : Dimension {
+      construct {
+        dtype = "z";
+      }
+    }
   }
 
   public class Registers : GomArrayList {
@@ -469,6 +476,24 @@ class GomSerializationTest : GXmlTest  {
       GLib.message ("DOC:"+s);
 //#endif
       assert ("<BookStand Classification=\"Science\"><BookRegister Year=\"2016\"/><BookRegister 
Year=\"2010\"/><Test/><BookRegister Year=\"2000\"/><Dimension Length=\"1\" Type=\"x\"/></BookStand>" in s);
+      assert (bs.create_instance_property("DimensionY"));
+      assert (bs.dimension_y != null);
+      assert (bs.dimension_y.length == 1.0);
+      s = bs.to_string ();
+      assert (s != null);
+//#if DEBUG
+      GLib.message ("DOC:"+s);
+//#endif
+      assert ("<BookStand Classification=\"Science\"><BookRegister Year=\"2016\"/><BookRegister 
Year=\"2010\"/><Test/><BookRegister Year=\"2000\"/><Dimension Length=\"1\" Type=\"x\"/><Dimension 
Length=\"1\" Type=\"y\"/></BookStand>" in s);
+      assert (bs.create_instance_property("::DimensionZ"));
+      assert (bs.dimension_z != null);
+      assert (bs.dimension_z.length == 1.0);
+      s = bs.to_string ();
+      assert (s != null);
+//#if DEBUG
+      GLib.message ("DOC:"+s);
+//#endif
+      assert ("<BookStand Classification=\"Science\"><BookRegister Year=\"2016\"/><BookRegister 
Year=\"2010\"/><Test/><BookRegister Year=\"2000\"/><Dimension Length=\"1\" Type=\"x\"/><Dimension 
Length=\"1\" Type=\"y\"/><Dimension Length=\"1\" Type=\"z\"/></BookStand>" in s);
     } catch (GLib.Error e) {
       GLib.message ("Error: "+e.message);
       assert_not_reached ();


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