From 3043b5ef887696ac2d295cdedc7dd4eff9e86222 Mon Sep 17 00:00:00 2001 From: a-sansara Date: Thu, 2 Aug 2018 18:57:16 +0200 Subject: [PATCH] continue imports --- samples/yaml-config.vala | 4 +-- samples/yaml-imports.vala | 11 ++++++-- src/vala/Pluie/Yaml.Config.vala | 48 ++++++++++++++++++--------------- src/vala/Pluie/Yaml.Finder.vala | 2 +- 4 files changed, 38 insertions(+), 27 deletions(-) diff --git a/samples/yaml-config.vala b/samples/yaml-config.vala index 7a1f463..bb01f3e 100644 --- a/samples/yaml-config.vala +++ b/samples/yaml-config.vala @@ -35,13 +35,13 @@ int main (string[] args) { Echo.init(false); - var path = "resources/test.yml"; + var path = "resources/config/db.yml"; var done = false; of.title ("Pluie Yaml Library", Pluie.Yaml.VERSION, "a-sansara"); var config = new Yaml.Config (path); - var spath = "ship-to.address.city{0}"; + var spath = "bo.host{0}"; var node = config.get (spath); if ((done = node != null)) { of.action ("retriew node from Yaml.Config", spath); diff --git a/samples/yaml-imports.vala b/samples/yaml-imports.vala index 706077d..0330e76 100644 --- a/samples/yaml-imports.vala +++ b/samples/yaml-imports.vala @@ -42,7 +42,7 @@ int main (string[] args) of.title ("Pluie Yaml Library", Pluie.Yaml.VERSION, "a-sansara"); - Pluie.Yaml.Scanner.DEBUG = true; + Pluie.Yaml.Scanner.DEBUG = false; var config = new Yaml.Config (path); var spath = "^imports"; var node = config.get (spath); @@ -52,8 +52,15 @@ int main (string[] args) of.echo ((node as Yaml.NodeMap).map["^path"].to_string (false)); } of.echo (node.to_string (false)); + spath = "therapy.dbname{0}"; + of.action ("retriew imported node from Yaml.Config", spath); + var inode = config.get (spath); + if ((done = node != null)) { + of.echo (inode.to_string (false)); + } } - + var root = config.root_node (); + root.display_childs (); of.rs (done); of.echo (); diff --git a/src/vala/Pluie/Yaml.Config.vala b/src/vala/Pluie/Yaml.Config.vala index c4ac5da..7ad2d9b 100644 --- a/src/vala/Pluie/Yaml.Config.vala +++ b/src/vala/Pluie/Yaml.Config.vala @@ -7,12 +7,12 @@ public class Pluie.Yaml.Config /** * current path */ - public string? path { get; internal set; default = null; } + public string? path { get; internal set; default = null; } /** * Yaml Loader */ - public Yaml.Loader loader { internal get; internal set; } + public Yaml.Loader loader { internal get; internal set; } /** * Yaml Finder @@ -37,7 +37,7 @@ public class Pluie.Yaml.Config Yaml.BaseNode.mode = mode; this.path = path; if (this.path != null) { - this.loader = new Yaml.Loader (this.path, true, true); + this.loader = new Yaml.Loader (this.path, false, false); this.finder = new Yaml.Finder(this.loader.get_nodes ()); this.get_imports (); } @@ -55,12 +55,18 @@ public class Pluie.Yaml.Config return node; } + public Yaml.NodeRoot root_node () + { + return this.finder.context as Yaml.NodeRoot; + } + /** * find node matching specifiyed keyPath */ public void get_imports () { var node = this.get("^imports") as Yaml.NodeMap; + var root = node.parent as Yaml.NodeRoot; if (node != null) { this.get_imports_var(node); var dir = this.strip_path(Path.get_dirname (this.path)); @@ -70,8 +76,19 @@ public class Pluie.Yaml.Config dir = Path.is_absolute(p) ? p : Path.build_filename(dir, p); } } - of.keyval ("import path", dir); this.update_var (node, dir); + foreach(var entry in this.paths.entries) { + var config = new Yaml.Config(entry.value); + Yaml.NodeMap sub = config.loader.get_nodes (); + Yaml.NodeMap n = new Yaml.NodeMap (root, 0, entry.key); + + foreach(var subnode in sub.map.values) { + subnode.parent = null; + + n.add(subnode); + } + root.add (n); + } } } @@ -84,30 +101,20 @@ public class Pluie.Yaml.Config string? file = null; foreach (var entry in node.map.entries) { if (entry.key[0] != IMPORTS_SPE) { - message (entry.key); var val = entry.value.val (); - message ("%s = %s", entry.key, val); if (!Path.is_absolute (val)) { val = Path.build_filename(path, val); - message ("new relative %s", val); } - message (" == update var == "); + // update var foreach (var v in this.varmap.entries) { if (v.key != "path") { - message ("-- var %s", v.key); entry.value.data = val.replace ("^%s^".printf (v.key), v.value); - node.map[entry.key] = entry.value; - of.echo ("%s : %s".printf (entry.key, node.map[entry.key].val () )); - + this.paths[entry.key] = entry.value.data; of.keyval (entry.key, entry.value.data); -//~ this.paths[entry.key] = entry.value.data; } } } } - foreach (var entry in this.paths.entries) { - of.keyval (entry.key, entry.value); - } } /** @@ -122,14 +129,11 @@ public class Pluie.Yaml.Config this.varmap.set (entry.key.substring (1), entry.value.val ()); } } - of.echo (""); - foreach (var entry in this.varmap.entries) { - of.keyval (entry.key, entry.value); - } - of.echo (""); - of.echo (""); } + /** + * + */ private string? strip_path(string? path) { string? s = path; diff --git a/src/vala/Pluie/Yaml.Finder.vala b/src/vala/Pluie/Yaml.Finder.vala index ad7fe11..80625f6 100644 --- a/src/vala/Pluie/Yaml.Finder.vala +++ b/src/vala/Pluie/Yaml.Finder.vala @@ -40,7 +40,7 @@ public class Pluie.Yaml.Finder : Object /** * Context node */ - private Yaml.Node? context { get; internal set; } + public Yaml.Node? context { get; internal set; } /** * default Yaml.Node constructor