diff --git a/meson.build b/meson.build index bec3f40..78fee6b 100644 --- a/meson.build +++ b/meson.build @@ -73,7 +73,6 @@ sources = [ 'src/vala/Pluie/Yaml.ExampleChild.vala', 'src/vala/Pluie/Yaml.ExampleStruct.vala', 'src/vala/Pluie/Yaml.Finder.vala', - 'src/vala/Pluie/Yaml.GeeBuilder.vala', 'src/vala/Pluie/Yaml.Loader.vala', 'src/vala/Pluie/Yaml.Mapping.vala', 'src/vala/Pluie/Yaml.Node.vala', diff --git a/resources/tag.yml b/resources/tag.yml index d00f10f..39dba7d 100644 --- a/resources/tag.yml +++ b/resources/tag.yml @@ -44,19 +44,3 @@ titi : 44 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 diff --git a/samples/yaml-test.vala b/samples/yaml-test.vala new file mode 100644 index 0000000..6136fa6 --- /dev/null +++ b/samples/yaml-test.vala @@ -0,0 +1,99 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * + * @software : pluie-yaml + * @version : 0.5 + * @type : library + * @date : 2018 + * @licence : GPLv3.0 + * @author : a-Sansara <[dev]at[pluie]dot[org]> + * @copyright : 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 . + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ + +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; + +} diff --git a/src/vala/Pluie/Yaml.Builder.vala b/src/vala/Pluie/Yaml.Builder.vala index c0b6e2e..1d186ac 100644 --- a/src/vala/Pluie/Yaml.Builder.vala +++ b/src/vala/Pluie/Yaml.Builder.vala @@ -48,7 +48,7 @@ public class Pluie.Yaml.Builder private static unowned GLib.Module p_open_module () { if (p_module == null) { - p_module = GLib.Module.open (null, 0); + p_module = Module.open (null, 0); } return p_module; } @@ -58,7 +58,7 @@ public class Pluie.Yaml.Builder */ 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; } @@ -110,7 +110,7 @@ public class Pluie.Yaml.Builder } } } - catch (GLib.RegexError e) { + catch (RegexError e) { of.error (e.message, true); } 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 ())); - if (type.is_a (typeof (Yaml.Object))) { - obj.set (node.name, Yaml.Builder.from_node(node, type)); - } - 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); + if (type.is_a (typeof (Yaml.Object)) || Yaml.Object.register.is_registered_type (parentType, type)) { + obj.populate_from_node (node.name, type, node); } else { 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 ()){ name = Yaml.Builder.transform_param_name(def.name); if (name != null && name != "yaml_name") { - if (def.value_type.is_a (typeof (Gee.ArrayList))) { - void *p; - 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 (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(name, def.value_type, node); if (child != null) { child.tag = new Yaml.Tag (Yaml.Object.register.resolve_namespace_type(def.value_type), "v"); node.add (child); diff --git a/src/vala/Pluie/Yaml.Example.vala b/src/vala/Pluie/Yaml.Example.vala index 9e9f9ec..3bae2a6 100644 --- a/src/vala/Pluie/Yaml.Example.vala +++ b/src/vala/Pluie/Yaml.Example.vala @@ -81,10 +81,10 @@ public class Pluie.Yaml.Example : Yaml.Object */ protected override void yaml_construct () { - this.type_gee_al = new Gee.ArrayList (); + this.type_gee_al = new Gee.ArrayList (); this.type_gee_alobject = new Gee.ArrayList (); - // base.yaml_init (); - Dbg.msg ("Yaml.Object %s (%s) instantiated".printf (this.yaml_name, this.get_type().name ()), Log.LINE, Log.FILE); + register.add_namespace("Gee"); + 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 () { - // base.yaml_init (); - Dbg.msg ("Yaml.Object %s (%s) initialized".printf (this.yaml_name, 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 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)) { 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) - { - Yaml.Node? node = base.populate_to_node (type, 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); if (node == null) { if (type == typeof (Yaml.ExampleStruct)) { Yaml.ExampleStruct p = this.type_struct; 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; } diff --git a/src/vala/Pluie/Yaml.GeeBuilder.vala b/src/vala/Pluie/Yaml.GeeBuilder.vala index 41c0eef..096b44a 100644 --- a/src/vala/Pluie/Yaml.GeeBuilder.vala +++ b/src/vala/Pluie/Yaml.GeeBuilder.vala @@ -117,19 +117,22 @@ public class Pluie.Yaml.GeeBuilder obj.set (node.name, l); break; case Type.DOUBLE : - var l = new Gee.ArrayList (); + +//~ .add(double.parse(child.data)); +//~ var l = new Gee.ArrayList (); 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; } } else if (elementType.is_object ()) { - var pg = p; - foreach (var child in node) { - pg.add(Yaml.Builder.from_node (child, elementType)); - } +//~ var pg = p; +//~ foreach (var child in node) { +//~ pg.add(Yaml.Builder.from_node (child, elementType)); +//~ } } } else { diff --git a/src/vala/Pluie/Yaml.Object.vala b/src/vala/Pluie/Yaml.Object.vala index ef31f63..e5d308d 100644 --- a/src/vala/Pluie/Yaml.Object.vala +++ b/src/vala/Pluie/Yaml.Object.vala @@ -31,7 +31,7 @@ using GLib; 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 { @@ -48,7 +48,7 @@ public abstract class Pluie.Yaml.Object : GLib.Object /** * */ - public static Yaml.Tag yaml_tag { get; internal set; } + public static Yaml.Tag yaml_tag { get; internal set; } /** * @@ -57,9 +57,12 @@ public abstract class Pluie.Yaml.Object : GLib.Object { register = new Yaml.Register(); 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 () { this.yaml_construct (); @@ -70,7 +73,7 @@ public abstract class Pluie.Yaml.Object : GLib.Object */ 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 () { - 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))) { 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; if (type.is_a (typeof (Yaml.Object))) { var o = (Yaml.Object) GLib.Object.new (type); @@ -104,4 +105,5 @@ public abstract class Pluie.Yaml.Object : GLib.Object } return node; } + }