tmp - remove Yaml.Object dependency for Yaml.Builder

This commit is contained in:
a-sansara 2018-08-22 18:55:25 +02:00
parent 1366bcea29
commit 8e2b1a92d2
7 changed files with 102 additions and 47 deletions

View File

@ -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',

View File

@ -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 ();
}
}

View File

@ -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);

View File

@ -71,6 +71,7 @@ public class Pluie.Yaml.Example : Yaml.Object
{
Yaml.Object.register.add_type (
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<double?> ();
this.type_gee_alobject = new Gee.ArrayList<Yaml.ExampleChild> ();
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,35 +116,46 @@ 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) {
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)) {
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 ());
}
Yaml.GeeBuilder.arraylist_to_node (this.type_gee_al, name, parent);
break;
case "type_gee_alobject" :
foreach (var data in this.type_gee_alobject) {
Yaml.Builder.to_node (data, node, false);
}
this.collection_to_node (this.type_gee_alobject, cast_child_collection, name, parent);
break;
}
}
else {
base.populate_to_node (name, type, parent);
}
return node;
}

View File

@ -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; }

View File

@ -119,12 +119,12 @@ public class Pluie.Yaml.GeeBuilder
case Type.DOUBLE :
//~ .add(double.parse(child.data));
//~ var l = new Gee.ArrayList<double?> ();
var l = new Gee.ArrayList<double?> ();
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;
}
}

View File

@ -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;
}
}