pluie-yaml/README.md

240 lines
5.9 KiB
Markdown
Raw Normal View History

2018-08-01 13:10:12 +00:00
# pluie-yaml
2018-08-01 15:15:45 +00:00
**pluie-yaml** is a vala shared library managing yaml files (v 1.2) and yaml nodes in vala language.
As json is now a valid subset of yaml, you can use this lib to load json files too.
2018-08-01 13:10:12 +00:00
2018-08-01 15:15:45 +00:00
The purpose of this project is to make vala able to load and deal with yaml configuration files.
2018-08-07 22:56:38 +00:00
So, currently the lib deal only with one yaml document (it's not recommended to use multiples doc),
but you can use a special `^imports` clause (special mapping node) to load a subset of yaml files
in the main yaml document.
2018-08-01 15:49:01 +00:00
2018-08-02 21:44:36 +00:00
the lib does not manage yet tag directives and tag values.
2018-08-01 15:15:45 +00:00
**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.
2018-08-06 14:57:20 +00:00
![pluie-yaml](https://www.meta-tech.academy/img/pluie-yaml-imports2.png)
_legend display_childs_ :
```
2018-08-07 22:56:38 +00:00
[ node.name [refCount] node.parent.name node.level node.ntype.infos () node.count () node.uuid ]
```
2018-08-07 22:56:38 +00:00
2018-08-01 15:15:45 +00:00
## License
GNU GPL v3
2018-08-01 13:10:12 +00:00
## Prerequisites
2018-08-01 15:15:45 +00:00
`valac meson ninja glib gee gobject pluie-echo`
2018-08-01 13:10:12 +00:00
2018-08-07 22:56:38 +00:00
see https://git.pluie.org/pluie/libpluie-echo in order to install pluie-echo-0.2 pkg
2018-08-01 13:10:12 +00:00
## Install
2018-08-01 15:15:45 +00:00
git clone the project then cd to project directory and do :
2018-08-01 13:10:12 +00:00
```
meson --prefix=/usr ./ build
sudo ninja install -C build
```
## Compilation
```
2018-08-05 13:18:25 +00:00
valac --pkg gee-0.8 --pkg pluie-echo-0.2 --pkg pluie-yaml-0.4 main.vala
2018-08-01 13:10:12 +00:00
```
2018-08-01 15:15:45 +00:00
you can use `./build.sh` to rebuild/install the **pluie-yaml** lib and compile samples files
2018-08-07 22:56:38 +00:00
2018-08-01 13:10:12 +00:00
## Api / Documentation
2018-08-05 13:18:25 +00:00
https://pluie.org/pluie-yaml-0.4/index.htm
2018-08-01 13:10:12 +00:00
2018-08-07 22:56:38 +00:00
2018-08-01 13:10:12 +00:00
## Docker
2018-08-07 22:56:38 +00:00
a demo image is now available on docker hub. To run a container :
2018-08-01 13:10:12 +00:00
```
docker run --rm -it pluie/libyaml
```
2018-08-07 22:56:38 +00:00
then you can execute any samples, for example :
```
./json-loader
```
![pluie-yaml-json](https://www.meta-tech.academy/img/pluie-yaml-json.png)
2018-08-07 22:56:38 +00:00
see ![pluie/docker-images repository](https://github.com/pluie-org/docker-images)
for more details
2018-08-01 13:10:12 +00:00
## Usage
2018-08-02 21:44:36 +00:00
-------------------
2018-08-01 15:49:01 +00:00
### config
2018-08-02 21:44:36 +00:00
```vala
2018-08-01 15:49:01 +00:00
var config = new Yaml.Config (path);
var node = config.get ("ship-to.address.city{0}");
if (node != null) {
of.echo (node.data)
}
```
2018-08-07 22:56:38 +00:00
see Finder below to get precisions about config.get parameter (search path definition)
2018-08-05 13:18:25 +00:00
2018-08-02 21:44:36 +00:00
-------------------
### config with ^imports clause
```yml
# | use special key word '^imports' to import other yaml config files in
# | current yaml document
# | '^imports' must be uniq and a direct child of root node
# | imported files are injected as mapping nodes at top document level
# | so you cannot use keys that already exists in the document
^imports :
# you can redefine default import path with the special key '^path'
# if you do not use it, the default path value will be the current directory
# redefined path values are relative to the current directory (if a relative path
# is provided)
^path : ./config
# you can also define any other var by prefixing key with ^
^dir : subdir
# and use it enclosed by ^
# here final test path will be "./config/subdir/test.yml"
test : ^dir^/test.yml
# here final db path will be "./config/db.yml"
db : db.yml
```
-------------------
2018-08-01 15:49:01 +00:00
2018-08-01 15:15:45 +00:00
### loader
2018-08-02 21:44:36 +00:00
load a single document.
`^imports` clause is out of effects here.
```vala
2018-08-01 15:15:45 +00:00
var path = "./config/main.yml";
// uncomment to enable debug
// Pluie.Yaml.Scanner.DEBUG = true;
var loader = new Yaml.Loader (path /* , displayFile, displayNode */);
2018-08-01 13:10:12 +00:00
if ((done = loader.done)) {
2018-08-05 13:18:25 +00:00
Yaml.Node root = loader.get_nodes ();
2018-08-01 13:10:12 +00:00
root.display_childs ();
}
```
2018-08-02 21:44:36 +00:00
-------------------
2018-08-01 13:10:12 +00:00
2018-08-01 15:15:45 +00:00
### finder
**lib-yaml** provide a `Yaml.Finder` to easily retriew a particular yaml node.
Search path definition has two mode.
2018-08-02 21:44:36 +00:00
The default mode is `Yaml.FIND_MODE.DOT`
2018-08-01 15:15:45 +00:00
- child mapping node are separated by dot
- sequence entry must be enclosed in curly brace
ex : `grandfather.father.son{2}.age`
2018-08-02 21:44:36 +00:00
The Other mode is Yaml.FIND_MODE.SQUARE_BRACKETS
- node's key name must be enclosed in square brackets
- sequence entry must be enclosed in curly brace
ex : `[grandfather][father][son]{2}[age]`
2018-08-01 15:15:45 +00:00
with singlepair node, you can retriew corresponding scalar node with {0}
2018-08-02 21:44:36 +00:00
ex yaml file :
```yml
2018-08-01 15:15:45 +00:00
product:
- sku : BL394D
quantity : 4
description : Basketball
2018-08-02 21:44:36 +00:00
```
vala code :
```vala
2018-08-01 15:15:45 +00:00
...
2018-08-05 13:18:25 +00:00
var loader = new Yaml.Loader (path, true);
if ((done = loader.done)) {
Yaml.Node root = loader.get_nodes ();
var finder = new Yaml.Finder(root);
Yaml.Node? node = null;
if ((node = finder.find ("product{0}.description")) != null) {
var val = node.val ();
}
...
}
```
### Traversing
#### via iterator
2018-08-06 14:57:20 +00:00
```vala
2018-08-05 13:18:25 +00:00
var config = new Yaml.Config (path);
var root = config.root_node ();
Iterator<Yaml.Node> it = root.iterator ();
Yaml.Node? child = null;
for (var has_next = it.next (); has_next; has_next = it.next ()) {
child = it.get ();
of.echo (child.to_string ());
2018-08-01 15:15:45 +00:00
}
```
2018-08-05 13:18:25 +00:00
#### other
2018-08-06 14:57:20 +00:00
```vala
2018-08-05 13:18:25 +00:00
if (!node.empty ()) {
Yaml.Node child = node.first();
of.action("loop throught mapping next sibling", child.name);
while (child!=null && !child.is_last()) {
// do stuff
of.echo (child.to_string ());
child = child.next_sibling ();
}
}
```
2018-08-06 14:57:20 +00:00
```vala
2018-08-05 13:18:25 +00:00
if (node.count () > 0) {
child = node.last();
of.action("loop throught mapping previous sibling", child.name);
while (child!=null && !child.is_first()) {
// do stuff
of.echo (child.to_string ());
child = child.previous_sibling ();
}
}
```
2018-08-02 21:44:36 +00:00
-------------------
2018-08-01 15:15:45 +00:00
2018-08-01 13:10:12 +00:00
### more samples
see samples files in ./samples directory
2018-08-01 15:15:45 +00:00
2018-08-02 21:44:36 +00:00
-------------------
2018-08-01 15:15:45 +00:00
### todo
2018-08-02 21:44:36 +00:00
* ~~imports clause~~
2018-08-05 13:18:25 +00:00
* ~~fix nodes traversing~~
* ~~rewrite nodes classes~~
2018-08-06 14:57:20 +00:00
* ~~put doc online~~
2018-08-07 22:56:38 +00:00
* ~~add docker image~~
2018-08-06 14:57:20 +00:00
* improve doc
2018-08-01 15:49:01 +00:00
* dumper
2018-08-02 21:44:36 +00:00
* manage tag directives & tag