tag directives & tag partially managed

This commit is contained in:
a-Sansara 2018-08-11 02:28:19 +02:00
parent 6c1e4d0251
commit 7a597188de
13 changed files with 304 additions and 76 deletions

View File

@ -8,7 +8,8 @@ So, currently the lib deal only with one yaml document (it's not recommended to
but you can use a special `^imports` clause (special mapping node) to load a subset of yaml files
in the main yaml document.
the lib does not manage yet tag directives and tag values (planned).
The lib partially manage tag directives and tag values (basic types and Yaml.Object extended objects types).
**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](https://www.meta-tech.academy/img/pluie-yaml-imports2.png)
@ -237,6 +238,55 @@ or
-------------------
### Tag Directives & Tag values
an example is available with `samples/yaml-tag.vala` sample
and `resources/tag.yml` file
on yaml side, proceed like that :
```yaml
%YAML 1.2
%TAG !v! tag:pluie.org,2018:vala/
---
!v!Pluie.Yaml.Example test1 :
myname : test1object
type_int : !v!int 3306
type_bool : !v!bool false
type_char : !v!char c
type_string : !v!string mystring1
type_uchar : !v!uchar L
type_uint : !v!uint 62005
type_float : !v!float 42.36
type_double : !v!double 95542123.4579512128
!v!Pluie.Yaml.SubExample type_object :
toto : totovalue1
tata : tatavalue1
titi : 123
tutu : 1
```
on vala side :
```vala
...
Yaml.Example obj = (Yaml.Example) Yaml.Object.from_node (root.first ());
of.echo("obj.type_int : %d".printf (o.type_int));
obj.type_object.method_a ()
```
![pluie-yaml-tag](https://www.meta-tech.academy/img/libyaml-tag-ex.png)
code from samples/yaml-tag.vala :
![pluie-yaml-tag](https://www.meta-tech.academy/img/libyaml-tag-code.png)
output from samples/yaml-tag.vala :
![pluie-yaml-tag](https://www.meta-tech.academy/img/libyaml-tag-ex2.png)
-------------------
### more samples
see samples files in ./samples directory
@ -250,6 +300,6 @@ see samples files in ./samples directory
* ~~rewrite nodes classes~~
* ~~put doc online~~
* ~~add docker image~~
* manage tag directives & tag
* manage tag directives & tag (partially done)
* improve doc
* dumper

View File

@ -74,9 +74,11 @@ sources = [
'src/vala/Pluie/Yaml.Node.vala',
'src/vala/Pluie/Yaml.Object.vala',
'src/vala/Pluie/Yaml.Processor.vala',
'src/vala/Pluie/Yaml.Root.vala',
'src/vala/Pluie/Yaml.Scalar.vala',
'src/vala/Pluie/Yaml.Scanner.vala',
'src/vala/Pluie/Yaml.Sequence.vala',
'src/vala/Pluie/Yaml.SubExample.vala',
'src/vala/Pluie/Yaml.Tag.vala',
'src/c/yaml.c'
]

View File

@ -3,7 +3,7 @@
%TAG !v! tag:pluie.org,2018:vala/
---
!v!Pluie.Yaml.Example test1 :
myname : test1object
type_int : !v!int 3306
type_bool : !v!bool false
type_char : !v!char c
@ -12,14 +12,24 @@
type_uint : !v!uint 62005
type_float : !v!float 42.36
type_double : !v!double 95542123.4579512128
!v!Pluie.Yaml.SubExample type_object :
toto : totovalue1
tata : tatavalue1
titi : 123
tutu : 1
!v!Pluie.Yaml.Example test2 :
type_int : !v!int 3306
type_bool : !v!bool true
type_char : !v!char g
type_string : !v!string mystring2
type_uchar : !v!uchar Y
type_uint : !v!uint 63005
type_float : !v!float 5.28
type_double : !v!double 9.28
myname : test2object
type_int : 3306
type_bool : "true"
type_char : g
type_string : mystring2
type_uchar : Y
type_uint : 63005
type_float : 5.28
type_double : 9.28
!v!Pluie.Yaml.SubExample type_object :
toto : totovalue2
tata : tatavalue2
titi : !v!int 456
tutu : !v!bool TRUE

View File

@ -59,12 +59,11 @@ int main (string[] args)
node = node.next_sibling ();
}
}
of.echo ("");
// hard code
Yaml.Example? o = new Pluie.Yaml.Example ();
Yaml.Example? o = null;
foreach (var entry in list.entries) {
if ((o = (Yaml.Example) entry.value)!=null) {
of.action ("Getting Hard coded values for Yaml.Object %s".printf (of.c (ECHO.MICROTIME).s (o.type_from_self ())), entry.key);
if ((o = entry.value as Yaml.Example)!=null) {
of.keyval("type_int" , "%d" .printf(o.type_int));
of.keyval("type_bool" , "%s" .printf(o.type_bool.to_string ()));
of.keyval("type_char" , "%c" .printf(o.type_char));
@ -72,6 +71,12 @@ int main (string[] args)
of.keyval("type_uchar" , "%u" .printf(o.type_uchar));
of.keyval("type_float" , "%f" .printf(o.type_float));
of.keyval("type_double", "%f" .printf(o.type_double));
of.keyval("type_object", "%s" .printf(o.type_object.get_type ().name ()));
of.keyval(" toto (string)", "%s" .printf(o.type_object.toto));
of.keyval(" tapa (string)", "%s" .printf(o.type_object.tata));
of.keyval(" titi (int)" , "%d" .printf(o.type_object.titi));
of.keyval(" tutu (bool)" , "%s" .printf(o.type_object.tutu.to_string ()));
o.type_object.method_a ();
}
}

View File

@ -88,7 +88,17 @@ public abstract class Pluie.Yaml.AbstractNode : GLib.Object
return node != null && node.uuid == this.uuid;
}
public virtual string to_string (bool withIndent = Yaml.DBG_SHOW_INDENT, bool withParent = Yaml.DBG_SHOW_PARENT, bool withUuid = Yaml.DBG_SHOW_UUID, bool withLevel = Yaml.DBG_SHOW_LEVEL, bool withCount = Yaml.DBG_SHOW_COUNT, bool withRefCount = Yaml.DBG_SHOW_REF){
public virtual string to_string (
bool withIndent = Yaml.DBG_SHOW_INDENT,
bool withParent = Yaml.DBG_SHOW_PARENT,
bool withUuid = Yaml.DBG_SHOW_UUID,
bool withLevel = Yaml.DBG_SHOW_LEVEL,
bool withCount = Yaml.DBG_SHOW_COUNT,
bool withRefCount = Yaml.DBG_SHOW_REF,
bool withTag = Yaml.DBG_SHOW_TAG,
bool withType = Yaml.DBG_SHOW_TYPE
)
{
return "";
}

View File

@ -33,6 +33,7 @@
*/
public class Pluie.Yaml.Example : Yaml.Object
{
public string myname { get; set; }
public string type_string { get; set; }
public int type_int { get; set; }
public uint type_uint { get; set; }
@ -56,5 +57,15 @@ public class Pluie.Yaml.Example : Yaml.Object
public int64 type_int64 { get; set; }
public uint64 type_uint64 { get; set; }
public bool type_bool { get; set; }
public Yaml.SubExample type_object { get; set; }
/**
*
*/
protected override void yaml_init ()
{
// base.yaml_init ();
Dbg.msg ("Yaml.Object %s (%s) instantiated".printf (this.myname, this.type_from_self ()), Log.LINE, Log.FILE);
}
}

View File

@ -55,7 +55,7 @@ public class Pluie.Yaml.Mapping : Yaml.Node
*/
public Mapping.with_scalar (Yaml.Node? parent = null, string? name = null, string? data = null)
{
base (parent, NODE_TYPE.MAPPING, name);
base (parent, NODE_TYPE.SINGLE_PAIR, name);
var s = new Scalar (null, data);
this.add (s);
}
@ -90,6 +90,9 @@ public class Pluie.Yaml.Mapping : Yaml.Node
{
base.on_added (child);
if (this.keys != null) {
if (!this.ntype.is_single_pair () && this.keys.size == 0 && child.ntype.is_scalar ()) {
this.ntype = Yaml.NODE_TYPE.SINGLE_PAIR;
}
this.keys.add(child.name);
}
}

View File

@ -259,6 +259,7 @@ public class Pluie.Yaml.Node : Yaml.AbstractChild, Pluie.Yaml.Collection
{
if (withTitle) {
of.action ("display_childs", this.name);
of.echo ("");
}
of.echo (this.to_string ());
if (!this.empty ()) {
@ -271,13 +272,22 @@ public class Pluie.Yaml.Node : Yaml.AbstractChild, Pluie.Yaml.Collection
/**
* get a presentation string of current Yaml.Node
*/
public override string to_string (bool withIndent = Yaml.DBG_SHOW_INDENT, bool withParent = Yaml.DBG_SHOW_PARENT, bool withUuid = Yaml.DBG_SHOW_UUID, bool withLevel = Yaml.DBG_SHOW_LEVEL, bool withCount = Yaml.DBG_SHOW_COUNT, bool withRefCount = Yaml.DBG_SHOW_REF)
public override string to_string (
bool withIndent = Yaml.DBG_SHOW_INDENT,
bool withParent = Yaml.DBG_SHOW_PARENT,
bool withUuid = Yaml.DBG_SHOW_UUID,
bool withLevel = Yaml.DBG_SHOW_LEVEL,
bool withCount = Yaml.DBG_SHOW_COUNT,
bool withRefCount = Yaml.DBG_SHOW_REF,
bool withTag = Yaml.DBG_SHOW_TAG,
bool withType = Yaml.DBG_SHOW_TYPE
)
{
return "%s%s%s%s%s%s%s%s%s%s".printf (
return "%s%s%s%s%s%s%s%s%s%s%s".printf (
this.level == 0 ? "" : of.s_indent ((int8) (withIndent ? (this.level-1)*4 : 0)),
of.c (ECHO.OPTION).s ("["),
this.name != null && !this.ntype.is_scalar ()
? of.c (ECHO.TIME).s ("%s".printf (this.name))
? of.c (ntype.is_root () ? ECHO.MICROTIME : ECHO.TIME).s ("%s".printf (this.name))
: (
this.ntype.is_scalar ()
? of.c(ECHO.DATE).s ("%s".printf (this.data))
@ -285,18 +295,21 @@ public class Pluie.Yaml.Node : Yaml.AbstractChild, Pluie.Yaml.Collection
),
withRefCount ? of.c (ECHO.COMMAND).s ("[%lu]".printf (this.ref_count)) : "",
!withParent || this.parent == null
? ""
? withLevel ? of.c (ECHO.NUM).s (" %d".printf (this.level)) : ""
: of.c (ECHO.SECTION).s (" "+this.parent.name)+(
withLevel ? of.c (ECHO.NUM).s (" %d".printf (this.level)) : " "
),
of.c (ECHO.OPTION_SEP).s (" %s".printf(
!this.ntype.is_mapping () || this.count () >= 1 && !this.first().ntype.is_scalar () ? this.ntype.infos () : NODE_TYPE.SINGLE_PAIR.infos ()
)),
withCount ? of.c (ECHO.MICROTIME).s (" %d".printf(this.count ())) : "",
withType ? of.c (ECHO.OPTION_SEP).s (" %s".printf(this.ntype.infos ())) : "",
withCount && this.ntype.is_collection () ? of.c (ECHO.MICROTIME).s (" %d".printf(this.count ())) : "",
withUuid ? of.c (ECHO.COMMENT).s (" %s".printf(this.uuid[0:8]+"...")) : "",
this.tag != null ? of.c (ECHO.OPTION_SEP).s (" %s".printf(this.tag.@value)) : "",
//~ of.c (ECHO.NUM).s ("%d".printf (this.level)),
of.c (ECHO.OPTION).s ("]")
this.tag != null && withTag
? " %s%s".printf (
of.c (ECHO.TITLE).s (" %s ".printf(this.tag.handle)),
of.c (ECHO.DEFAULT).s (" %s".printf(this.tag.value))
)
: "",
of.c (ECHO.OPTION).s ("]"),
withTag && this.ntype.is_root () ? (this as Yaml.Root).get_display_tag_directives () : ""
);
}
}

View File

@ -34,8 +34,19 @@ using Gee;
*/
public abstract class Pluie.Yaml.Object : GLib.Object
{
/**
*
*/
private static GLib.Module? p_module;
/**
*
*/
public virtual void yaml_init ()
{
Dbg.msg ("Yaml.Object (%s) instantiated".printf (this.type_from_self ()), Log.LINE, Log.FILE);
}
/**
*
*/
@ -136,16 +147,16 @@ public abstract class Pluie.Yaml.Object : GLib.Object
obj = (Yaml.Object) GLib.Object.new (type);
if (node!= null && !node.empty ()) {
GLib.ParamSpec? def = null;
Yaml.Node? snode = null;
Yaml.Node? scalar = null;
foreach (var child in node) {
if ((def = obj.get_class ().find_property (child.name)) != null) {
if ((snode = child.first ()) != null) {
if (snode.tag != null) {
obj.set_from_scalar (def.name, def.value_type, snode);
if (child.ntype.is_single_pair ()) {
if ((scalar = child.first ()) != null) {
obj.set_from_scalar (def.name, def.value_type, scalar);
}
else {
obj.set (child.name, snode.data);
}
else if (child.ntype.is_mapping ()) {
obj.set (child.name, from_node(child));
}
}
}
@ -159,6 +170,7 @@ public abstract class Pluie.Yaml.Object : GLib.Object
catch (GLib.Error e) {
of.warn (e.message);
}
obj.yaml_init ();
return obj;
}
@ -185,7 +197,7 @@ public abstract class Pluie.Yaml.Object : GLib.Object
v.set_uchar((uint8)data.data[0]);
break;
case Type.BOOLEAN :
v.set_boolean (bool.parse(data.down ()));
v.set_boolean (data == "1" || data.down () == "true");
break;
case Type.INT :
v.set_int(int.parse(data));

View File

@ -86,11 +86,6 @@ public class Pluie.Yaml.Processor
*/
Gee.HashMap<string, Yaml.Node> anchors { get; internal set; }
/**
* Tags map definition
*/
Gee.HashMap<string, string> tags { get; internal set; }
/**
* Error event
*/
@ -104,7 +99,7 @@ public class Pluie.Yaml.Processor
/**
* the root Yaml.Node
*/
public Yaml.Node root;
public Yaml.Root root;
/**
* current previous Yaml.Node
@ -133,7 +128,6 @@ public class Pluie.Yaml.Processor
{
this.events = new Gee.ArrayList<Yaml.Event>();
this.anchors = new Gee.HashMap<string, Yaml.Node>();
this.tags = new Gee.HashMap<string, string>();
}
/**
@ -214,8 +208,7 @@ public class Pluie.Yaml.Processor
*/
private void reset ()
{
this.root = new Yaml.Mapping (null, "PluieYamlRoot");
this.root.ntype = Yaml.NODE_TYPE.ROOT;
this.root = new Yaml.Root ();
this.prev_node = this.root;
this.parent_node = this.root;
this.iterator = this.events.iterator ();
@ -273,7 +266,7 @@ public class Pluie.Yaml.Processor
{
if (Yaml.Scanner.DEBUG)
of.action ("on_tag_directive %s".printf (this.event.data["handle"]), this.event.data["prefix"]);
this.tags[this.event.data["handle"]] = this.event.data["prefix"];
this.root.tag_directives[this.event.data["handle"]] = this.event.data["prefix"];
}
/**
@ -318,8 +311,8 @@ public class Pluie.Yaml.Processor
if (this.event.evtype.is_tag ()) {
if (Yaml.Scanner.DEBUG)
of.keyval ("tag %s".printf (this.event.data["handle"]), this.event.data["suffix"]);
if (this.tags.has_key (this.event.data["handle"])) {
var tag = new Yaml.Tag (this.event.data["suffix"], this.tags[this.event.data["handle"]]);
if (this.root.tag_directives.has_key (this.event.data["handle"])) {
var tag = new Yaml.Tag (this.event.data["suffix"], this.event.data["handle"].replace("!", ""));
if (onKey)
this.keyTag = tag;
else

View File

@ -0,0 +1,72 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* @software : lib-yaml <https://git.pluie.org/pluie/lib-yaml>
* @version : 0.4
* @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 Pluie;
using Gee;
/**
* a class representing a mapping node
*/
public class Pluie.Yaml.Root : Yaml.Mapping
{
/**
* Tags map definition
*/
public Gee.HashMap<string, string> tag_directives { get; internal set; }
/**
*
*/
public Root (string? name = "PluieYamlRoot")
{
base (null, name);
this.ntype = Yaml.NODE_TYPE.ROOT;
this.tag_directives = new Gee.HashMap<string, string> ();
}
/**
*
*/
public string get_display_tag_directives ()
{
var sb = new StringBuilder();
foreach (var entry in this.tag_directives.entries) {
int len = 10 - entry.key.length -2;
var str = " %TAG "+@" %$(len)s"+" %s %s";
sb.append (
"\n %s %s".printf (
of.c(ECHO.TITLE).s (str.printf (" ", entry.key.replace("!", ""), Color.off ())),
of.c (ECHO.DEFAULT).s (entry.value))
);
}
return sb.str;
}
}

View File

@ -0,0 +1,45 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* @software : lib-yaml <https://git.pluie.org/pluie/lib-yaml>
* @version : 0.4
* @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/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
/**
* a test class to implements yamlize
*/
public class Pluie.Yaml.SubExample : Yaml.Object
{
public string toto { get; set; }
public string tata { get; set; }
public int titi { get; set; }
public bool tutu { get; set; }
public void method_a ()
{
of.echo (" called method from object %s builded via yaml".printf (this.type_from_self ()));
}
}

View File

@ -32,11 +32,13 @@ namespace Pluie
namespace Yaml
{
public static bool DBG_SHOW_INDENT = true;
public static bool DBG_SHOW_PARENT = true;
public static bool DBG_SHOW_PARENT = false;
public static bool DBG_SHOW_UUID = true;
public static bool DBG_SHOW_LEVEL = true;
public static bool DBG_SHOW_REF = true;
public static bool DBG_SHOW_LEVEL = false;
public static bool DBG_SHOW_REF = false;
public static bool DBG_SHOW_COUNT = true;
public static bool DBG_SHOW_TAG = true;
public static bool DBG_SHOW_TYPE = true;
/**
* ParseError