[gxml] Enum: support custome value output



commit 876341df4e4b350481a002bfca27fa0bc7d8e1cc
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue Jul 9 12:27:27 2019 -0500

    Enum: support custome value output
    
    Enum class now can be configured to change its value string
    representation, to use the default GLib, use upper cases,
    use nick and use nick upper cases.

 gxml/Enumeration.vala       |  2 +-
 gxml/Property.vala          | 64 +++++++++++++++++++++++++++++++++---
 test/SerializationTest.vala | 79 +++++++++++++++++++++++++++++----------------
 3 files changed, 112 insertions(+), 33 deletions(-)
---
diff --git a/gxml/Enumeration.vala b/gxml/Enumeration.vala
index db7ecce..4599420 100644
--- a/gxml/Enumeration.vala
+++ b/gxml/Enumeration.vala
@@ -126,7 +126,7 @@ namespace GXml {
                                        enumv = ev;
                        }
                        if (enumv == null)
-                               throw new EnumerationError.INVALID_TEXT (_("text cannot be parsed to 
enumeration type:")+enumeration.name ());
+                               throw new EnumerationError.INVALID_TEXT (_("text '%s' cannot be parsed to 
enumeration type: %s"), val, enumeration.name ());
                        return enumv;
                }
                /**
diff --git a/gxml/Property.vala b/gxml/Property.vala
index f73a95e..8680beb 100644
--- a/gxml/Property.vala
+++ b/gxml/Property.vala
@@ -327,24 +327,78 @@ public class GXml.Boolean : GXml.BaseProperty {
 public class GXml.Enum : GXml.BaseProperty {
   protected int _value = 0;
   protected Type _enum_type;
+  /**
+   * Introspect the enumeration and use its nick to produce the value. Defaults to TRUE.
+   *
+   * An enum declared as 'ENUM_VALUE', its value is converted to 'enum-value'. Without
+   * this option the output is a complete name definition as declared by GLib.
+   *
+   * An enum value like : Myenum.ENUM_VALUE, is converted to 'enum-value' if no {@link camel_case}
+   * is enable and {@link use_nick} is enable; to 'EnumValue' if {@link camel_case} is true and
+   * {@link use_nick} is enable; and to 'MYENUM_ENUM_VALUE' if {@link use_nick} is set to false.
+   *
+   * An enum value like : Myenum.VALUE, is converted to 'value' if if no {@link camel_case}
+   * is enable; to 'Value' if  if no {@link camel_case} is enable; and to 'MYENUM_VALUE'
+   * if {@link use_nick} is set to false.
+   *
+   * The value can be converted to upper cases if {@link upper_case} is set to true.
+   *
+   * If  {@link use_nick} is set to false and {@link upper_case} is set to false, the value
+   * will be the lower case of the GLib default representation. For Myenum.ENUM_VALUE, the
+   * value will be 'myenum_enum_value'. Set {@link upper_case} to true, to keep the default
+   * GLib upper cases representation.
+   */
+  public bool use_nick { get; construct set; }
+  /**
+   * Tries to convert the value to CamelCase using its nick non canical name. Defaults to FALSE.
+   *
+   * An enum declared as 'ENUM_VALUE', its value is converted to 'EnumValue'. See
+   * {@link use_nick} for details.
+   */
+  public bool camel_case { get; construct set; }
+  /**
+   * The value output, is always converted to upper cases. See
+   * {@link use_nick} for details.
+   */
+  public bool upper_case { get; construct set; }
+
   public override string? value {
     owned get {
       string s = "";
       try {
-        s = Enumeration.get_string (enum_type, _value, true, true);
-      } catch {
-        GLib.warning (_("Error when transform enum to attribute's value"));
+        if (use_nick) {
+          if (camel_case) {
+            s = Enumeration.get_nick_camelcase (enum_type, _value);
+          } else {
+            s = Enumeration.get_nick (enum_type, _value);
+          }
+          if (upper_case) {
+            s = s.up ();
+          }
+        } else {
+          s = Enumeration.get_string (enum_type, _value, false, false);
+          if (!upper_case) {
+            s = s.down ();
+          }
+        }
+      } catch (GLib.Error e) {
+        GLib.warning (_("Error when transform enum to attribute's value: %s"), e.message);
       }
       return s;
     }
     set {
       try {
         _value = (int) Enumeration.parse (enum_type, value).value;
-      } catch {
-        GLib.warning (_("Error when transform from attribute string value to enum"));
+      } catch (GLib.Error e) {
+        GLib.warning (_("Error when transform from attribute string value to enum: %s"), e.message);
       }
     }
   }
+  construct {
+    use_nick = true;
+    camel_case = false;
+    upper_case = false;
+  }
   /**
    * Enum type used by property.
    */
diff --git a/test/SerializationTest.vala b/test/SerializationTest.vala
index 5633f36..8a2dddd 100644
--- a/test/SerializationTest.vala
+++ b/test/SerializationTest.vala
@@ -479,6 +479,10 @@ class SerializationTest : GXmlTest  {
     public Speed speed { get; set; }
     [Description (nick="::TensionType")]
     public TensionType tension_type { get; set; }
+    [Description (nick="::TensionSupply")]
+    public TensionSupply tension_supply { get; set; }
+    [Description (nick="::TensionClassification")]
+    public TensionClassification tension_class { get; set; }
     [Description (nick="::Tension")]
     public Tension tension { get; set; }
     [Description (nick="::Model")]
@@ -499,6 +503,16 @@ class SerializationTest : GXmlTest  {
       AC,
       DC
     }
+    public enum TensionSupplyEnum {
+      DEFAULT,
+      FROM_WALL,
+      FROM_FLOOR
+    }
+    public enum TensionClassificationEnum {
+      ONE,
+      TWO,
+      THREE
+    }
     public class On : GXml.Boolean {}
     public class Torque : GXml.Double {}
     public class Speed : GXml.Float {}
@@ -508,6 +522,21 @@ class SerializationTest : GXmlTest  {
         catch { assert_not_reached (); }
       }
     }
+    public class TensionSupply : GXml.Enum {
+      construct {
+        use_nick = true;
+        camel_case = true;
+        try { initialize_enum (typeof (TensionSupplyEnum)); }
+        catch { assert_not_reached (); }
+      }
+    }
+    public class TensionClassification : GXml.Enum {
+      construct {
+        upper_case = true;
+        try { initialize_enum (typeof (TensionClassificationEnum)); }
+        catch { assert_not_reached (); }
+      }
+    }
     public class Tension : GXml.Int {}
     public class Model : GXml.ArrayString {
       construct {
@@ -886,31 +915,19 @@ class SerializationTest : GXmlTest  {
       var m = new Motor ();
       string s = m.to_string ();
       assert (s != null);
-#if DEBUG
-      GLib.message ("DOC:"+s);
-#endif
       assert ("<Motor/>" in s);
       m.is_on = new Motor.On ();
       s = m.to_string ();
       assert (s != null);
-#if DEBUG
-      GLib.message ("DOC:"+s);
-#endif
       assert ("<Motor On=\"false\"/>" in s);
       m.torque = new Motor.Torque ();
       s = m.to_string ();
       assert (s != null);
-#if DEBUG
-      GLib.message ("DOC:"+s);
-#endif
       assert ("<Motor On=\"false\" Torque=\"0.0000\"/>" in s);
       m.speed = new Motor.Speed ();
       m.speed.set_double (1.0);
       s = m.to_string ();
       assert (s != null);
-#if DEBUG
-      GLib.message ("DOC:"+s);
-#endif
       assert ("<Motor On=\"false\" Torque=\"0.0000\" Speed=\"1.0000\"/>" in s);
       assert (m.speed != null);
       assert (m is GXml.Object);
@@ -918,26 +935,40 @@ class SerializationTest : GXmlTest  {
       assert (m.speed.get_double () == 1.0);
       assert (m.speed.value != null);
       assert (m.speed.value == "1.0000");
-#if DEBUG
-      message ("Searching Element's attribute node: speed");
-#endif
       assert (m.get_attribute ("speed") != null);
+
       assert (m.tension_type == null);
       m.tension_type = new Motor.TensionType ();
       s = m.to_string ();
       assert (s != null);
-#if DEBUG
-      GLib.message ("DOC:"+s);
-#endif
       assert ("<Motor On=\"false\" Torque=\"0.0000\" Speed=\"1.0000\" TensionType=\"ac\"/>" in s);
       assert (m.tension_type != null);
       assert (m.tension_type.value == "ac");
+
+      assert (m.tension_supply == null);
+      m.tension_supply = new Motor.TensionSupply ();
+      m.tension_supply.@value = "fromwall";
+      s = m.to_string ();
+      assert (s != null);
+      assert ("<Motor On=\"false\" Torque=\"0.0000\" Speed=\"1.0000\" TensionType=\"ac\" 
TensionSupply=\"FromWall\"/>" in s);
+      assert (m.tension_supply != null);
+      assert (m.tension_supply.value == "FromWall");
+      m.tension_supply = null;
+
+      assert (m.tension_class == null);
+      m.tension_class = new Motor.TensionClassification ();
+      m.tension_class.@value = "one";
+      s = m.to_string ();
+      message (s);
+      assert (s != null);
+      assert ("<Motor On=\"false\" Torque=\"0.0000\" Speed=\"1.0000\" TensionType=\"ac\" 
TensionClassification=\"ONE\"/>" in s);
+      assert (m.tension_class != null);
+      assert (m.tension_class.value == "ONE");
+      m.tension_class = null;
+
       m.tension = new Motor.Tension ();
       s = m.to_string ();
       assert (s != null);
-#if DEBUG
-      GLib.message ("DOC:"+s);
-#endif
       assert ("<Motor On=\"false\" Torque=\"0.0000\" Speed=\"1.0000\" TensionType=\"ac\" Tension=\"0\"/>" in 
s);
       m.is_on.set_boolean (true);
       m.torque.set_double (3.1416);
@@ -946,9 +977,6 @@ class SerializationTest : GXmlTest  {
       m.tension.set_integer (125);
       s = m.to_string ();
       assert (s != null);
-#if DEBUG
-      GLib.message ("DOC:"+s);
-#endif
       assert ("<Motor On=\"true\" Torque=\"3.1416\" Speed=\"3600.1011\" TensionType=\"dc\" 
Tension=\"125\"/>" in s);
       m.model = new Motor.Model ();
       assert (m.model != null);
@@ -958,9 +986,6 @@ class SerializationTest : GXmlTest  {
       assert (m.model.value == "Model3");
       s = m.to_string ();
       assert (s != null);
-#if DEBUG
-      GLib.message ("DOC:"+s);
-#endif
       assert ("<Motor On=\"true\" Torque=\"3.1416\" Speed=\"3600.1011\" TensionType=\"dc\" Tension=\"125\" 
Model=\"Model3\"/>" in s);
       assert (!m.model.is_valid_value ());
       assert (m.model.search ("MODEL1"));


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