suite tag impl

This commit is contained in:
a-Sansara 2018-08-10 02:12:08 +02:00
parent 18ceff507f
commit 3de39868e6
7 changed files with 231 additions and 149 deletions

View File

@ -25,7 +25,7 @@ GNU GPL v3
## Prerequisites ## Prerequisites
`valac meson ninja glib gee gobject pluie-echo` `valac meson ninja libyaml glib gobject gmodule gee pluie-echo`
see https://git.pluie.org/pluie/libpluie-echo in order to install pluie-echo-0.2 pkg see https://git.pluie.org/pluie/libpluie-echo in order to install pluie-echo-0.2 pkg
@ -42,7 +42,7 @@ sudo ninja install -C build
## Compilation ## Compilation
``` ```
valac --pkg gee-0.8 --pkg pluie-echo-0.2 --pkg pluie-yaml-0.4 main.vala valac --pkg pluie-echo-0.2 --pkg pluie-yaml-0.4 main.vala
``` ```
you can use `./build.sh` to rebuild/install the **pluie-yaml** lib and compile samples files you can use `./build.sh` to rebuild/install the **pluie-yaml** lib and compile samples files

View File

@ -31,6 +31,7 @@ project('libpluie-yaml', 'vala', 'c')
cc = meson.get_compiler('c') cc = meson.get_compiler('c')
dep_glib = dependency('glib-2.0') dep_glib = dependency('glib-2.0')
dep_gobject = dependency('gobject-2.0') dep_gobject = dependency('gobject-2.0')
dep_gmodule = dependency('gmodule-2.0')
dep_gio = dependency('gio-2.0') dep_gio = dependency('gio-2.0')
dep_gee = dependency('gee-0.8') dep_gee = dependency('gee-0.8')
dep_echo = dependency('pluie-echo-0.2') dep_echo = dependency('pluie-echo-0.2')
@ -92,7 +93,7 @@ libpluie_yaml = library(
sources, sources,
vala_header : 'pluie_yaml.h', vala_header : 'pluie_yaml.h',
vala_vapi : 'pluie-yaml-' +version+'.vapi', vala_vapi : 'pluie-yaml-' +version+'.vapi',
dependencies : [dep_glib, dep_gobject, dep_gio, dep_gee, dep_echo, dep_m, dep_yaml], dependencies : [dep_glib, dep_gobject, dep_gmodule, dep_gio, dep_gee, dep_echo, dep_m, dep_yaml],
install : true, install : true,
install_dir : [get_option('libdir'), incdir, true] install_dir : [get_option('libdir'), incdir, true]
) )
@ -100,7 +101,7 @@ libpluie_yaml = library(
pkgconfig = import('pkgconfig') pkgconfig = import('pkgconfig')
pkgconfig.generate( pkgconfig.generate(
libraries : libpluie_yaml, libraries : libpluie_yaml,
requires : 'glib-2.0 gobject-2.0 gio-2.0 gee-0.8 pluie-echo-0.2', requires : 'glib-2.0 gmodule-2.0 gobject-2.0 gio-2.0 gee-0.8 pluie-echo-0.2',
variables : 'datarootdir='+join_paths('${prefix}', get_option('datadir'))+'\ndatadir='+join_paths('${datarootdir}', 'pluie'), variables : 'datarootdir='+join_paths('${prefix}', get_option('datadir'))+'\ndatadir='+join_paths('${datarootdir}', 'pluie'),
version : version, version : version,
name : 'libpluie-yaml', name : 'libpluie-yaml',

View File

@ -7,9 +7,11 @@
type_int : !vala!int 3306 type_int : !vala!int 3306
type_bool : !vala!bool false type_bool : !vala!bool false
type_char : !vala!char c type_char : !vala!char c
type_string : !vala!string mystring1
!vala!Pluie.Yaml.Example test2 : !vala!Pluie.Yaml.Example test2 :
type_int : !vala!int 3306 type_int : !vala!int 3306
type_bool : !vala!bool true type_bool : !vala!bool true
type_char : !vala!char g type_char : !vala!char g
type_string : !vala!string mystring2

79
samples/yaml-tag.vala Normal file
View File

@ -0,0 +1,79 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* @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 = false;
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
Gee.HashMap<string, Yaml.Object> list = new Gee.HashMap<string, Yaml.Object> ();
if ((done = root != null)) {
foreach (var node in root) {
of.action ("Yaml.Object from node", node.name);
of.echo (node.to_string (false));
if ((obj = Yaml.Object.from_node (node)) != null) {
list[node.name] = obj;
}
else {
of.error ("cannot set Yaml.Object from node : %s".printf (node.name), true);
}
node = node.next_sibling ();
}
}
of.echo ("");
// hard code
Yaml.Example? o = new Pluie.Yaml.Example ();
foreach (var entry in list.entries) {
of.action ("Getting Hard coded values for Yaml.Object %s".printf (of.c (ECHO.MICROTIME).s (o.type_from_self ())), entry.key);
if ((o = entry.value as Yaml.Example)!=null) {
of.keyval("type_int" , "%d".printf(o.type_int));
of.keyval("type_bool" , "%s".printf(o.type_bool.to_string ()));
of.keyval("type_char" , "%c".printf(o.type_char));
of.keyval("type_string", "%s".printf(o.type_string));
}
}
of.rs (done);
of.echo ();
return (int) done;
}

View File

@ -1,98 +0,0 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* @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 = false;
var config = new Yaml.Config (path, true);
var root = config.root_node ();
root.display_childs ();
// define a map with base Yaml.Object
Gee.HashMap<string, Yaml.Object> list = new Gee.HashMap<string, Yaml.Object> ();
// require to register type;
Yaml.Example? obj = new Pluie.Yaml.Example ();
if ((done = root != null)) {
foreach (var node in root) {
of.action ("Yamlize Yaml.Example", node.name);
of.echo (node.to_string ());
if (node.tag != null) {
of.action ("tag value", node.tag.value);
var type = Yaml.Object.type_from_name (node.tag.value);
if (type != null && type.is_object ()) {
of.echo ("type founded : %s".printf (type.to_string ()));
//~ of.echo ("======");
//~ of.action ("Auto Instanciate object as Yaml.Object", type.name ());
//~ var o = (Yaml.Object) GLib.Object.new (type);
//~ if (o != null) {
//~ of.action ("Yamlize Yaml.Object", type.name ());
//~ of.state (o.yamlize (node));
//~ of.action ("Hardcode casting as", type.name ());
//~ var so = o as Yaml.Example;
//~ of.action ("Hardcode Getting values object", type.name ());
//~ of.keyval("type_int" , "%d".printf(so.type_int));
//~ of.keyval("type_bool", "%s".printf(so.type_bool.to_string ()));
//~ of.keyval("type_char", "%c".printf(so.type_char));
//~ of.echo ("======");
//~ }
list[node.name] = (Pluie.Yaml.Object) GLib.Object.new (type);
of.state (list[node.name].yamlize (node));
}
else {
of.warn ("type %s not found, you probably need to instanciate it first".printf (node.tag.value));
}
}
node = node.next_sibling ();
}
}
foreach (var entry in list.entries) {
of.action ("Getting values", entry.key);
if ((obj = entry.value as Yaml.Example)!=null) {
of.keyval("type_int" , "%d".printf(obj.type_int));
of.keyval("type_bool", "%s".printf(obj.type_bool.to_string ()));
of.keyval("type_char", "%c".printf(obj.type_char));
}
}
of.rs (done);
of.echo ();
return (int) done;
}

View File

@ -31,67 +31,163 @@ using Gee;
/** /**
* a test class to implements yamlize * a test class to implements yamlize
*/ */
public abstract class Pluie.Yaml.Object : GLib.Object public abstract class Pluie.Yaml.Object : GLib.Object
{ {
private static GLib.Module? p_module;
public static string type_name (string name) /**
*
*/
private static unowned GLib.Module p_open_module ()
{ {
return name.replace(".", ""); if (p_module == null) {
p_module = GLib.Module.open (null, 0);
} }
return p_module;
public static GLib.Type? type_from_name (string name)
{
GLib.Type? type = Type.from_name (type_name (name));
return type;
}
public string get_type_name ()
{
return Type.from_instance (this).name ();
} }
/** /**
* *
*/ */
public bool yamlize (Yaml.Node node) public static GLib.Type? type_from_string (string name)
{ {
bool done = false; GLib.Type? type = Type.from_name (name.replace(".", ""));
return type;
}
/**
* retriew GLib.Type related to specified vala name
* @param name a valid vala identifier name
*/
public static Type type_from_vala (string name)
{
void * s;
p_open_module ();
if (!p_module.symbol (resolve_c_name(@"$(name).get_type"), out s)) {
of.error ("cannot resolve type %s (not found)".printf (name));
}
return ((dlgType) s)();
}
/**
* retiew GLib.Type related to specified tag value.
* Type may not be registered yet
*/
public static Type? type_from_tag (string tagValue)
{
var type = type_from_string (tagValue);
if(type != null && type == Type.INVALID) {
type = type_from_vala (tagValue);
}
return type;
}
/**
* retiew GLib.Type related to instance
*/
public string type_from_self ()
{
return Type.from_instance (this).name ();
}
/**
* retriew corresponding c name related to specified vala name
* @param name a valid vala identifier name
*/
public static string resolve_c_name (string name)
{
string? str = null;
MatchInfo? mi = null;
StringBuilder sb = new StringBuilder ();
bool begin = true;
try { try {
var reg = new Regex ("([^.]*).?");
for (reg.match (name, 0, out mi) ; mi.matches () ; mi.next ()) {
if ((str = mi.fetch (1)) != null && str.length > 0) {
if (!begin) sb.append_unichar ('_');
else begin = false;
sb.append_unichar (str[0].tolower ());
sb.append (str.substring(1));
}
}
}
catch (GLib.RegexError e) {
of.error (e.message, true);
}
return !begin ? sb.str : name;
}
[CCode (has_target = false)]
private delegate Type dlgType();
/**
*
*/
public static Yaml.Object? from_node (Yaml.Node node)
{
Yaml.Object? obj = null;
try {
if (node.tag != null) {
if (Yaml.Scanner.DEBUG) of.action ("tag value", node.tag.value);
Type? type = type_from_tag (node.tag.value);
if (type != null && type.is_object ()) {
if (Yaml.Scanner.DEBUG) of.echo ("object type founded : %s".printf (type.to_string ()));
obj = (Yaml.Object) GLib.Object.new (type);
if (node!= null && !node.empty ()) { if (node!= null && !node.empty ()) {
Iterator<Yaml.Node> it = node.iterator (); GLib.ParamSpec? def = null;
Yaml.Node? snode = null;
foreach (var child in node) { foreach (var child in node) {
of.action ("yamlize ", child.to_string ()); if ((def = obj.get_class ().find_property (child.name)) != null) {
var pspec = this.get_class ().find_property (child.name); if ((snode = child.first ()) != null) {
if (pspec != null) { if (snode.tag != null) {
if (child.first ().tag != null) { obj.set_by_basic_type (def.name, def.value_type, snode);
of.keyval ("found tag", child.first ().tag.@value); }
//~ of.keyval ("value is `%s`", child.first ().data); else {
switch (child.first ().tag.@value) { obj.set (child.name, snode.data);
case "char" : }
this.set (child.name, child.first ().data[0]); }
break; }
case "bool" : }
this.set (child.name, bool.parse(child.first ().data.down ()));
break;
case "int" :
this.set (child.name, int.parse(child.first ().data));
break;
} }
} }
else { else {
this.set (child.name, child.first ().data); of.echo ("searched type : %s".printf (type.to_string ()));
}
}
} }
} }
} }
catch (GLib.Error e) { catch (GLib.Error e) {
of.warn (e.message); of.warn (e.message);
done = false;
} }
done = true; return obj;
return done; }
/**
*
*/
public void set_by_basic_type (string name, GLib.Type type, Yaml.Node node)
{
GLib.Value v = GLib.Value(type);
var data = node.data;
if (Yaml.Scanner.DEBUG) {
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.BOOLEAN :
v.set_boolean (bool.parse(data.down ()));
break;
case Type.INT :
v.set_int(int.parse(data));
break;
}
this.set_property(name, v);
} }
} }

View File

@ -271,6 +271,7 @@ public class Pluie.Yaml.Processor
*/ */
private void on_tag_directive () private void on_tag_directive ()
{ {
if (Yaml.Scanner.DEBUG)
of.action ("on_tag_directive %s".printf (this.event.data["handle"]), this.event.data["prefix"]); of.action ("on_tag_directive %s".printf (this.event.data["handle"]), this.event.data["prefix"]);
this.tags[this.event.data["handle"]] = this.event.data["prefix"]; this.tags[this.event.data["handle"]] = this.event.data["prefix"];
} }
@ -315,8 +316,9 @@ public class Pluie.Yaml.Processor
private void on_tag (bool onKey = false) private void on_tag (bool onKey = false)
{ {
if (this.event.evtype.is_tag ()) { if (this.event.evtype.is_tag ()) {
if (Yaml.Scanner.DEBUG)
of.keyval ("tag %s".printf (this.event.data["handle"]), this.event.data["suffix"]); of.keyval ("tag %s".printf (this.event.data["handle"]), this.event.data["suffix"]);
if (this.tags.contains (this.event.data["handle"])) { if (this.tags.has_key (this.event.data["handle"])) {
var tag = new Yaml.Tag (this.event.data["suffix"], this.tags[this.event.data["handle"]]); var tag = new Yaml.Tag (this.event.data["suffix"], this.tags[this.event.data["handle"]]);
if (onKey) if (onKey)
this.keyTag = tag; this.keyTag = tag;
@ -449,12 +451,12 @@ public class Pluie.Yaml.Processor
private void on_update () private void on_update ()
{ {
if (this.node != null) { if (this.node != null) {
of.echo (this.node.name); if (Yaml.Scanner.DEBUG) of.echo (this.node.name);
} }
if (this.change) { if (this.change) {
of.action ("on change", this.node.name); if (Yaml.Scanner.DEBUG) of.action ("on change", this.node.name);
if (this.keyTag != null) { if (this.keyTag != null) {
of.action ("setting tag", this.keyTag.@value); if (Yaml.Scanner.DEBUG) of.action ("setting tag", this.keyTag.@value);
this.node.tag = this.keyTag; this.node.tag = this.keyTag;
} }
else if (this.valueTag != null) { else if (this.valueTag != null) {