[gxml] GOM: Added support to boolean and enumerations



commit 325431d7df74a108fe2fb1af5b39da658752688b
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Nov 3 18:11:21 2016 -0600

    GOM: Added support to boolean and enumerations
    
    GomObject now have support to serialize boolean
    and enumeration values

 gxml/GomObject.vala            |   12 ++++++++++++
 test/GomSerializationTest.vala |   18 ++++++++++++++++--
 2 files changed, 28 insertions(+), 2 deletions(-)
---
diff --git a/gxml/GomObject.vala b/gxml/GomObject.vala
index 9fd7084..18abfc1 100644
--- a/gxml/GomObject.vala
+++ b/gxml/GomObject.vala
@@ -89,6 +89,18 @@ public interface GXml.GomObject : GLib.Object,
       if (prop.value_type.is_a (typeof (double))) {
         return ((double) v).to_string ();
       }
+      if (prop.value_type.is_a (typeof (bool))) {
+        return ((bool) v).to_string ();
+      }
+      if (prop.value_type.is_a (Type.ENUM)) {
+        var n = v.get_enum ();
+        try {
+          return Enumeration.get_string (prop.value_type, n, true, true);
+        } catch {
+          GLib.warning (_("Enumeration is out of range"));
+        }
+        return "";
+      }
     }
     return null;
   }
diff --git a/test/GomSerializationTest.vala b/test/GomSerializationTest.vala
index 2ba5edc..52b7f6b 100644
--- a/test/GomSerializationTest.vala
+++ b/test/GomSerializationTest.vala
@@ -1,4 +1,4 @@
-/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
+/* -*- Mode: vala; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
 /* Notation.vala
  *
  * Copyright (C) 2016  Daniel Espinosa <esodan gmail com>
@@ -43,10 +43,18 @@ class GomSerializationTest : GXmlTest  {
   public class Taxes : GomElement {
     [Description (nick="::monthRate")]
     public double month_rate { get; set; }
+    [Description (nick="::TaxFree")]
+    public bool tax_free { get; set; }
+    [Description (nick="::Month")]
+    public Month month { get; set; }
     construct {
       _local_name = "Taxes";
     }
     public string to_string () { return (_document as GomDocument).to_string (); }
+    public enum Month {
+      JANUARY,
+      FEBRUARY
+    }
   }
   public static void add_tests () {
     Test.add_func ("/gxml/gom-serialization/write/properties", () => {
@@ -77,10 +85,16 @@ class GomSerializationTest : GXmlTest  {
       var t = new Taxes ();
       string s = t.to_string ();
       assert (s != null);
-      assert ("<Taxes monthRate=\"0\"/>" in s);
+      assert ("<Taxes monthRate=\"0\" Month=\"january\" 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"));
+      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");
       s = t.to_string ();
       GLib.message ("DOC:"+s);
     });


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