diff --git a/resources/tag.yml b/resources/tag.yml index 10e11a2..9928736 100644 --- a/resources/tag.yml +++ b/resources/tag.yml @@ -12,6 +12,7 @@ type_uint : !v!uint 62005 type_float : !v!float 42.36 type_double : !v!double 95542123.4579512128 + type_enum : !v!Pluie.Yaml.NODE_TYPE SCALAR !v!Pluie.Yaml.SubExample type_object : toto : totovalue1 tata : tatavalue1 diff --git a/samples/yaml-tag2.vala b/samples/yaml-tag2.vala new file mode 100644 index 0000000..a35587f --- /dev/null +++ b/samples/yaml-tag2.vala @@ -0,0 +1,54 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * + * @software : lib-yaml + * @version : 0.4 + * @date : 2018 + * @licence : GPLv3.0 + * @author : a-Sansara <[dev]at[pluie]dot[org]> + * @copyright : pluie.org + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * + * This file is part of lib-yaml. + * + * lib-yaml is free software (free as in speech) : you can redistribute it + * and/or modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * lib-yaml 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 General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with lib-yaml. If not, see . + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ + +using GLib; +using Gee; +using Pluie; + +int main (string[] args) +{ + Echo.init(false); + + var path = Yaml.DATA_PATH + "/tag.yml"; + var done = false; + + of.title ("Pluie Yaml Library", Pluie.Yaml.VERSION, "a-sansara"); + Pluie.Yaml.Scanner.DEBUG = true; + Yaml.Object? obj = null; + var config = new Yaml.Config (path, true); + var root = config.root_node (); + root.display_childs (); + // define a map with base Yaml.Object type rather than target type + obj = Yaml.Object.from_node (root.first()); + + of.rs (done); + of.echo (); + return (int) done; + +} diff --git a/src/vala/Pluie/Yaml.Example.vala b/src/vala/Pluie/Yaml.Example.vala index 15d77c3..b9b9fba 100644 --- a/src/vala/Pluie/Yaml.Example.vala +++ b/src/vala/Pluie/Yaml.Example.vala @@ -58,6 +58,7 @@ public class Pluie.Yaml.Example : Yaml.Object public uint64 type_uint64 { get; set; } public bool type_bool { get; set; } public Yaml.SubExample type_object { get; set; } + public Yaml.NODE_TYPE type_enum { get; set; } /** * diff --git a/src/vala/Pluie/Yaml.Object.vala b/src/vala/Pluie/Yaml.Object.vala index bcf3365..f74ba2f 100644 --- a/src/vala/Pluie/Yaml.Object.vala +++ b/src/vala/Pluie/Yaml.Object.vala @@ -185,41 +185,66 @@ public abstract class Pluie.Yaml.Object : GLib.Object of.action("Auto setting property value %s".printf (of.c (ECHO.MICROTIME).s (type.name ())), name); of.echo (data); } - switch (type) - { - case Type.STRING : - v.set_string(data); - break; - case Type.CHAR : - v.set_schar((int8)data.data[0]); - break; - case Type.UCHAR : - v.set_uchar((uint8)data.data[0]); - break; - case Type.BOOLEAN : - v.set_boolean (data == "1" || data.down () == "true"); - break; - case Type.INT : - v.set_int(int.parse(data)); - break; - case Type.UINT : - v.set_uint((uint)long.parse(data)); - break; - case Type.LONG : - case Type.INT64 : - v.set_long((long)int64.parse(data)); - break; - case Type.ULONG : - case Type.UINT64 : - v.set_ulong((ulong)uint64.parse(data)); - break; - case Type.FLOAT : - v.set_float((float)double.parse(data)); - break; - case Type.DOUBLE : - v.set_double(double.parse(data)); - break; + if (type.is_a(Type.ENUM)) { + this.set_enum_value (ref v, type, data); + } + else { + switch (type) + { + case Type.STRING : + v.set_string(data); + break; + case Type.CHAR : + v.set_schar((int8)data.data[0]); + break; + case Type.UCHAR : + v.set_uchar((uint8)data.data[0]); + break; + case Type.BOOLEAN : + v.set_boolean (data == "1" || data.down () == "true"); + break; + case Type.INT : + v.set_int(int.parse(data)); + break; + case Type.UINT : + v.set_uint((uint)long.parse(data)); + break; + case Type.LONG : + case Type.INT64 : + v.set_long((long)int64.parse(data)); + break; + case Type.ULONG : + case Type.UINT64 : + v.set_ulong((ulong)uint64.parse(data)); + break; + case Type.FLOAT : + v.set_float((float)double.parse(data)); + break; + case Type.DOUBLE : + v.set_double(double.parse(data)); + break; + } } this.set_property(name, v); } + + /** + * + */ + public void set_enum_value (ref Value v, GLib.Type type, string data) + { + EnumClass eclass = (EnumClass) type.class_ref(); + unowned EnumValue? evalue = eclass.get_value_by_name(data); + if (evalue == null) { + evalue = eclass.get_value_by_nick(data.down()); + int64 e = 0; + if(evalue == null) { + if(!int64.try_parse(data, out e)) { + Dbg.error ("invalid enum value %s".printf(data), Log.METHOD, Log.LINE); + } + } + e = evalue.value; + v.set_enum((int)e); + } + } }