[gxml/serialization] Added convenient Enumeration class manipulate Enum types
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml/serialization] Added convenient Enumeration class manipulate Enum types
- Date: Fri, 8 Nov 2013 00:04:08 +0000 (UTC)
commit 6abcfbb98ed53ada56616181f92b820fff4b685d
Author: Daniel Espinosa <esodan gmail com>
Date: Thu Nov 7 16:15:18 2013 -0600
Added convenient Enumeration class manipulate Enum types
* Added tests cases for Enumeration class
gxml/Enumeration.vala | 143 +++++++++++++++++++++++++++++++++
gxml/Makefile.am | 1 +
test/SerializableObjectModelTest.vala | 71 ++++++++++++++++-
3 files changed, 214 insertions(+), 1 deletions(-)
---
diff --git a/gxml/Enumeration.vala b/gxml/Enumeration.vala
new file mode 100644
index 0000000..3188a0b
--- /dev/null
+++ b/gxml/Enumeration.vala
@@ -0,0 +1,143 @@
+/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/* Serializable.vala
+ *
+ + Copyright (C) 2013 Daniel Espinosa <esodan gmail com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Daniel Espinosa <esodan gmail com>
+ */
+
+using GXml;
+
+namespace GXml {
+ public class Enumeration
+ {
+ /**
+ * Introspect an enumeration to get value's nick name.
+ *
+ * Returns: an string representing an enumeration's value.
+ *
+ * @enumeration: a { link GLib.Type} of type { link GLib.Type.ENUM}
+ * @val: an integer to parse an enum value of type @enumeration.
+ */
+ public static string get_nick (Type enumeration, int val) throws GLib.Error
+ {
+ return get_string (enumeration, val, true);
+ }
+ /**
+ * Introspect an enumeration to get value's nick name and transform
+ * to camel case representation.
+ *
+ * Returns: an string representing an enumeration's value.
+ *
+ * @enumeration: a { link GLib.Type} of type { link GLib.Type.ENUM}
+ * @val: an integer to parse an enum value of type @enumeration.
+ */
+ public static string get_nick_camelcase (Type enumeration, int val) throws GLib.Error
+ {
+ return get_string (enumeration, val, false, true);
+ }
+ /**
+ * Transform enumeration's value to its string representation.
+ *
+ * Returns: an string representing an enumeration's value.
+ *
+ * @enumeration: a { link GLib.Type} of type { link GLib.Type.ENUM}
+ * @val: an integer to parse an enum value of type @enumeration.
+ * @use_nick: makes to returns value's nick name in { link GLib.EnumClass}
+ * @camelcase: makes to returns value's nick name in { link GLib.EnumClass}
+ * as camel case representation. If @use_nick is set this take no effect.
+ */
+ public static string get_string (Type enumeration, int val,
+ bool use_nick = false,
+ bool camelcase = false)
+ throws GLib.Error
+ requires (enumeration.is_a (Type.ENUM))
+ {
+ string camel = "";
+ EnumClass enumc = (EnumClass) enumeration.class_ref ();
+ EnumValue? enumv = enumc.get_value (val);
+ if (enumv == null)
+ throw new SerializableEnumError.INVALID_VALUE ("value is invalid");
+ if (use_nick && enumv.value_nick != null)
+ return enumv.value_nick;
+ if (camelcase && enumv.value_nick != null) {
+ string[] sp = enumv.value_nick.split ("-");
+ foreach (string s in sp) {
+ camel += @"$(s[0])".up () + @"$(s[1:s.length])";
+ }
+ }
+ else
+ camel = enumv.value_name;
+ return camel;
+ }
+ /**
+ * Parse @val to an enumeration's value.
+ *
+ * Returns: an { link GLib.EnumValue} representing an enumeration's value.
+ *
+ * @enumeration: a { link GLib.Type} of type { link GLib.Type.ENUM}
+ * @val: a string to parse an enum value of type @enumeration.
+ * @camelcase: makes to returns value's nick name in { link GLib.EnumClass}
+ * as camel case representation. If @use_nick is set this take no effect.
+ */
+ public static EnumValue? parse (Type enumeration, string val)
+ throws GLib.Error
+ requires (enumeration.is_a (Type.ENUM))
+ {
+ EnumClass enumc = (EnumClass) enumeration.class_ref ();
+ EnumValue? enumv = null;
+ foreach (EnumValue ev in enumc.values) {
+ if (val == ev.value_name)
+ enumv = ev;
+ if (val == ev.value_nick)
+ enumv = ev;
+ if (val == get_nick_camelcase (enumeration, ev.value))
+ enumv = ev;
+ }
+ if (enumv == null)
+ throw new SerializableEnumError.INVALID_NAME ("text is not valid");
+ return enumv;
+ }
+ /**
+ * Transform an enumeration in an array of { link GLib.EnumValue}.
+ *
+ * Returns: an array of { link GLib.EnumValue} representing an enumeration.
+ *
+ * @enumeration: a { link GLib.Type} of type { link GLib.Type.ENUM}
+ * @val: a string to parse an enum value of type @enumeration.
+ * @camelcase: makes to returns value's nick name in { link GLib.EnumClass}
+ * as camel case representation. If @use_nick is set this take no effect.
+ */
+ public static unowned EnumValue[] to_array (Type enumeration)
+ requires (enumeration.is_a (Type.ENUM))
+ {
+ EnumClass enumc = (EnumClass) enumeration.class_ref ();
+ return enumc.values;
+ }
+ }
+ public errordomain SerializableEnumError
+ {
+ /**
+ * Given value is invalid in enumeration, when transform to string.
+ */
+ INVALID_VALUE,
+ /**
+ * Given text to transform to an enumeration's value.
+ */
+ INVALID_NAME
+ }
+}
diff --git a/gxml/Makefile.am b/gxml/Makefile.am
index b148f08..ed8aeb4 100644
--- a/gxml/Makefile.am
+++ b/gxml/Makefile.am
@@ -61,6 +61,7 @@ libgxml_la_SOURCES = \
ProcessingInstruction.vala \
Text.vala \
Serializable.vala \
+ Enumeration.vala \
SerializableObjectModel.vala \
SerializableJson.vala \
Serialization.vala \
diff --git a/test/SerializableObjectModelTest.vala b/test/SerializableObjectModelTest.vala
index 215f912..5adeb5f 100644
--- a/test/SerializableObjectModelTest.vala
+++ b/test/SerializableObjectModelTest.vala
@@ -259,6 +259,21 @@ class UnknownAttribute : ObjectModel
public FakeSerializable fake { get; set; }
}
+public enum OptionsEnum
+{
+ [Description (nick="SelectionOption")]
+ SelectBefore,
+ HoldOn,
+ LeaveHeare,
+ NORMAL_OPERATION
+}
+
+class Options : Object
+{
+ public string test { get; set; }
+ public OptionsEnum options { get; set; }
+}
+
class SerializableObjectModelTest : GXmlTest
{
public static void add_tests ()
@@ -810,7 +825,7 @@ class SerializableObjectModelTest : GXmlTest
assert_not_reached ();
}
});
- Test.add_func ("/gxml/serializable/object_model/serialize_unknown_property",
+ Test.add_func ("/gxml/serializable/object_model/deserialize_unknown_property",
() => {
var doc = new Document.from_string ("""<?xml version="1.0"?>
<UnknownAttribute ignore="true" ignore2="test">
@@ -890,6 +905,60 @@ class SerializableObjectModelTest : GXmlTest
assert_not_reached ();
}
});
+ Test.add_func ("/gxml/serializable/object_model/enumeration",
+ () => {
+ var doc = new Document ();
+ var e = new Options ();
+ try {
+ e.test = "t1";
+ e.options = OptionsEnum.SelectBefore;
+ string s = Enumeration.get_string (typeof (OptionsEnum), e.options);
+ if (s != "OPTIONS_ENUM_SelectBefore") {
+ stdout.printf (@"ERROR: Bad Enum stringification: $(s)");
+ assert_not_reached ();
+ }
+ s = Enumeration.get_nick (typeof (OptionsEnum), e.options);
+ if (s != "selectbefore") {
+ stdout.printf (@"ERROR: Bad Enum nick name: $(s)");
+ assert_not_reached ();
+ }
+ s = Enumeration.get_nick (typeof (OptionsEnum),OptionsEnum.NORMAL_OPERATION);
+ if (s != "normal-operation") {
+ stdout.printf (@"ERROR: Bad Enum nick name: $(s)");
+ assert_not_reached ();
+ }
+ s = Enumeration.get_nick_camelcase (typeof
(OptionsEnum),OptionsEnum.NORMAL_OPERATION);
+ if (s != "NormalOperation") {
+ stdout.printf (@"ERROR: Bad Enum nick name: $(s)");
+ assert_not_reached ();
+ }
+ try {
+ Enumeration.parse (typeof (OptionsEnum), "selectbefore");
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"ERROR PARSING selectbefore: $(e.message)");
+ assert_not_reached ();
+ }
+ try {
+ Enumeration.parse (typeof (OptionsEnum), "normal-operation");
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"ERROR PARSING normal-operation: $(e.message)");
+ assert_not_reached ();
+ }
+ try {
+ Enumeration.parse (typeof (OptionsEnum), "NormalOperation");
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"ERROR PARSING NormalOperation: $(e.message)");
+ assert_not_reached ();
+ }
+ }
+ catch (GLib.Error e) {
+ stdout.printf (@"Error: $(e.message)");
+ assert_not_reached ();
+ }
+ });
}
static void serialize_manual_check (Element element, Manual manual)
{
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]