From 133147205bfcc75bd409e3e6185282be5a13c7a5 Mon Sep 17 00:00:00 2001 From: a-sansara Date: Wed, 1 Aug 2018 17:49:01 +0200 Subject: [PATCH] adding Yaml.Config --- README.md | 18 ++++++++++- meson.build | 1 + samples/yaml-config.vala | 56 +++++++++++++++++++++++++++++++++ src/vala/Pluie/Yaml.Config.vala | 46 +++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 samples/yaml-config.vala create mode 100644 src/vala/Pluie/Yaml.Config.vala diff --git a/README.md b/README.md index f01ff92..699de4e 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ 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. + **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 @@ -49,6 +51,18 @@ docker run --rm -it pluie/libyaml ## Usage +### config + +``` + + var config = new Yaml.Config (path); + var node = config.get ("ship-to.address.city{0}"); + if (node != null) { + of.echo (node.data) + } + +``` + ### loader ``` @@ -102,5 +116,7 @@ see samples files in ./samples directory ### todo -* dumper +* import clause * fix nodes traversing +* dumper + diff --git a/meson.build b/meson.build index b5f4947..bb64807 100644 --- a/meson.build +++ b/meson.build @@ -61,6 +61,7 @@ sources = [ 'src/vala/Pluie/Io.Reader.vala', 'src/vala/Pluie/Io.StreamLineMark.vala', 'src/vala/Pluie/Yaml.global.vala', + 'src/vala/Pluie/Yaml.Config.vala', 'src/vala/Pluie/Yaml.Document.vala', 'src/vala/Pluie/Yaml.Event.vala', 'src/vala/Pluie/Yaml.Loader.vala', diff --git a/samples/yaml-config.vala b/samples/yaml-config.vala new file mode 100644 index 0000000..7a1f463 --- /dev/null +++ b/samples/yaml-config.vala @@ -0,0 +1,56 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * + * @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"); + + var config = new Yaml.Config (path); + var spath = "ship-to.address.city{0}"; + var node = config.get (spath); + if ((done = node != null)) { + of.action ("retriew node from Yaml.Config", spath); + of.echo (node.to_string (false)); + of.echo (node.data); + } + + of.rs (done); + of.echo (); + return (int) done; + +} diff --git a/src/vala/Pluie/Yaml.Config.vala b/src/vala/Pluie/Yaml.Config.vala new file mode 100644 index 0000000..13321b8 --- /dev/null +++ b/src/vala/Pluie/Yaml.Config.vala @@ -0,0 +1,46 @@ +/** + * a class to manage Yaml configuration files + */ +public class Pluie.Yaml.Config +{ + /** + * current path + */ + public string? path { get; internal set; default = null; } + + /** + * Yaml Loader + */ + public Yaml.Loader loader { internal get; internal set; } + + /** + * Yaml Finder + */ + public Yaml.Finder finder { internal get; internal set; } + + /** + * construct a Yaml Config for specifiyed path + */ + public Config (string? path = null, 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); + this.finder = new Yaml.Finder(this.loader.get_nodes ()); + } + } + + /** + * find node matching specifiyed keyPath + */ + public new Yaml.Node? get (string keyPath) + { + Yaml.Node? node = null; + if (this.finder != null) { + node = this.finder.find (keyPath); + } + return node; + } + +}