pluie-yaml/samples/yaml-finder.vala

118 lines
3.9 KiB
Vala
Raw Normal View History

2018-08-01 14:44:07 +00:00
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* @software : lib-yaml <https://git.pluie.org/pluie/lib-yaml>
2018-08-06 14:57:20 +00:00
* @version : 0.4
2018-08-01 14:44:07 +00:00
* @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 = Yaml.DATA_PATH + "/test.yml";
2018-08-01 14:44:07 +00:00
var done = false;
of.title ("Pluie Yaml Library", Pluie.Yaml.VERSION, "a-sansara");
//~ Pluie.Yaml.DEBUG = false;
2018-08-01 14:44:07 +00:00
var loader = new Yaml.Loader (path, true);
if ((done = loader.done)) {
2018-08-05 13:18:25 +00:00
Yaml.Node root = loader.get_nodes ();
2018-08-01 14:44:07 +00:00
var finder = new Yaml.Finder(root);
Yaml.Node? node = null;
2018-08-02 21:44:36 +00:00
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);
2018-08-05 13:18:25 +00:00
of.echo (node.first ().data);
2018-08-02 21:44:36 +00:00
of.action ("get parent node");
of.echo (node.parent.to_string ());
of.action ("get address node");
2018-08-05 13:18:25 +00:00
if ((node = (node.parent as Yaml.Mapping).item ("address"))!= null) {
2018-08-02 21:44:36 +00:00
of.echo (node.to_string (false));
2018-08-05 13:18:25 +00:00
node.display_childs ();
2018-08-02 21:44:36 +00:00
of.action ("Loop throught childs", node.name);
2018-08-05 13:18:25 +00:00
foreach (var child in node.list) {
2018-08-02 21:44:36 +00:00
of.echo (child.to_string (false));
}
}
of.state (node != null);
}
else of.state (node != null);
of.action ("Set find mode", "SQUARE_BRACKETS");
2018-08-05 13:18:25 +00:00
Yaml.MODE = Yaml.FIND_MODE.SQUARE_BRACKETS;
2018-08-02 21:44:36 +00:00
of.state (true);
spath = "[product]{0}[description]";
2018-08-01 14:44:07 +00:00
// 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.rs (done);
of.echo ();
return (int) done;
}