diff --git a/meson.build b/meson.build index 619f2ba..b5f4947 100644 --- a/meson.build +++ b/meson.build @@ -64,6 +64,7 @@ sources = [ 'src/vala/Pluie/Yaml.Document.vala', 'src/vala/Pluie/Yaml.Event.vala', 'src/vala/Pluie/Yaml.Loader.vala', + 'src/vala/Pluie/Yaml.Finder.vala', 'src/vala/Pluie/Yaml.Scanner.vala', 'src/vala/Pluie/Yaml.Processor.vala', 'src/vala/Pluie/Yaml.BaseNode.vala', diff --git a/samples/json-load.vala b/samples/json-load.vala index b45e81e..6f01fb9 100644 --- a/samples/json-load.vala +++ b/samples/json-load.vala @@ -38,7 +38,7 @@ int main (string[] args) var path = "resources/test.json"; var done = false; - of.title ("Pluie Yaml Parser", Pluie.Yaml.VERSION, "a-sansara"); + of.title ("Pluie Yaml Library", Pluie.Yaml.VERSION, "a-sansara"); Pluie.Yaml.Scanner.DEBUG = true; var loader = new Yaml.Loader (path, true, true); if ((done = loader.done)) { diff --git a/samples/yaml-finder.vala b/samples/yaml-finder.vala new file mode 100644 index 0000000..8f8710d --- /dev/null +++ b/samples/yaml-finder.vala @@ -0,0 +1,116 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * + * @software : lib-yaml + * @version : 0.3 + * @date : 2018 + * @licence : GPLv3.0 + * @author : a-Sansara <[dev]at[pluie]dot[org]> + * @copyright : pluie.org + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * + * This file is part of lib-yaml. + * + * lib-yaml is free software (free as in speech) : you can redistribute it + * and/or modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * lib-yaml is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with lib-yaml. If not, see . + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ + +using GLib; +using Gee; +using Pluie; + +int main (string[] args) +{ + Echo.init(false); + + var path = "resources/test.yml"; + var done = false; + + of.title ("Pluie Yaml Library", Pluie.Yaml.VERSION, "a-sansara"); +//~ Pluie.Yaml.Scanner.DEBUG = false; + var loader = new Yaml.Loader (path, true); + if ((done = loader.done)) { + Yaml.NodeRoot root = loader.get_nodes (); + var finder = new Yaml.Finder(root); + Yaml.Node? node = null; + + var spath = "[product]{0}[description]"; + // equivalent in DOT MODE + // spath = "product{0}.description"; + of.action ("Find node", spath); + if ((node = finder.find(spath)) != null) { + of.echo (node.to_string (false)); + } + of.state (node != null); + + spath = "[product]{0}[description]{0}"; + // equivalent in DOT MODE + // spath = "product{0}.description[0}"; + of.action ("Find scalar node", spath); + if ((node = finder.find(spath)) != null) { + of.echo (node.to_string (false)); + } + of.state (node != null); + + spath = "[product]{1}"; + of.action ("Find node", spath); + if ((node = finder.find(spath)) != null) { + of.echo (node.to_string (false)); + of.state (node != null); + + spath = "[description]{0}"; + of.action ("Find subnode in node context", spath); + of.keyval ("context", node.name); + if ((node = finder.find(spath, node)) != null) { + of.echo (node.to_string (false)); + } + of.state (node != null); + } + 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); + of.echo (); + return (int) done; + +} diff --git a/samples/yaml-load.vala b/samples/yaml-load.vala index 8a9ce41..b214606 100644 --- a/samples/yaml-load.vala +++ b/samples/yaml-load.vala @@ -38,7 +38,7 @@ int main (string[] args) var path = "resources/test.yml"; var done = false; - of.title ("Pluie Yaml Parser", Pluie.Yaml.VERSION, "a-sansara"); + of.title ("Pluie Yaml Library", Pluie.Yaml.VERSION, "a-sansara"); Pluie.Yaml.Scanner.DEBUG = true; var loader = new Yaml.Loader (path, true, true); if ((done = loader.done)) { diff --git a/src/vala/Pluie/Yaml.Finder.vala b/src/vala/Pluie/Yaml.Finder.vala index 779e783..ad7fe11 100644 --- a/src/vala/Pluie/Yaml.Finder.vala +++ b/src/vala/Pluie/Yaml.Finder.vala @@ -111,12 +111,13 @@ public class Pluie.Yaml.Finder : Object string search = this.find_path (path); bool match = false; Yaml.Node? node = context == null ? this.context : context; - Regex reg = /(\[|\{)([^\]]*)(\]|\})/; + Regex reg = /(\[|\{)([^\]\}]*)(\]|\})/; MatchInfo mi; try { - of.echo ("find node %s".printf (path)); +//~ of.echo ("find node %s".printf (path)); +//~ of.echo ("search %s".printf (search)); for (reg.match (search, 0, out mi) ; mi.matches () ; mi.next ()) { - of.echo ("%s%s%s".printf (mi.fetch (1), mi.fetch (2), mi.fetch (3))); +//~ of.echo ("=> %s%s%s".printf (mi.fetch (1), mi.fetch (2), mi.fetch (3))); if (this.is_collection_path (mi)) { if (!match) match = true; if (this.is_collection_path (mi, true)) { @@ -124,11 +125,18 @@ public class Pluie.Yaml.Finder : Object } else { int index = int.parse (mi.fetch (FIND_COLLECTION.KEY)); - var n = node as Yaml.NodeSequence; - if (index < n.list.size && index >= 0) { - node = n.list.get (index); + if (index == 0 && node.node_type.is_single_pair ()) { + var n = node as Yaml.NodeSinglePair; + node = n.scalar (); + } + // assume sequence + else { + var n = node as Yaml.NodeSequence; + if (index < n.list.size && index >= 0) { + node = n.list.get (index); + } + else node = null; } - else node = null; } if (node == null) break; } diff --git a/src/vala/Pluie/Yaml.NodeMap.vala b/src/vala/Pluie/Yaml.NodeMap.vala index 4095efc..2026f2c 100644 --- a/src/vala/Pluie/Yaml.NodeMap.vala +++ b/src/vala/Pluie/Yaml.NodeMap.vala @@ -89,7 +89,7 @@ public class Pluie.Yaml.NodeMap : Yaml.BaseNode, Yaml.NodeCollection public void display_childs (bool root=true) { if (root == true) { - of.action ("display root node\n"); + of.action ("display childs\n"); } of.echo (this.to_string ()); if (this.map.size > 0) {