From 4ac9c387b4c66d5ef8433156474fc6bb4e108620 Mon Sep 17 00:00:00 2001 From: a-sansara Date: Mon, 20 Aug 2018 18:45:50 +0200 Subject: [PATCH] tmp --- resources/tag.yml | 8 +-- samples/yaml-tonode.vala | 71 +++++++++++++++++++++++++- src/vala/Pluie/Yaml.Builder.vala | 58 ++++++++++++++++++++- src/vala/Pluie/Yaml.Example.vala | 19 +++---- src/vala/Pluie/Yaml.ExampleStruct.vala | 24 +++++---- 5 files changed, 152 insertions(+), 28 deletions(-) diff --git a/resources/tag.yml b/resources/tag.yml index 4a0e529..dabc005 100644 --- a/resources/tag.yml +++ b/resources/tag.yml @@ -22,10 +22,10 @@ green : !v!uint8 78 blue : 153 !v!Gee.ArrayList type_gee_al : - - ab_1 - - ab_2 - - ab_3 - - ab_4 + - 1.2 + - 2.2 + - 3.2 + - 4.2 !v!Pluie.Yaml.Example test2 : myname : test2object diff --git a/samples/yaml-tonode.vala b/samples/yaml-tonode.vala index 5defbbb..3032e24 100644 --- a/samples/yaml-tonode.vala +++ b/samples/yaml-tonode.vala @@ -32,6 +32,57 @@ using GLib; using Gee; using Pluie; +public void inspect_type (GLib.Type type, ...) +{ + var l = va_list(); + while (true) { + var obj = l.arg (); + if (obj == null) { + break; // end of the list + } + print ("%s\n", type.name ()); + print (" is-obj: %s\n", type.is_object ().to_string ()); + print (" is-abstr: %s\n", type.is_abstract ().to_string ()); + print (" is-classed: %s\n", type.is_classed ().to_string ()); + print (" is-derivable: %s\n", type.is_derivable ().to_string ()); + print (" is-derived: %s\n", type.is_derived ().to_string ()); + print (" is-fund: %s\n", type.is_fundamental ().to_string ()); + print (" is-inst: %s\n", type.is_instantiatable ().to_string ()); + print (" is-iface: %s\n", type.is_interface ().to_string ()); + print (" is-enum: %s\n", type.is_enum ().to_string ()); + print (" is-flags: %s\n", type.is_object ().to_string ()); + + // Output: + // `` Children:`` + print (" Children:\n"); + foreach (unowned Type ch in type.children ()) { + print (" - %s\n", ch.name ()); + } + + // `` Interfaces:`` + // `` - Interface`` + print (" Interfaces:\n"); + foreach (unowned Type ch in type.interfaces ()) { + if ( ch == typeof(Gee.Traversable)) { + print (" --- !!! element type is %s\n", (obj as Gee.Traversable).element_type.name ()); + if ((obj as Gee.Traversable).element_type == typeof (Gee.Map.Entry)) { + print (" --- !!! key type is %s\n", (obj as Gee.Map).key_type.name ()); + print (" --- !!! value type is %s\n", (obj as Gee.Map).value_type.name ()); + } + } + print (" - %s\n", ch.name ()); + } + + // Output: + // `` Parents:`` + // `` - GObject`` + print (" Parents:\n"); + for (Type p = type.parent (); p != 0 ; p = p.parent ()) { + print (" - %s\n", p.name ()); + } + } +} + int main (string[] args) { Echo.init(false); @@ -42,14 +93,32 @@ int main (string[] args) 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 (); + var root = config.root_node () as Yaml.Root; root.display_childs (); of.action ("Yaml.Builder.from_node", root.first ().name); var 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 (double v in obj.type_gee_al as Gee.ArrayList) { + of.echo(" - item : %f".printf (v)); + } + } + + +//~ double dv = 46548970.54324546464; +//~ var d = new Gee.ArrayList (); +//~ d.add(dv); + +//~ Gee.ArrayList* z = d; +//~ of.action ("!!!!!!!Yaml.Builder.to_node", obj.get_type ().name ()); +//~ var m = Yaml.Builder.gee_arraylist_to_node (z, "toto", root); of.action ("Yaml.Builder.to_node", obj.get_type ().name ()); + +//~ inspect_type (root.tag_directives.get_type (), root.tag_directives); + var n = Yaml.Builder.to_node (obj); if ((done = n !=null)) { n.display_childs (); diff --git a/src/vala/Pluie/Yaml.Builder.vala b/src/vala/Pluie/Yaml.Builder.vala index 1d5e7c1..fe62e18 100644 --- a/src/vala/Pluie/Yaml.Builder.vala +++ b/src/vala/Pluie/Yaml.Builder.vala @@ -327,7 +327,12 @@ 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 (Yaml.Object)) || Yaml.Object.register.is_registered_type(obj.get_type (), def.value_type)) { + if (def.value_type.is_a (typeof (Gee.ArrayList))) { + void *p; + obj.get(name, out p); + Yaml.Builder.gee_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) { child.tag = new Yaml.Tag (Yaml.Object.register.resolve_namespace_type(def.value_type), "v"); @@ -362,4 +367,55 @@ public class Pluie.Yaml.Builder } else return node; } + + /** + * + */ + public static Yaml.Node? gee_arraylist_to_node (Gee.ArrayList* o, string property_name, Yaml.Node parent) + { + Yaml.dbg_action ("prop %s (type %s) has element type :".printf (property_name, o->get_type ().name ()), o->element_type.name ()); + var type = o->element_type; + var node = new Yaml.Sequence (parent, property_name); + var it = o->iterator(); + while (it.next ()) { + if (!o->element_type.is_object () && o->element_type.is_fundamental ()) { + string data = ""; + switch (o->element_type) { + case Type.INT64 : + case Type.INT : + data = ((int64) it.get ()).to_string (); + break; + case Type.CHAR : + data = ((char) it.get ()).to_string (); + break; + case Type.UCHAR : + data = ((uchar) it.get ()).to_string (); + break; + case Type.UINT64 : + case Type.UINT : + data = ((uint64) it.get ()).to_string (); + break; + case Type.BOOLEAN : + data = ((bool) it.get ()).to_string (); + break; + case Type.FLOAT : + float* f = (float*) it.get (); + data = f.to_string (); + break; + case Type.DOUBLE : + double* d = (double*) it.get (); + data = d.to_string (); + break; + default : + data = (string) it.get (); + break; + } + new Yaml.Scalar (node, data); + } + else if (o->element_type.is_object ()) { + + } + } + return node; + } } diff --git a/src/vala/Pluie/Yaml.Example.vala b/src/vala/Pluie/Yaml.Example.vala index c0011e8..e5c746d 100644 --- a/src/vala/Pluie/Yaml.Example.vala +++ b/src/vala/Pluie/Yaml.Example.vala @@ -64,7 +64,7 @@ public class Pluie.Yaml.Example : Yaml.Object public Yaml.ExampleChild type_object { get; set; } public Yaml.NODE_TYPE type_enum { get; set; } public Yaml.ExampleStruct type_struct { get; set; } - public Gee.ArrayList type_gee_al { get; set; } + public Gee.ArrayList type_gee_al { get; set; } static construct { @@ -94,10 +94,11 @@ public class Pluie.Yaml.Example : Yaml.Object this.type_struct = ExampleStruct.from_yaml_node (node); } else if (type == typeof (Gee.ArrayList)) { - this.type_gee_al = new Gee.ArrayList (); + this.type_gee_al = new Gee.ArrayList (); if (!node.empty ()) { + this.type_gee_al.clear (); foreach (var child in node) { - this.type_gee_al.add(child.data); + this.type_gee_al.add((double) double.parse (child.data)); } } } @@ -115,16 +116,8 @@ public class Pluie.Yaml.Example : Yaml.Object Yaml.Node? node = base.populate_to_node (type, name); if (node == null) { if (type == typeof (Yaml.ExampleStruct)) { - node = new Yaml.Mapping (null, name); - new Yaml.Mapping.with_scalar (node, "red" , this.type_struct.red.to_string ()); - new Yaml.Mapping.with_scalar (node, "green", this.type_struct.green.to_string ()); - new Yaml.Mapping.with_scalar (node, "blue" , this.type_struct.blue.to_string ()); - } - else if (type == typeof (Gee.ArrayList)) { - node = new Yaml.Sequence (null, name); - foreach (var data in this.type_gee_al) { - new Yaml.Scalar (node, data); - } + Yaml.ExampleStruct p = this.type_struct; + node = ExampleStruct.to_yaml_node (ref p, name); } } return node; diff --git a/src/vala/Pluie/Yaml.ExampleStruct.vala b/src/vala/Pluie/Yaml.ExampleStruct.vala index 291bfd0..8c8586b 100644 --- a/src/vala/Pluie/Yaml.ExampleStruct.vala +++ b/src/vala/Pluie/Yaml.ExampleStruct.vala @@ -54,20 +54,26 @@ public struct Pluie.Yaml.ExampleStruct foreach (var child in node) { var v = child.val (typeof (uint)); switch (child.name) { - case "red" : - s.red = v.get_uint (); - break; - case "green" : - s.green = v.get_uint (); - break; - case "blue" : - s.blue = v.get_uint (); - break; + case "red" : s.red = v.get_uint (); break; + case "green" : s.green = v.get_uint (); break; + case "blue" : s.blue = v.get_uint (); break; } } return s; } + /** + * + */ + public static Yaml.Node to_yaml_node (ref ExampleStruct self, string name) + { + var node = new Yaml.Mapping (null, name); + new Yaml.Mapping.with_scalar (node, "red" , self.red.to_string ()); + new Yaml.Mapping.with_scalar (node, "green", self.green.to_string ()); + new Yaml.Mapping.with_scalar (node, "blue" , self.blue.to_string ()); + return node; + } + /** * */