From 30197dde4239b4f1082453255375eadcfc5c5517 Mon Sep 17 00:00:00 2001 From: a-Sansara Date: Thu, 2 Aug 2018 23:44:36 +0200 Subject: [PATCH] finish ^imports --- README.md | 76 +++++++++++++++++++------ build.sh | 16 +++++- resources/main.yml | 5 +- samples/json-loader.vala | 2 +- samples/yaml-config.vala | 4 +- samples/yaml-finder.vala | 57 ++++++++++--------- samples/yaml-imports.vala | 11 +--- samples/yaml-loader.vala | 2 +- src/vala/Pluie/Yaml.BaseNode.vala | 60 ++++++++++++------- src/vala/Pluie/Yaml.Config.vala | 54 ++++++++++-------- src/vala/Pluie/Yaml.Document.vala | 5 +- src/vala/Pluie/Yaml.Finder.vala | 6 +- src/vala/Pluie/Yaml.Loader.vala | 26 +++++---- src/vala/Pluie/Yaml.Node.vala | 10 +++- src/vala/Pluie/Yaml.NodeMap.vala | 10 ++-- src/vala/Pluie/Yaml.NodeRoot.vala | 5 +- src/vala/Pluie/Yaml.NodeScalar.vala | 7 +-- src/vala/Pluie/Yaml.NodeSequence.vala | 11 ++-- src/vala/Pluie/Yaml.NodeSinglePair.vala | 9 ++- src/vala/Pluie/Yaml.Processor.vala | 38 +++++++------ 20 files changed, 248 insertions(+), 166 deletions(-) diff --git a/README.md b/README.md index 699de4e..98475cb 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,10 @@ As json is now a valid subset of yaml, you can use this lib to load json files too. The purpose of this project is to make vala able to load and deal with yaml configuration files. -So, currently the lib deal only with one yaml document, but an @import clause (nodemap) is plan in order to load a subset of yaml files in the main yaml document. +So, currently the lib deal only with one yaml document, but you can use a special `^imports` clause (nodemap) +to load a subset of yaml files in the main yaml document. +the lib does not manage yet tag directives and tag values. **pluie-yaml** use the ![libyaml c library](https://github.com/yaml/libyaml) (License MIT, many thanks to Kirill Simonov) to parse and retriew related yaml events. ## License @@ -51,9 +53,11 @@ docker run --rm -it pluie/libyaml ## Usage +------------------- + ### config -``` +```vala var config = new Yaml.Config (path); var node = config.get ("ship-to.address.city{0}"); @@ -62,10 +66,39 @@ docker run --rm -it pluie/libyaml } ``` +------------------- + +### config with ^imports clause + +```yml +# | use special key word '^imports' to import other yaml config files in +# | current yaml document +# | '^imports' must be uniq and a direct child of root node +# | imported files are injected as mapping nodes at top document level +# | so you cannot use keys that already exists in the document +^imports : + # you can redefine default import path with the special key '^path' + # if you do not use it, the default path value will be the current directory + # redefined path values are relative to the current directory (if a relative path + # is provided) + ^path : ./config + # you can also define any other var by prefixing key with ^ + ^dir : subdir + # and use it enclosed by ^ + # here final test path will be "./config/subdir/test.yml" + test : ^dir^/test.yml + # here final db path will be "./config/db.yml" + db : db.yml +``` + +------------------- ### loader -``` +load a single document. +`^imports` clause is out of effects here. + +```vala var path = "./config/main.yml"; // uncomment to enable debug // Pluie.Yaml.Scanner.DEBUG = true; @@ -75,48 +108,57 @@ docker run --rm -it pluie/libyaml root.display_childs (); } ``` +------------------- ### finder **lib-yaml** provide a `Yaml.Finder` to easily retriew a particular yaml node. Search path definition has two mode. -The default mode is `Yaml.FIND_MODE.SQUARE_BRACKETS` -- node's key name must be enclosed in square brackets -- sequence entry must be enclosed in curly brace - -ex : `[grandfather][father][son]{2}[age]` - -The Other mode is Yaml.FIND_MODE.DOT +The default mode is `Yaml.FIND_MODE.DOT` - child mapping node are separated by dot - sequence entry must be enclosed in curly brace ex : `grandfather.father.son{2}.age` +The Other mode is Yaml.FIND_MODE.SQUARE_BRACKETS +- node's key name must be enclosed in square brackets +- sequence entry must be enclosed in curly brace + +ex : `[grandfather][father][son]{2}[age]` + with singlepair node, you can retriew corresponding scalar node with {0} -``` -/* -# ex yaml file : +ex yaml file : + +```yml product: - sku : BL394D quantity : 4 description : Basketball -*/ +``` + +vala code : + +```vala ... Yaml.NodeRoot root = loader.get_nodes (); Yaml.Node? node = null; - if ((node = finder.find("product{0}.description{0}")) != null) { - var val = node.data; + if ((node = finder.find ("product{0}.description")) != null) { + var val = node.val (); } ``` +------------------- ### more samples see samples files in ./samples directory +------------------- + ### todo -* import clause +* ~~imports clause~~ * fix nodes traversing * dumper +* manage tag directives & tag diff --git a/build.sh b/build.sh index f973b86..d5e73b1 100755 --- a/build.sh +++ b/build.sh @@ -36,6 +36,7 @@ lib="pluie-yaml-0.3" cok="\033[1;38;5;37m" cko="\033[1;38;5;204m" off="\033[m" +resume= # -------------------------------------------------------- function build.title() { @@ -49,7 +50,11 @@ function build.title() fi s="$cko<" fi - echo -e "\n $s $c1[$c2$1$c1] $state$off" + if [ ! -z $3 ]; then + echo -e " |- $c1[$c2$1$c1] $state$off" + else + echo -e "\n $s $c1[$c2$1$c1] $state$off" + fi } # -------------------------------------------------------- function build.lib() @@ -72,7 +77,10 @@ function build.samples() build.sample "$file" fi done - echo + echo -e "\n RESUME : " + for t in $resume; do + build.title "${t:1}" ${t:0:1} 1 + done } # -------------------------------------------------------- function build.sample() @@ -84,7 +92,9 @@ function build.sample() build.title "$f SAMPLE BUILD" echo -e "\n$cmd" $cmd - build.title "$f SAMPLE BUILD" $? + local done=$? + resume="$resume$done$f " + build.title "$f SAMPLE BUILD" $done } # -------------------------------------------------------- function build.main() diff --git a/resources/main.yml b/resources/main.yml index c17909c..c017057 100644 --- a/resources/main.yml +++ b/resources/main.yml @@ -1,4 +1,3 @@ - project: name : blabla env : local @@ -15,12 +14,12 @@ project: # redefined path values are relative to the current directory (if a relative path # is provided) ^path : ./config - # you can also define any other var by prefixing key with ~ + # you can also define any other var by prefixing key with ^ ^dir : subdir # and use it enclosed by ^ # here final test path will be "./config/subdir/test.yml" test : ^dir^/test.yml # here final db path will be "./config/db.yml" - db : db.yml + db : db.yml bilbo : tato diff --git a/samples/json-loader.vala b/samples/json-loader.vala index 6f01fb9..1a3a090 100644 --- a/samples/json-loader.vala +++ b/samples/json-loader.vala @@ -39,7 +39,7 @@ int main (string[] args) var done = false; of.title ("Pluie Yaml Library", Pluie.Yaml.VERSION, "a-sansara"); - Pluie.Yaml.Scanner.DEBUG = true; + Pluie.Yaml.Scanner.DEBUG = false; var loader = new Yaml.Loader (path, true, true); if ((done = loader.done)) { Yaml.NodeRoot root = loader.get_nodes (); diff --git a/samples/yaml-config.vala b/samples/yaml-config.vala index bb01f3e..89ae625 100644 --- a/samples/yaml-config.vala +++ b/samples/yaml-config.vala @@ -39,8 +39,8 @@ int main (string[] args) var done = false; of.title ("Pluie Yaml Library", Pluie.Yaml.VERSION, "a-sansara"); - - var config = new Yaml.Config (path); + Pluie.Yaml.Scanner.DEBUG = false; + var config = new Yaml.Config (path, true); var spath = "bo.host{0}"; var node = config.get (spath); if ((done = node != null)) { diff --git a/samples/yaml-finder.vala b/samples/yaml-finder.vala index 8f8710d..5e0bf6b 100644 --- a/samples/yaml-finder.vala +++ b/samples/yaml-finder.vala @@ -46,7 +46,35 @@ int main (string[] args) var finder = new Yaml.Finder(root); Yaml.Node? node = null; - var spath = "[product]{0}[description]"; + var spath = "bill-to.family"; + of.action ("Find node", spath); + if ((node = finder.find(spath)) != null) { + of.echo (node.to_string (false)); + of.action ("get scalar value", spath); + of.echo ((node as Yaml.NodeSinglePair).scalar ().data); + + of.action ("get parent node"); + of.echo (node.parent.to_string ()); + + of.action ("get address node"); + if ((node = (node.parent as Yaml.NodeMap).map["address"])!= null) { + of.echo (node.to_string (false)); + (node as Yaml.NodeMap).display_childs (); + + of.action ("Loop throught childs", node.name); + foreach (var child in (node as Yaml.NodeMap).map.values) { + of.echo (child.to_string (false)); + } + } + of.state (node != null); + } + else of.state (node != null); + + of.action ("Set find mode", "SQUARE_BRACKETS"); + Yaml.BaseNode.mode = Yaml.FIND_MODE.SQUARE_BRACKETS; + of.state (true); + + spath = "[product]{0}[description]"; // equivalent in DOT MODE // spath = "product{0}.description"; of.action ("Find node", spath); @@ -80,33 +108,6 @@ int main (string[] args) } else of.state (node != null); - of.action ("Set find mode", "DOT"); - Yaml.BaseNode.mode = Yaml.FIND_MODE.DOT; - of.state (true); - - spath = "bill-to.family"; - of.action ("Find node", spath); - if ((node = finder.find(spath)) != null) { - of.echo (node.to_string (false)); - of.action ("get scalar value", spath); - of.echo ((node as Yaml.NodeSinglePair).scalar ().data); - - of.action ("get parent node"); - of.echo (node.parent.to_string ()); - - of.action ("get address node"); - if ((node = (node.parent as Yaml.NodeMap).map["address"])!= null) { - of.echo (node.to_string (false)); - (node as Yaml.NodeMap).display_childs (); - - of.action ("Loop throught childs", node.name); - foreach (var child in (node as Yaml.NodeMap).map.values) { - of.echo (child.to_string (false)); - } - } - of.state (node != null); - } - else of.state (node != null); } of.rs (done); diff --git a/samples/yaml-imports.vala b/samples/yaml-imports.vala index 0330e76..c2429e4 100644 --- a/samples/yaml-imports.vala +++ b/samples/yaml-imports.vala @@ -43,19 +43,14 @@ int main (string[] args) of.title ("Pluie Yaml Library", Pluie.Yaml.VERSION, "a-sansara"); Pluie.Yaml.Scanner.DEBUG = false; - var config = new Yaml.Config (path); + var config = new Yaml.Config (path, true); var spath = "^imports"; var node = config.get (spath); if ((done = node != null)) { - of.action ("retriew node from Yaml.Config", spath); - if ((node as Yaml.NodeMap).map.has_key ("^path")) { - of.echo ((node as Yaml.NodeMap).map["^path"].to_string (false)); - } - of.echo (node.to_string (false)); - spath = "therapy.dbname{0}"; + spath = "db.therapy.dbname{0}"; of.action ("retriew imported node from Yaml.Config", spath); var inode = config.get (spath); - if ((done = node != null)) { + if ((done = inode != null)) { of.echo (inode.to_string (false)); } } diff --git a/samples/yaml-loader.vala b/samples/yaml-loader.vala index b214606..2ee5a05 100644 --- a/samples/yaml-loader.vala +++ b/samples/yaml-loader.vala @@ -39,7 +39,7 @@ int main (string[] args) var done = false; of.title ("Pluie Yaml Library", Pluie.Yaml.VERSION, "a-sansara"); - Pluie.Yaml.Scanner.DEBUG = true; + Pluie.Yaml.Scanner.DEBUG = false; var loader = new Yaml.Loader (path, true, true); if ((done = loader.done)) { Yaml.NodeRoot root = loader.get_nodes (); diff --git a/src/vala/Pluie/Yaml.BaseNode.vala b/src/vala/Pluie/Yaml.BaseNode.vala index fa04af0..e1934bf 100644 --- a/src/vala/Pluie/Yaml.BaseNode.vala +++ b/src/vala/Pluie/Yaml.BaseNode.vala @@ -46,7 +46,7 @@ public class Pluie.Yaml.BaseNode : Object, Pluie.Yaml.Node /** * find mode related to Yaml.FIND_MODE, default is Yaml.FIND_MODE.SQUARE_BRACKETS */ - public static Yaml.FIND_MODE mode { get; set; default = Yaml.FIND_MODE.SQUARE_BRACKETS; } + public static Yaml.FIND_MODE mode { get; set; default = Yaml.FIND_MODE.DOT; } /** * node type related to Yaml.NODE_TYPE @@ -54,9 +54,9 @@ public class Pluie.Yaml.BaseNode : Object, Pluie.Yaml.Node public Yaml.NODE_TYPE node_type { get; internal set; } /** - * current representation indent + * current representation level */ - public int indent { get; internal set; } + public int level { get; internal set; } /** * parent node @@ -76,33 +76,31 @@ public class Pluie.Yaml.BaseNode : Object, Pluie.Yaml.Node /** * default Yaml.Node constructor * @param parent the parent node - * @param indent the current indentation in node representation string * @param type the NODE_TYPE of Yaml.Node to create */ - public BaseNode (Yaml.Node? parent = null, int indent = 0, NODE_TYPE type = NODE_TYPE.UNDEFINED) + public BaseNode (Yaml.Node? parent = null, NODE_TYPE type = NODE_TYPE.UNDEFINED) { - this.standard (parent, indent, type); + this.standard (parent, type); } /** * constructor for root Yaml.Node */ public BaseNode.root () { - this.standard (null, -4, NODE_TYPE.ROOT); + this.standard (null, NODE_TYPE.ROOT); this.name = "PluieYamlRootNode"; } /** * constructor for standard Yaml.Node * @param parent the parent node - * @param indent the current indentation in node representation string * @param type the NODE_TYPE of Yaml.Node to create */ - internal BaseNode.standard (Yaml.Node? parent = null, int indent = 0, NODE_TYPE type = NODE_TYPE.UNDEFINED) + internal BaseNode.standard (Yaml.Node? parent = null, NODE_TYPE type = NODE_TYPE.UNDEFINED) { this.parent = parent; this.node_type = type; - this.indent = indent; + this.level = parent!=null ? parent.level + 1 : 0; this.uuid = Yaml.uuid (); } @@ -132,7 +130,7 @@ public class Pluie.Yaml.BaseNode : Object, Pluie.Yaml.Node */ public virtual Yaml.Node clone_node (string? name = null) { - return new BaseNode.standard (this.parent, this.indent); + return new BaseNode.standard (this.parent); } /** @@ -226,30 +224,54 @@ public class Pluie.Yaml.BaseNode : Object, Pluie.Yaml.Node return has; } + /** + * + */ + public virtual void update_level() + { + this.level = this.parent != null ? this.parent.level + 1 : 0; + switch (this.node_type) { + case NODE_TYPE.SINGLE_PAIR : + (this as Yaml.NodeSinglePair).scalar ().update_level (); + break; + case NODE_TYPE.ROOT : + case NODE_TYPE.MAPPING : + foreach (var child in (this as Yaml.NodeMap).map.values) { + child.update_level (); + } + break; + case NODE_TYPE.SEQUENCE : + foreach (var child in (this as Yaml.NodeSequence).list) { + child.update_level (); + } + break; + } + } + /** * get a presentation string of current Yaml.Node */ - public string to_string (bool indentFormat = true, bool withParent = false, bool withUuid = false, bool withIndent = false, bool withRefCount = false) + public string to_string (bool indentFormat = true, bool withParent = false, bool withUuid = false, bool withLevel = false, bool withRefCount = false) { - return "%s%s%s%s%s%s%s%s".printf ( - this.node_type.is_root () ? "" : of.s_indent ((int8) (indentFormat ? this.indent : 0)), + this.node_type.is_root () ? "" : of.s_indent ((int8) (indentFormat ? (this.level-1)*4 : 0)), of.c (ECHO.OPTION).s ("["), - of.c (ECHO.OPTION_SEP).s (this.node_type.infos ()), this.name != null && !this.node_type.is_scalar () - ? of.c (ECHO.TIME).s (" %s".printf (this.name)) + ? of.c (ECHO.TIME).s ("%s".printf (this.name)) : ( this.node_type.is_scalar () - ? of.c(ECHO.DATE).s (" %s".printf (this.data)) + ? of.c(ECHO.DATE).s ("%s".printf (this.data)) : "" ), - withRefCount ? of.c (ECHO.COMMAND).s ("[%x]".printf (this.ref_count)) : "", + withRefCount ? of.c (ECHO.COMMAND).s ("[%lu]".printf (this.ref_count)) : "", !withParent || this.parent == null ? "" : of.c (ECHO.SECTION).s (" "+this.parent.name)+( - withIndent ? of.c (ECHO.NUM).s (" "+this.indent.to_string()) : "" + withLevel ? of.c (ECHO.NUM).s (" %d".printf (this.level)) : " " ), + of.c (ECHO.OPTION_SEP).s (" %s".printf(this.node_type.infos ())), withUuid ? of.c (ECHO.COMMENT).s (" %s".printf(this.uuid[0:8]+"...")) : "", +//~ of.c (ECHO.NUM).s ("%d".printf (this.level)), of.c (ECHO.OPTION).s ("]") ); } diff --git a/src/vala/Pluie/Yaml.Config.vala b/src/vala/Pluie/Yaml.Config.vala index 7ad2d9b..e2ed9f1 100644 --- a/src/vala/Pluie/Yaml.Config.vala +++ b/src/vala/Pluie/Yaml.Config.vala @@ -32,14 +32,17 @@ public class Pluie.Yaml.Config /** * construct a Yaml Config for specifiyed path */ - public Config (string? path = null, Yaml.FIND_MODE mode = Yaml.FIND_MODE.DOT) + public Config (string? path = null, bool displayFile = false, Yaml.FIND_MODE mode = Yaml.FIND_MODE.DOT) { Yaml.BaseNode.mode = mode; this.path = path; if (this.path != null) { - this.loader = new Yaml.Loader (this.path, false, false); - this.finder = new Yaml.Finder(this.loader.get_nodes ()); - this.get_imports (); + this.loader = new Yaml.Loader (this.path, displayFile, false); + Yaml.NodeRoot? root = this.loader.get_nodes (); + if (root != null) { + this.finder = new Yaml.Finder(root); + this.get_imports (); + } } } @@ -55,6 +58,9 @@ public class Pluie.Yaml.Config return node; } + /** + * + */ public Yaml.NodeRoot root_node () { return this.finder.context as Yaml.NodeRoot; @@ -66,28 +72,30 @@ public class Pluie.Yaml.Config 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)); - if (this.varmap.has_key ("path")) { - var p = this.strip_path(this.varmap["path"]); - if (p != null) { - dir = Path.is_absolute(p) ? p : Path.build_filename(dir, p); + var root = node.parent as Yaml.NodeRoot; + if (root != null && root.node_type.is_root ()) { + this.get_imports_var(node); + var dir = this.strip_path(Path.get_dirname (this.path)); + if (this.varmap.has_key ("path")) { + var p = this.strip_path(this.varmap["path"]); + if (p != null) { + dir = Path.is_absolute(p) ? p : Path.build_filename(dir, p); + } } - } - 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); + 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, entry.key); + foreach(var subnode in sub.map.values) { + subnode.parent = null; + n.add(subnode); + } + root.add (n); } - root.add (n); + root.update_level(); } } } diff --git a/src/vala/Pluie/Yaml.Document.vala b/src/vala/Pluie/Yaml.Document.vala index 9fb958c..973793b 100644 --- a/src/vala/Pluie/Yaml.Document.vala +++ b/src/vala/Pluie/Yaml.Document.vala @@ -37,13 +37,12 @@ public class Pluie.Yaml.Document : Yaml.NodeMap /** * construct a single/pair mapping node * @param parent the parent node - * @param indent the current indentation in node representation string * @param name the current name (key) of sequence node * @param data the current scalar data */ - public Document (Yaml.Node? parent = null, int indent = 0, string? name = null, string? data = null) + public Document (Yaml.Node? parent = null, string? name = null, string? data = null) { - this.standard (null, -4, NODE_TYPE.ROOT); + this.standard (null, NODE_TYPE.ROOT); this.name = "PluieYamlRootNode"; } } diff --git a/src/vala/Pluie/Yaml.Finder.vala b/src/vala/Pluie/Yaml.Finder.vala index 80625f6..7c7b015 100644 --- a/src/vala/Pluie/Yaml.Finder.vala +++ b/src/vala/Pluie/Yaml.Finder.vala @@ -43,10 +43,8 @@ public class Pluie.Yaml.Finder : Object public Yaml.Node? context { get; internal set; } /** - * default Yaml.Node constructor - * @param parent the parent node - * @param indent the current indentation in node representation string - * @param type the NODE_TYPE of Yaml.Node to create + * default Yaml.Finder constructor + * @param default Yaml.Node context */ public Finder (Yaml.Node context) { diff --git a/src/vala/Pluie/Yaml.Loader.vala b/src/vala/Pluie/Yaml.Loader.vala index c1df1bd..c9abfab 100644 --- a/src/vala/Pluie/Yaml.Loader.vala +++ b/src/vala/Pluie/Yaml.Loader.vala @@ -64,8 +64,11 @@ public class Pluie.Yaml.Loader } this.scanner = new Yaml.Scanner (path); if ((this.done = this.scanner.run()) && displayNode) { - this.get_nodes ().display_childs (); - of.state(true); + var n = this.get_nodes (); + if (n != null) { + n.display_childs (); + } + of.state(n != null); } if (!this.done) { var evt = this.scanner.get_error_event (); @@ -91,7 +94,7 @@ public class Pluie.Yaml.Loader of.echo (); this.reader.rewind(new Io.StreamLineMark(0, 0)); int line = 0; - string? data = null;; + string? data = null; while (this.reader.readable) { line = this.reader.line + 1; data = this.reader.read (); @@ -105,13 +108,16 @@ public class Pluie.Yaml.Loader )); } } - of.echo ("%s%s%s".printf ( - of.c (ECHO.MICROTIME ).s (" %03d ".printf (line)), - of.c (ECHO.DATE).s ("| "), - errorLine > 0 && line == errorLine - ? of.c (ECHO.FAIL).s (data) - : of.c (ECHO.COMMAND).s (data) - ), errorLine == 0 || line < errorLine); + if (data !=null) { + ECHO color = data.strip()[0] != '#' ? ECHO.COMMAND : ECHO.COMMENT; + of.echo ("%s%s%s".printf ( + of.c (ECHO.MICROTIME ).s (" %03d ".printf (line)), + of.c (ECHO.DATE).s ("| "), + errorLine > 0 && line == errorLine + ? of.c (ECHO.FAIL).s (data) + : of.c (color).s (data) + ), errorLine == 0 || line < errorLine); + } if (errorLine > 0 && line == errorLine) { int len = of.term_width - data.length - 13; stdout.printf (of.c (ECHO.FAIL).s (@" %$(len)s ".printf (" "))); diff --git a/src/vala/Pluie/Yaml.Node.vala b/src/vala/Pluie/Yaml.Node.vala index 1dcea4e..154d291 100644 --- a/src/vala/Pluie/Yaml.Node.vala +++ b/src/vala/Pluie/Yaml.Node.vala @@ -42,7 +42,7 @@ public interface Pluie.Yaml.Node : Object */ public abstract Yaml.NODE_TYPE node_type { get; internal set; } - public abstract int indent { get; internal set; } + public abstract int level { get; internal set; } /** * parent node @@ -109,9 +109,15 @@ public interface Pluie.Yaml.Node : Object */ public abstract Yaml.Node? get_root_node (); + /** + * update node level and all childs level + */ + public abstract void update_level (); + + /** * get a presentation string of current Yaml.Node */ - public abstract string to_string (bool indentFormat = true, bool withParent = false, bool withUuid = true, bool withIndent = false, bool withRefCount = true); + public abstract string to_string (bool indentFormat = true, bool withParent = false, bool withUuid = false, bool withIndent = true, bool withRefCount = false); } diff --git a/src/vala/Pluie/Yaml.NodeMap.vala b/src/vala/Pluie/Yaml.NodeMap.vala index 2026f2c..b6e23c7 100644 --- a/src/vala/Pluie/Yaml.NodeMap.vala +++ b/src/vala/Pluie/Yaml.NodeMap.vala @@ -44,12 +44,11 @@ public class Pluie.Yaml.NodeMap : Yaml.BaseNode, Yaml.NodeCollection /** * construct a mapping node * @param parent the parent node - * @param indent the current indentation in node representation string * @param name the current name (key) of mapping node */ - public NodeMap (Yaml.Node? parent = null, int indent = 0, string? name = null) + public NodeMap (Yaml.Node? parent = null, string? name = null) { - base (parent, indent, NODE_TYPE.MAPPING); + base (parent, NODE_TYPE.MAPPING); this.map = new HashMap (); this.name = name; } @@ -74,7 +73,7 @@ public class Pluie.Yaml.NodeMap : Yaml.BaseNode, Yaml.NodeCollection public override bool add (Yaml.Node node) { node.on_change_parent (); - node.indent = this.indent + 4; + node.level = this.level + 1; node.parent = this; if (this.map == null) { this.map = new HashMap (); @@ -182,7 +181,7 @@ public class Pluie.Yaml.NodeMap : Yaml.BaseNode, Yaml.NodeCollection public override Yaml.Node clone_node (string? name = null) { var key = name != null ? name : this.name; - Yaml.Node clone = new Yaml.NodeMap (this.parent, this.indent, key); + Yaml.Node clone = new Yaml.NodeMap (this.parent, key); foreach (string k in this.map.keys) { var n = this.map.get(k).clone_node(); n.parent = clone; @@ -190,5 +189,4 @@ public class Pluie.Yaml.NodeMap : Yaml.BaseNode, Yaml.NodeCollection } return clone; } - } diff --git a/src/vala/Pluie/Yaml.NodeRoot.vala b/src/vala/Pluie/Yaml.NodeRoot.vala index 0b5fb70..461f117 100644 --- a/src/vala/Pluie/Yaml.NodeRoot.vala +++ b/src/vala/Pluie/Yaml.NodeRoot.vala @@ -37,13 +37,12 @@ public class Pluie.Yaml.NodeRoot : Yaml.NodeMap /** * construct a single/pair mapping node * @param parent the parent node - * @param indent the current indentation in node representation string * @param name the current name (key) of sequence node * @param data the current scalar data */ - public NodeRoot (Yaml.Node? parent = null, int indent = 0, string? name = null, string? data = null) + public NodeRoot (Yaml.Node? parent = null, string? name = null, string? data = null) { - this.standard (null, -4, NODE_TYPE.ROOT); + this.standard (null, NODE_TYPE.ROOT); this.name = "PluieYamlRootNode"; } } diff --git a/src/vala/Pluie/Yaml.NodeScalar.vala b/src/vala/Pluie/Yaml.NodeScalar.vala index 8117327..07b7d1c 100644 --- a/src/vala/Pluie/Yaml.NodeScalar.vala +++ b/src/vala/Pluie/Yaml.NodeScalar.vala @@ -37,12 +37,11 @@ public class Pluie.Yaml.NodeScalar : Yaml.BaseNode /** * construct a scalar node * @param parent the parent node - * @param indent the current indentation in node representation string * @param data the current scalar data */ - public NodeScalar (Yaml.Node? parent = null, int indent = 0, string? data = null) + public NodeScalar (Yaml.Node? parent = null, string? data = null) { - base (parent, indent, NODE_TYPE.SCALAR); + base (parent, NODE_TYPE.SCALAR); this.data = data; } @@ -52,6 +51,6 @@ public class Pluie.Yaml.NodeScalar : Yaml.BaseNode */ public override Yaml.Node clone_node (string? name = null) { - return new Yaml.NodeScalar (this.parent, this.indent, this.data); + return new Yaml.NodeScalar (this.parent, this.data); } } diff --git a/src/vala/Pluie/Yaml.NodeSequence.vala b/src/vala/Pluie/Yaml.NodeSequence.vala index 2449ba6..583bc26 100644 --- a/src/vala/Pluie/Yaml.NodeSequence.vala +++ b/src/vala/Pluie/Yaml.NodeSequence.vala @@ -44,12 +44,11 @@ public class Pluie.Yaml.NodeSequence : Yaml.BaseNode, Yaml.NodeCollection /** * construct a sequence node * @param parent the parent node - * @param indent the current indentation in node representation string * @param name the current name (key) of sequence node */ - public NodeSequence (Yaml.Node? parent = null, int indent = 0, string? name = null) + public NodeSequence (Yaml.Node? parent = null, string? name = null) { - base (parent, indent, NODE_TYPE.SEQUENCE); + base (parent, NODE_TYPE.SEQUENCE); this.name = name; } @@ -76,7 +75,7 @@ public class Pluie.Yaml.NodeSequence : Yaml.BaseNode, Yaml.NodeCollection this.list = new ArrayList (); } node.on_change_parent (); - node.indent = this.indent + 4; + node.level = this.level + 1; node.parent = this; return this.list.add (node); } @@ -88,7 +87,7 @@ public class Pluie.Yaml.NodeSequence : Yaml.BaseNode, Yaml.NodeCollection */ public Yaml.Node add_scalar (string? data = null) { - Yaml.Node scalar = new Yaml.NodeScalar (this, this.indent+4, data); + Yaml.Node scalar = new Yaml.NodeScalar (this, data); this.add (scalar); return scalar; } @@ -201,7 +200,7 @@ public class Pluie.Yaml.NodeSequence : Yaml.BaseNode, Yaml.NodeCollection public override Yaml.Node clone_node (string? name = null) { var key = name != null ? name : this.name; - Yaml.Node clone = new Yaml.NodeSequence (this.parent, this.indent, key); + Yaml.Node clone = new Yaml.NodeSequence (this.parent, key); if (this.list!= null && this.list.size > 0) { foreach (Yaml.Node child in this.list) { var n = child.clone_node(); diff --git a/src/vala/Pluie/Yaml.NodeSinglePair.vala b/src/vala/Pluie/Yaml.NodeSinglePair.vala index 3293c81..a2593c7 100644 --- a/src/vala/Pluie/Yaml.NodeSinglePair.vala +++ b/src/vala/Pluie/Yaml.NodeSinglePair.vala @@ -37,16 +37,15 @@ public class Pluie.Yaml.NodeSinglePair : Yaml.NodeMap /** * construct a single/pair mapping node * @param parent the parent node - * @param indent the current indentation in node representation string * @param name the current name (key) of sequence node * @param data the current scalar data */ - public NodeSinglePair (Yaml.Node? parent = null, int indent = 0, string? name = null, string? data = null) + public NodeSinglePair (Yaml.Node? parent = null, string? name = null, string? data = null) { - this.standard (parent, indent, NODE_TYPE.SINGLE_PAIR); + this.standard (parent, NODE_TYPE.SINGLE_PAIR); this.name = name; if (data != null) { - var scalar = new Yaml.NodeScalar (this, this.indent+4, data); + var scalar = new Yaml.NodeScalar (this, data); scalar.name = "singlepair"; this.add (scalar); } @@ -68,7 +67,7 @@ public class Pluie.Yaml.NodeSinglePair : Yaml.NodeMap public override Yaml.Node clone_node (string? name = null) { var key = name != null ? name : this.name; - Yaml.Node clone = new Yaml.NodeSinglePair (this.parent, this.indent, key, this.scalar ().data); + Yaml.Node clone = new Yaml.NodeSinglePair (this.parent, key, this.scalar ().data); return clone; } } diff --git a/src/vala/Pluie/Yaml.Processor.vala b/src/vala/Pluie/Yaml.Processor.vala index 27fefe4..ad19e72 100644 --- a/src/vala/Pluie/Yaml.Processor.vala +++ b/src/vala/Pluie/Yaml.Processor.vala @@ -77,9 +77,9 @@ public class Pluie.Yaml.Processor Yaml.Node node; /** - * previous indent + * previous level */ - int prev_indent; + int prev_level; /** * @@ -163,8 +163,8 @@ public class Pluie.Yaml.Processor this.root = new Yaml.NodeRoot (); this.prev_node = this.root; this.parent_node = this.root; - this.prev_indent = this.root.indent; - int indent = this.root.indent +4; + this.prev_level = this.root.level; + int level = this.root.level + 1; var it = this.events.iterator (); var change = false; string? key = null; @@ -179,8 +179,10 @@ public class Pluie.Yaml.Processor break; } if (evt.evtype.is_mapping_end () || evt.evtype.is_sequence_end ()) { - indent -= 4; - this.parent_node = this.prev_node.parent != this.root ? this.prev_node.parent.parent : this.root; + level -= 4; + this.parent_node = this.prev_node.parent != null && this.prev_node.parent != this.root + ? this.prev_node.parent.parent + : this.root; this.prev_node = this.parent_node; continue; } @@ -188,20 +190,20 @@ public class Pluie.Yaml.Processor evt = this.next_event(it); if (evt.evtype.is_mapping_start ()) { key = "_%d".printf((this.parent_node as Yaml.NodeSequence).get_size()); - this.node = new Yaml.NodeMap (this.parent_node, indent, key); + this.node = new Yaml.NodeMap (this.parent_node, key); key = null; - indent += 4; + level += 1; change = true; } else if (evt.evtype.is_scalar ()) { var content = evt.data["data"]; - this.node = new Yaml.NodeScalar (this.parent_node, indent, content); + this.node = new Yaml.NodeScalar (this.parent_node, content); change = true; } } if (beginFlowSeq && evt.evtype.is_scalar ()) { var content = evt.data["data"]; - this.node = new Yaml.NodeScalar (this.parent_node, indent, content); + this.node = new Yaml.NodeScalar (this.parent_node, content); change = true; beginFlowSeq = false; } @@ -212,7 +214,7 @@ public class Pluie.Yaml.Processor if (evt.evtype.is_scalar ()) { var content = evt.data["data"]; if (key != null) { - this.node = new Yaml.NodeSinglePair (this.parent_node, indent, key, content); + this.node = new Yaml.NodeSinglePair (this.parent_node, key, content); change = true; } } @@ -226,18 +228,18 @@ public class Pluie.Yaml.Processor if (refnode != null) { this.node = refnode.clone_node (key); this.parent_node.add (this.node); - this.prev_node = this.node; - this.prev_indent = this.prev_node.indent; + this.prev_node = this.node; + this.prev_level = this.prev_node.level; } } if (evt.evtype.is_mapping_start ()) { - this.node = new Yaml.NodeMap (this.parent_node, indent, key); - indent += 4; + this.node = new Yaml.NodeMap (this.parent_node, key); + level += 1; change = true; } else if (evt.evtype.is_sequence_start ()) { - this.node = new Yaml.NodeSequence (this.parent_node, indent, key); - indent += 4; + this.node = new Yaml.NodeSequence (this.parent_node, key); + level += 1; change = true; beginFlowSeq = true; } @@ -255,7 +257,7 @@ public class Pluie.Yaml.Processor this.parent_node = this.node; } this.prev_node = this.node; - this.prev_indent = this.prev_node.indent; + this.prev_level = this.prev_node.level; this.node = null; change = false; }