From 8e2b1a92d2cc81f85e65d6f80375bda24a4f7c49 Mon Sep 17 00:00:00 2001 From: a-sansara Date: Wed, 22 Aug 2018 18:55:25 +0200 Subject: [PATCH] tmp - remove Yaml.Object dependency for Yaml.Builder --- meson.build | 1 + samples/yaml-test.vala | 10 ++++- src/vala/Pluie/Yaml.Builder.vala | 37 +++++++++++------ src/vala/Pluie/Yaml.Example.vala | 58 ++++++++++++++++----------- src/vala/Pluie/Yaml.ExampleChild.vala | 2 +- src/vala/Pluie/Yaml.GeeBuilder.vala | 8 ++-- src/vala/Pluie/Yaml.Object.vala | 33 +++++++++++++-- 7 files changed, 102 insertions(+), 47 deletions(-) diff --git a/meson.build b/meson.build index 78fee6b..bec3f40 100644 --- a/meson.build +++ b/meson.build @@ -73,6 +73,7 @@ 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/samples/yaml-test.vala b/samples/yaml-test.vala index 6136fa6..48d8b25 100644 --- a/samples/yaml-test.vala +++ b/samples/yaml-test.vala @@ -61,14 +61,20 @@ int main (string[] args) 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 ())); + of.keyval("type_gee_al", "(%s<%s>)" .printf(obj.type_gee_al.get_type ().name (), obj.type_gee_al.element_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 ())); + of.keyval("type_object", "(%s)" .printf(obj.type_object.get_type ().name ())); + of.echo(" - item toto : %s".printf (obj.type_object.toto)); + of.echo(" - item tata : %s".printf (obj.type_object.tata)); + obj.type_object.method_a (); + + of.keyval("type_gee_alobject", "(%s<%s>)" .printf(obj.type_gee_alobject.get_type ().name (), obj.type_gee_alobject.element_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)); + child.method_a (); } } diff --git a/src/vala/Pluie/Yaml.Builder.vala b/src/vala/Pluie/Yaml.Builder.vala index 1d186ac..39d5637 100644 --- a/src/vala/Pluie/Yaml.Builder.vala +++ b/src/vala/Pluie/Yaml.Builder.vala @@ -122,17 +122,19 @@ public class Pluie.Yaml.Builder /** * */ - public static Yaml.Object? from_node (Yaml.Node node, Type otype = GLib.Type.INVALID) + public static GLib.Object? from_node (Yaml.Node node, Type otype = GLib.Type.INVALID) { - Yaml.Object? obj = null; + GLib.Object? obj = null; try { Type type = node.tag != null ? type_from_tag (node.tag.value) : otype; if (type != Type.INVALID) { Yaml.dbg_action ("vala type founded", "%s (%s)".printf (type.name (), type.to_string ())); if (type.is_object ()) { - obj = (Yaml.Object) GLib.Object.new (type); - obj.set ("yaml_name", node.name); - obj.yaml_construct (); + obj = GLib.Object.new (type); + if (type.is_a (typeof (Yaml.Object))) { + (obj as Yaml.Object).set ("yaml_name", node.name); + (obj as Yaml.Object).yaml_construct (); + } if (node!= null && !node.empty ()) { GLib.ParamSpec? def = null; Yaml.Node? scalar = null; @@ -157,7 +159,9 @@ public class Pluie.Yaml.Builder of.warn ("searched type not found : %s".printf (type.name ())); } else { - obj.yaml_init (); + if (type.is_a (typeof (Yaml.Object))) { + (obj as Yaml.Object).yaml_init (); + } } } catch (GLib.Error e) { @@ -169,21 +173,21 @@ public class Pluie.Yaml.Builder /** * */ - public static void set_from_collection (ref Yaml.Object obj, Type parentType, Yaml.Node node, Type type) + public static void set_from_collection (ref GLib.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)) || Yaml.Object.register.is_registered_type (parentType, type)) { - obj.populate_from_node (node.name, type, node); + (obj as Yaml.Object).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); + of.error ("%s is not registered and cannot be populated".printf (type.name ())); } } /** * */ - public static void set_from_scalar (ref Yaml.Object obj, string name, GLib.Type type, string data) + public static void set_from_scalar (ref GLib.Object obj, string name, GLib.Type type, string data) { GLib.Value v = GLib.Value(type); Yaml.dbg_action ("Auto setting property value %s".printf (of.c (ECHO.MICROTIME).s (type.name ())), name); @@ -318,15 +322,22 @@ public class Pluie.Yaml.Builder /** * */ - public static Yaml.Node to_node (Yaml.Object obj, Yaml.Node? parent = null, bool root = true) + public static Yaml.Node to_node (GLib.Object obj, Yaml.Node? parent = null, bool root = true, int? index = null, string? property_name = null) { - var node = new Yaml.Mapping (parent, obj.yaml_name); + string node_name = ""; + if (obj.get_type ().is_a (typeof (Yaml.Object))) { + node_name = (obj as Yaml.Object).yaml_name; + } + else { + node_name = parent.ntype.is_sequence () && index != null ? "_%d".printf (index+1) : (property_name != null ? property_name : obj.get_type ().name ()); + } + var node = new Yaml.Mapping (parent, node_name); string? name = null; 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 (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); + var child = (obj as Yaml.Object).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 3bae2a6..d08f5a5 100644 --- a/src/vala/Pluie/Yaml.Example.vala +++ b/src/vala/Pluie/Yaml.Example.vala @@ -70,8 +70,9 @@ public class Pluie.Yaml.Example : Yaml.Object static construct { Yaml.Object.register.add_type ( - typeof (Yaml.Example), - typeof (Yaml.ExampleStruct), + typeof (Yaml.Example), + typeof (Yaml.ExampleChild), + typeof (Yaml.ExampleStruct), typeof (Gee.ArrayList) ); } @@ -83,7 +84,7 @@ public class Pluie.Yaml.Example : Yaml.Object { this.type_gee_al = new Gee.ArrayList (); this.type_gee_alobject = new Gee.ArrayList (); - register.add_namespace("Gee"); + register.add_namespace("Gee", "GLib"); Dbg.msg ("%s (%s) instantiated".printf (this.yaml_name, this.get_type().name ()), Log.LINE, Log.FILE); } @@ -115,36 +116,47 @@ public class Pluie.Yaml.Example : Yaml.Object } } else { - base.populate_from_node (name, type, node); + var obj = Yaml.Builder.from_node(node, type); + if (name == "type_object") { + this.set (node.name, (Yaml.ExampleChild) obj); + } + else { + this.set (node.name, obj); + } } } + /** + * match delegate CastYamlObject + */ + public GLib.Object cast_child_collection (string name, GLib.Object obj) + { + return (GLib.Object) ((Yaml.ExampleChild) obj); + } + /** * */ 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); - } + Yaml.Node? 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)) { + switch (name) { + case "type_gee_al" : + Yaml.GeeBuilder.arraylist_to_node (this.type_gee_al, name, parent); break; - } + case "type_gee_alobject" : + this.collection_to_node (this.type_gee_alobject, cast_child_collection, name, parent); + + break; } } + else { + base.populate_to_node (name, type, parent); + } return node; } } diff --git a/src/vala/Pluie/Yaml.ExampleChild.vala b/src/vala/Pluie/Yaml.ExampleChild.vala index 5bd41b1..ca39ed6 100644 --- a/src/vala/Pluie/Yaml.ExampleChild.vala +++ b/src/vala/Pluie/Yaml.ExampleChild.vala @@ -31,7 +31,7 @@ /** * a test class to implements yamlize */ -public class Pluie.Yaml.ExampleChild : Yaml.Object +public class Pluie.Yaml.ExampleChild : GLib.Object { public string toto { get; set; } public string tata { get; set; } diff --git a/src/vala/Pluie/Yaml.GeeBuilder.vala b/src/vala/Pluie/Yaml.GeeBuilder.vala index 096b44a..d3b316c 100644 --- a/src/vala/Pluie/Yaml.GeeBuilder.vala +++ b/src/vala/Pluie/Yaml.GeeBuilder.vala @@ -119,12 +119,12 @@ public class Pluie.Yaml.GeeBuilder case Type.DOUBLE : //~ .add(double.parse(child.data)); -//~ var l = new Gee.ArrayList (); + var l = new Gee.ArrayList (); foreach (var child in node) { - obj.sig_set_data (node.name, type, child); -//~ 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; } } diff --git a/src/vala/Pluie/Yaml.Object.vala b/src/vala/Pluie/Yaml.Object.vala index e5d308d..24c0ec5 100644 --- a/src/vala/Pluie/Yaml.Object.vala +++ b/src/vala/Pluie/Yaml.Object.vala @@ -88,9 +88,13 @@ public abstract class Pluie.Yaml.Object : GLib.Object * */ 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)); } + else { + message ("base Yaml.Object : %s".printf (Log.METHOD)); + this.set (node.name, (GLib.Object) Yaml.Builder.from_node(node, type)); + } } /** @@ -98,12 +102,33 @@ public abstract class Pluie.Yaml.Object : GLib.Object */ 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); + if (type.is_object ()) { + var o = (GLib.Object) GLib.Object.new (type); this.get (name, out o); - node = Yaml.Builder.to_node (o, null, false); + node = Yaml.Builder.to_node (o, parent, false, null, name); } return node; } + public delegate GLib.Object CastYamlObject (string name, GLib.Object obj); + + /** + * + */ + public virtual Yaml.Node? collection_to_node (Gee.Collection list, CastYamlObject castYamlObject, string name, Yaml.Node? parent = null) + { + var node = new Yaml.Sequence (parent, name); + node.tag = new Yaml.Tag (Yaml.Object.register.resolve_namespace_type(list.get_type ()), "v"); + var it = list.iterator (); + var i = 0; + while (it.next ()) { + Yaml.Builder.to_node ( + castYamlObject(name, (GLib.Object) it.get ()), + node, + false, + i++ + ); + } + return node; + } }