fix set enum value

This commit is contained in:
a-sansara 2018-08-17 18:46:22 +02:00 committed by a-Sansara
parent 2692c633ae
commit 68d2c026d4
4 changed files with 115 additions and 34 deletions

View File

@ -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

54
samples/yaml-tag2.vala Normal file
View File

@ -0,0 +1,54 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* @software : lib-yaml <https://git.pluie.org/pluie/lib-yaml>
* @version : 0.4
* @date : 2018
* @licence : GPLv3.0 <http://www.gnu.org/licenses/>
* @author : a-Sansara <[dev]at[pluie]dot[org]>
* @copyright : pluie.org <http://www.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 <http://www.gnu.org/licenses/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
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;
}

View File

@ -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; }
/**
*

View File

@ -185,6 +185,10 @@ 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);
}
if (type.is_a(Type.ENUM)) {
this.set_enum_value (ref v, type, data);
}
else {
switch (type)
{
case Type.STRING :
@ -220,6 +224,27 @@ public abstract class Pluie.Yaml.Object : GLib.Object
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);
}
}
}