adding Yaml.Config

This commit is contained in:
a-sansara 2018-08-01 17:49:01 +02:00
parent cc60109353
commit 133147205b
4 changed files with 120 additions and 1 deletions

View File

@ -4,6 +4,8 @@
As json is now a valid subset of yaml, you can use this lib to load json files too. 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. 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. **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 ## License
@ -49,6 +51,18 @@ docker run --rm -it pluie/libyaml
## Usage ## 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 ### loader
``` ```
@ -102,5 +116,7 @@ see samples files in ./samples directory
### todo ### todo
* dumper * import clause
* fix nodes traversing * fix nodes traversing
* dumper

View File

@ -61,6 +61,7 @@ sources = [
'src/vala/Pluie/Io.Reader.vala', 'src/vala/Pluie/Io.Reader.vala',
'src/vala/Pluie/Io.StreamLineMark.vala', 'src/vala/Pluie/Io.StreamLineMark.vala',
'src/vala/Pluie/Yaml.global.vala', 'src/vala/Pluie/Yaml.global.vala',
'src/vala/Pluie/Yaml.Config.vala',
'src/vala/Pluie/Yaml.Document.vala', 'src/vala/Pluie/Yaml.Document.vala',
'src/vala/Pluie/Yaml.Event.vala', 'src/vala/Pluie/Yaml.Event.vala',
'src/vala/Pluie/Yaml.Loader.vala', 'src/vala/Pluie/Yaml.Loader.vala',

56
samples/yaml-config.vala Normal file
View File

@ -0,0 +1,56 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* @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");
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;
}

View File

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