add finder sample

This commit is contained in:
a-sansara 2018-08-01 16:44:07 +02:00
parent 652c0a31f8
commit b4fe09f3ec
6 changed files with 135 additions and 10 deletions

View File

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

View File

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

116
samples/yaml-finder.vala Normal file
View File

@ -0,0 +1,116 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* @software : lib-yaml <https://git.pluie.org/pluie/lib-yaml>
* @version : 0.3
* @date : 2018
* @licence : GPLv3.0 <http://www.gnu.org/licenses/>
* @author : a-Sansara <[dev]at[pluie]dot[org]>
* @copyright : pluie.org <http://www.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 <http://www.gnu.org/licenses/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
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;
}

View File

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

View File

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

View File

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