This commit is contained in:
a-Sansara 2018-08-22 11:17:29 +02:00
parent 804edebbee
commit 1366bcea29
7 changed files with 165 additions and 63 deletions

View File

@ -73,7 +73,6 @@ sources = [
'src/vala/Pluie/Yaml.ExampleChild.vala', 'src/vala/Pluie/Yaml.ExampleChild.vala',
'src/vala/Pluie/Yaml.ExampleStruct.vala', 'src/vala/Pluie/Yaml.ExampleStruct.vala',
'src/vala/Pluie/Yaml.Finder.vala', 'src/vala/Pluie/Yaml.Finder.vala',
'src/vala/Pluie/Yaml.GeeBuilder.vala',
'src/vala/Pluie/Yaml.Loader.vala', 'src/vala/Pluie/Yaml.Loader.vala',
'src/vala/Pluie/Yaml.Mapping.vala', 'src/vala/Pluie/Yaml.Mapping.vala',
'src/vala/Pluie/Yaml.Node.vala', 'src/vala/Pluie/Yaml.Node.vala',

View File

@ -44,19 +44,3 @@
titi : 44 titi : 44
tutu : 0 tutu : 0
!v!Pluie.Yaml.Example test2 :
myname : test2object
type_int : 3306
type_bool : "true"
type_char : g
type_string : mystring2
type_uchar : Y
type_uint : 63005
type_float : 5.28
type_double : 9.28
type_enum : PLUIE_YAML_NODE_TYPE_SCALAR
!v!Pluie.Yaml.ExampleChild type_object :
toto : totovalue2
tata : tatavalue2
titi : !v!int 456
tutu : !v!bool TRUE

99
samples/yaml-test.vala Normal file
View File

@ -0,0 +1,99 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* @software : pluie-yaml <https://git.pluie.org/pluie/lib-yaml>
* @version : 0.5
* @type : library
* @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 pluie-yaml.
*
* pluie-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.
*
* pluie-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 pluie-yaml. If not, see <http://www.gnu.org/licenses/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
using GLib;
using Gee;
using Pluie;
class Foo {
public signal void sig (int x);
}
class Bar {
private int data = 42;
public void handler (int x) {
stdout.printf ("%d Data via instance: %d\n", x, this.data);
}
}
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.DEBUG = false;
var config = new Yaml.Config (path, true);
var root = config.root_node () as Yaml.Root;
root.first ().display_childs ();
of.action ("with signal Yaml.Builder.from_node", root.first ().name);
Yaml.Example obj = (Yaml.Example) Yaml.Builder.from_node (root.first ());
obj.type_object.method_a ();
if (obj.type_gee_al != null) {
of.keyval("type_gee_al", "(%s)" .printf(obj.type_gee_al.get_type ().name ()));
foreach (var v in obj.type_gee_al) {
of.echo(" - item : %f".printf (v));
}
of.keyval("type_gee_alobject", "(%s)" .printf(obj.type_gee_alobject.get_type ().name ()));
foreach (var child in obj.type_gee_alobject) {
of.echo(" - item toto : %s".printf (child.toto));
of.echo(" - item tata : %s".printf (child.tata));
}
}
of.action ("with signal Yaml.Builder.to_node", obj.yaml_name);
var n = Yaml.Builder.to_node (obj);
if ((done = n !=null)) {
n.display_childs ();
}
of.rs (done);
of.echo ();
var foo = new Foo ();
int data = 52;
foo.sig.connect ((x) => { // 'user_data' in C code = variables from outer context
stdout.printf ("%d Data via closure: %d\n", x, data);
});
var bar = new Bar ();
foo.sig.connect (bar.handler); // 'user_data' in C code = 'bar'
// Emit signal
foo.sig (73);
return (int) done;
}

View File

@ -48,7 +48,7 @@ public class Pluie.Yaml.Builder
private static unowned GLib.Module p_open_module () private static unowned GLib.Module p_open_module ()
{ {
if (p_module == null) { if (p_module == null) {
p_module = GLib.Module.open (null, 0); p_module = Module.open (null, 0);
} }
return p_module; return p_module;
} }
@ -58,7 +58,7 @@ public class Pluie.Yaml.Builder
*/ */
public static GLib.Type? type_from_string (string name) public static GLib.Type? type_from_string (string name)
{ {
GLib.Type? type = Type.from_name (name.replace(".", "")); Type? type = Type.from_name (name.replace(".", ""));
return type; return type;
} }
@ -110,7 +110,7 @@ public class Pluie.Yaml.Builder
} }
} }
} }
catch (GLib.RegexError e) { catch (RegexError e) {
of.error (e.message, true); of.error (e.message, true);
} }
return !begin ? sb.str : name; return !begin ? sb.str : name;
@ -169,18 +169,11 @@ public class Pluie.Yaml.Builder
/** /**
* *
*/ */
public static void set_from_collection (ref Yaml.Object obj, GLib.Type parentType, Yaml.Node node, GLib.Type type) public static void set_from_collection (ref Yaml.Object obj, Type parentType, Yaml.Node node, Type type)
{ {
Yaml.dbg (" > set_from_collection %s (%s)".printf (node.name, type.name ())); Yaml.dbg (" > set_from_collection %s (%s)".printf (node.name, type.name ()));
if (type.is_a (typeof (Yaml.Object))) { if (type.is_a (typeof (Yaml.Object)) || Yaml.Object.register.is_registered_type (parentType, type)) {
obj.set (node.name, Yaml.Builder.from_node(node, type)); obj.populate_from_node (node.name, type, node);
}
else if (type.is_a (typeof (Gee.ArrayList))) {
Yaml.GeeBuilder.arraylist_from_node(ref obj, node, type);
}
else if (Yaml.Object.register.is_registered_type(parentType, type)) {
Yaml.dbg ("%s is a registered type".printf (type.name ()));
obj.populate_from_node (type, node);
} }
else { else {
Dbg.error ("%s is not registered and cannot be populated".printf (type.name ()), Log.METHOD, Log.LINE); Dbg.error ("%s is not registered and cannot be populated".printf (type.name ()), Log.METHOD, Log.LINE);
@ -332,13 +325,8 @@ public class Pluie.Yaml.Builder
foreach (var def in obj.get_class ().list_properties ()){ foreach (var def in obj.get_class ().list_properties ()){
name = Yaml.Builder.transform_param_name(def.name); name = Yaml.Builder.transform_param_name(def.name);
if (name != null && name != "yaml_name") { if (name != null && name != "yaml_name") {
if (def.value_type.is_a (typeof (Gee.ArrayList))) { if (def.value_type.is_a (typeof (Yaml.Object)) || Yaml.Object.register.is_registered_type(obj.get_type (), def.value_type)) {
void *p; var child = obj.populate_to_node(name, def.value_type, node);
obj.get(name, out p);
Yaml.GeeBuilder.arraylist_to_node(p, name, node);
}
else if (def.value_type.is_a (typeof (Yaml.Object)) || Yaml.Object.register.is_registered_type(obj.get_type (), def.value_type)) {
var child = obj.populate_to_node(def.value_type, name);
if (child != null) { if (child != null) {
child.tag = new Yaml.Tag (Yaml.Object.register.resolve_namespace_type(def.value_type), "v"); child.tag = new Yaml.Tag (Yaml.Object.register.resolve_namespace_type(def.value_type), "v");
node.add (child); node.add (child);

View File

@ -83,8 +83,8 @@ public class Pluie.Yaml.Example : Yaml.Object
{ {
this.type_gee_al = new Gee.ArrayList<double?> (); this.type_gee_al = new Gee.ArrayList<double?> ();
this.type_gee_alobject = new Gee.ArrayList<Yaml.ExampleChild> (); this.type_gee_alobject = new Gee.ArrayList<Yaml.ExampleChild> ();
// base.yaml_init (); register.add_namespace("Gee");
Dbg.msg ("Yaml.Object %s (%s) instantiated".printf (this.yaml_name, this.get_type().name ()), Log.LINE, Log.FILE); Dbg.msg ("%s (%s) instantiated".printf (this.yaml_name, this.get_type().name ()), Log.LINE, Log.FILE);
} }
/** /**
@ -92,31 +92,58 @@ public class Pluie.Yaml.Example : Yaml.Object
*/ */
protected override void yaml_init () protected override void yaml_init ()
{ {
// base.yaml_init (); Dbg.msg ("%s (%s) initialized".printf (this.yaml_name, this.get_type().name ()), Log.LINE, Log.FILE);
Dbg.msg ("Yaml.Object %s (%s) initialized".printf (this.yaml_name, this.get_type().name ()), Log.LINE, Log.FILE);
} }
/** /**
* *
*/ */
public override void populate_from_node(GLib.Type type, Yaml.Node node) public override void populate_from_node (string name, GLib.Type type, Yaml.Node node) {
{
if (type == typeof (Yaml.ExampleStruct)) { if (type == typeof (Yaml.ExampleStruct)) {
this.type_struct = ExampleStruct.from_yaml_node (node); this.type_struct = ExampleStruct.from_yaml_node (node);
} }
else if (type == typeof (Gee.ArrayList)) {
foreach (var child in node) {
switch (name) {
case "type_gee_al":
this.type_gee_al.add(double.parse(child.data));
break;
case "type_gee_alobject":
this.type_gee_alobject.add((Yaml.ExampleChild) Yaml.Builder.from_node (child, typeof (Yaml.ExampleChild)));
break;
}
}
}
else {
base.populate_from_node (name, type, node);
}
} }
/** /**
* *
*/ */
public override Yaml.Node? populate_to_node(GLib.Type type, string name) public override Yaml.Node? populate_to_node (string name, GLib.Type type, Yaml.Node parent) {
{ Yaml.Node? node = base.populate_to_node (name, type, parent);
Yaml.Node? node = base.populate_to_node (type, name);
if (node == null) { if (node == null) {
if (type == typeof (Yaml.ExampleStruct)) { if (type == typeof (Yaml.ExampleStruct)) {
Yaml.ExampleStruct p = this.type_struct; Yaml.ExampleStruct p = this.type_struct;
node = ExampleStruct.to_yaml_node (ref p, name); node = ExampleStruct.to_yaml_node (ref p, name);
} }
else if (type == typeof (Gee.ArrayList)) {
node = new Yaml.Sequence (parent, name);
switch (name) {
case "type_gee_al" :
foreach (var data in this.type_gee_al) {
new Yaml.Scalar (node, data.to_string ());
}
break;
case "type_gee_alobject" :
foreach (var data in this.type_gee_alobject) {
Yaml.Builder.to_node (data, node, false);
}
break;
}
}
} }
return node; return node;
} }

View File

@ -117,19 +117,22 @@ public class Pluie.Yaml.GeeBuilder
obj.set (node.name, l); obj.set (node.name, l);
break; break;
case Type.DOUBLE : case Type.DOUBLE :
var l = new Gee.ArrayList<double?> ();
//~ .add(double.parse(child.data));
//~ var l = new Gee.ArrayList<double?> ();
foreach (var child in node) { foreach (var child in node) {
l.add(double.parse(child.data)); obj.sig_set_data (node.name, type, child);
//~ l.add(double.parse(child.data));
} }
obj.set (node.name, l); //~ obj.set (node.name, l);
break; break;
} }
} }
else if (elementType.is_object ()) { else if (elementType.is_object ()) {
var pg = p; //~ var pg = p;
foreach (var child in node) { //~ foreach (var child in node) {
pg.add(Yaml.Builder.from_node (child, elementType)); //~ pg.add(Yaml.Builder.from_node (child, elementType));
} //~ }
} }
} }
else { else {

View File

@ -31,7 +31,7 @@ using GLib;
using Gee; using Gee;
/** /**
* a test class to implements yamlize * Yaml.Object bqse class which can be transform to a Yaml.Node structure
*/ */
public abstract class Pluie.Yaml.Object : GLib.Object public abstract class Pluie.Yaml.Object : GLib.Object
{ {
@ -57,9 +57,12 @@ public abstract class Pluie.Yaml.Object : GLib.Object
{ {
register = new Yaml.Register(); register = new Yaml.Register();
yaml_tag = new Tag (typeof (Pluie.Yaml.Object).name (), "v"); yaml_tag = new Tag (typeof (Pluie.Yaml.Object).name (), "v");
register.add_namespace("Pluie", "Pluie.Yaml", "Gee"); register.add_namespace("Pluie", "Pluie.Yaml");
} }
/**
*
*/
public Object () public Object ()
{ {
this.yaml_construct (); this.yaml_construct ();
@ -70,7 +73,7 @@ public abstract class Pluie.Yaml.Object : GLib.Object
*/ */
public virtual void yaml_construct () public virtual void yaml_construct ()
{ {
Dbg.msg ("Yaml.Object (%s) construct".printf (this.get_type().name ()), Log.LINE, Log.FILE); Dbg.msg ("%s (%s) instantiated".printf (this.yaml_name, this.get_type().name ()), Log.LINE, Log.FILE);
} }
/** /**
@ -78,14 +81,13 @@ public abstract class Pluie.Yaml.Object : GLib.Object
*/ */
public virtual void yaml_init () public virtual void yaml_init ()
{ {
Dbg.msg ("Yaml.Object (%s) init".printf (this.get_type().name ()), Log.LINE, Log.FILE); Dbg.msg ("%s (%s) initialized".printf (this.yaml_name, this.get_type().name ()), Log.LINE, Log.FILE);
} }
/** /**
* *
*/ */
public virtual void populate_from_node(GLib.Type type, Yaml.Node node) public virtual signal void populate_from_node (string name, GLib.Type type, Yaml.Node node) {
{
if (type.is_a (typeof (Yaml.Object))) { if (type.is_a (typeof (Yaml.Object))) {
this.set (node.name, Yaml.Builder.from_node(node, type)); this.set (node.name, Yaml.Builder.from_node(node, type));
} }
@ -94,8 +96,7 @@ public abstract class Pluie.Yaml.Object : GLib.Object
/** /**
* *
*/ */
public virtual Yaml.Node? populate_to_node(GLib.Type type, string name) public virtual signal Yaml.Node? populate_to_node (string name, GLib.Type type, Yaml.Node parent) {
{
Yaml.Node? node = null; Yaml.Node? node = null;
if (type.is_a (typeof (Yaml.Object))) { if (type.is_a (typeof (Yaml.Object))) {
var o = (Yaml.Object) GLib.Object.new (type); var o = (Yaml.Object) GLib.Object.new (type);
@ -104,4 +105,5 @@ public abstract class Pluie.Yaml.Object : GLib.Object
} }
return node; return node;
} }
} }