[gxml] Enum: holds invalid strings to avoid data lost



commit 4a5ba80a08b9f90d58f67a1c6b4019199c03b97e
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue Jul 9 14:46:48 2019 -0500

    Enum: holds invalid strings to avoid data lost
    
    If a deserialization of an enum property yields
    to an invalid string of an enum, then that is
    hold and used to serialize back, so no data is
    lost and is tolerant for extensions not included
    in the current implementation

 gxml/Property.vala          | 14 +++++++++++++-
 test/SerializationTest.vala |  4 +++-
 2 files changed, 16 insertions(+), 2 deletions(-)
---
diff --git a/gxml/Property.vala b/gxml/Property.vala
index 8680beb..889c514 100644
--- a/gxml/Property.vala
+++ b/gxml/Property.vala
@@ -327,6 +327,7 @@ public class GXml.Boolean : GXml.BaseProperty {
 public class GXml.Enum : GXml.BaseProperty {
   protected int _value = 0;
   protected Type _enum_type;
+  protected string _val = null;
   /**
    * Introspect the enumeration and use its nick to produce the value. Defaults to TRUE.
    *
@@ -364,6 +365,9 @@ public class GXml.Enum : GXml.BaseProperty {
 
   public override string? value {
     owned get {
+      if (_val != null) {
+        return _val;
+      }
       string s = "";
       try {
         if (use_nick) {
@@ -389,8 +393,10 @@ public class GXml.Enum : GXml.BaseProperty {
     set {
       try {
         _value = (int) Enumeration.parse (enum_type, value).value;
+        _val = null;
       } catch (GLib.Error e) {
-        GLib.warning (_("Error when transform from attribute string value to enum: %s"), e.message);
+        GLib.message (_("Error when transform from attribute string value to enum: %s"), e.message);
+        _val = value;
       }
     }
   }
@@ -420,6 +426,12 @@ public class GXml.Enum : GXml.BaseProperty {
    * Sets current value.
    */
   public void set_enum (int value) { _value = value; }
+  /**
+   *
+   */
+  public bool is_valid () {
+    return _val == null;
+  }
 }
 
 /**
diff --git a/test/SerializationTest.vala b/test/SerializationTest.vala
index 8a2dddd..a9c87d8 100644
--- a/test/SerializationTest.vala
+++ b/test/SerializationTest.vala
@@ -952,7 +952,9 @@ class SerializationTest : GXmlTest  {
       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");
+      assert (m.tension_supply.@value == "FromWall");
+      m.tension_supply.@value = "UnkNown";
+      assert (m.tension_supply.@value == "UnkNown");
       m.tension_supply = null;
 
       assert (m.tension_class == null);


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