suite tag impl

This commit is contained in:
a-Sansara 2018-08-09 03:42:36 +02:00
parent e98c7c070f
commit 7bb36efe51
14 changed files with 240 additions and 74 deletions

View File

@ -250,6 +250,6 @@ see samples files in ./samples directory
* ~~rewrite nodes classes~~ * ~~rewrite nodes classes~~
* ~~put doc online~~ * ~~put doc online~~
* ~~add docker image~~ * ~~add docker image~~
* manage tag directives & tag
* improve doc * improve doc
* dumper * dumper
* manage tag directives & tag

View File

@ -60,26 +60,28 @@ sources = [
'build/install.vala', 'build/install.vala',
'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/Db.Profile.vala',
'src/vala/Pluie/Yaml.global.vala', 'src/vala/Pluie/Yaml.global.vala',
'src/vala/Pluie/Yaml.AbstractChild.vala', 'src/vala/Pluie/Yaml.AbstractChild.vala',
'src/vala/Pluie/Yaml.AbstractNode.vala', 'src/vala/Pluie/Yaml.AbstractNode.vala',
'src/vala/Pluie/Yaml.Collection.vala', 'src/vala/Pluie/Yaml.Collection.vala',
'src/vala/Pluie/Yaml.Config.vala', 'src/vala/Pluie/Yaml.Config.vala',
'src/vala/Pluie/Yaml.Event.vala', 'src/vala/Pluie/Yaml.Event.vala',
'src/vala/Pluie/Yaml.Example.vala',
'src/vala/Pluie/Yaml.Finder.vala', 'src/vala/Pluie/Yaml.Finder.vala',
'src/vala/Pluie/Yaml.Loader.vala', 'src/vala/Pluie/Yaml.Loader.vala',
'src/vala/Pluie/Yaml.Mapping.vala', 'src/vala/Pluie/Yaml.Mapping.vala',
'src/vala/Pluie/Yaml.Node.vala', 'src/vala/Pluie/Yaml.Node.vala',
'src/vala/Pluie/Yaml.Object.vala', 'src/vala/Pluie/Yaml.Object.vala',
'src/vala/Pluie/Yaml.Processor.vala',
'src/vala/Pluie/Yaml.Scalar.vala', 'src/vala/Pluie/Yaml.Scalar.vala',
'src/vala/Pluie/Yaml.Scanner.vala', 'src/vala/Pluie/Yaml.Scanner.vala',
'src/vala/Pluie/Yaml.Sequence.vala', 'src/vala/Pluie/Yaml.Sequence.vala',
'src/vala/Pluie/Yaml.Processor.vala', 'src/vala/Pluie/Yaml.Tag.vala',
'src/c/yaml.c' 'src/c/yaml.c'
] ]
install_data('resources/main.yml', install_dir : datadir) install_data('resources/main.yml', install_dir : datadir)
install_data('resources/tag.yml', install_dir : datadir)
install_data('resources/test.yml', install_dir : datadir) install_data('resources/test.yml', install_dir : datadir)
install_data('resources/test.json', install_dir : datadir) install_data('resources/test.json', install_dir : datadir)
install_data('resources/config/db.yml', install_dir : join_paths(datadir, 'config')) install_data('resources/config/db.yml', install_dir : join_paths(datadir, 'config'))

View File

@ -14,4 +14,4 @@ therapy :
user : dev user : dev
password : mysql password : mysql
charset : utf8 charset : utf8
port : !!int 3306 port : 3306

15
resources/tag.yml Normal file
View File

@ -0,0 +1,15 @@
%YAML 1.2
%TAG !yaml! tag:yaml.org,2002:
%TAG !vala! tag:pluie.org,2018:vala/
---
!vala!Pluie.Yaml.Example test1 :
type_int : !vala!int 3306
type_bool : !vala!bool false
type_char : !vala!char c
!vala!Pluie.Yaml.Example test2 :
type_int : !vala!int 3306
type_bool : !vala!bool true
type_char : !vala!char g

View File

@ -35,43 +35,33 @@ int main (string[] args)
{ {
Echo.init(false); Echo.init(false);
var path = Yaml.DATA_PATH + "/config/db.yml"; var path = Yaml.DATA_PATH + "/tag.yml";
var done = false; var done = false;
of.title ("Pluie Yaml Library", Pluie.Yaml.VERSION, "a-sansara"); of.title ("Pluie Yaml Library", Pluie.Yaml.VERSION, "a-sansara");
Pluie.Yaml.Scanner.DEBUG = false; Pluie.Yaml.Scanner.DEBUG = true;
var config = new Yaml.Config (path, true); var config = new Yaml.Config (path, true);
Yaml.Node root = (Yaml.Node) config.root_node (); Yaml.Node root = (Yaml.Node) config.root_node ();
Gee.HashMap<string, Db.Profile> db = new Gee.HashMap<string, Db.Profile> (); root.display_childs ();
Gee.HashMap<string, Yaml.Example> list = new Gee.HashMap<string, Yaml.Example> ();
if ((done = root != null)) { if ((done = root != null)) {
foreach (var node in root) { foreach (var node in root) {
of.action ("Yamlize DB profile", node.name); of.action ("Yamlize Yaml.Example", node.name);
db[node.name] = new Db.Profile (); of.echo (node.to_string ());
if (db[node.name].yamlize (node)) { if (node.tag != null && node.tag.@value == "Pluie.Yaml.Example") {
foreach (var p in db[node.name].get_class().list_properties ()) { list[node.name] = new Yaml.Example ();
var g = (node as Yaml.Mapping).item (p.name); of.state (list[node.name].yamlize (node));
if (g.tag == null) {
var v = null;
db[node.name].get(p.name, &v);
of.keyval (p.name, v != null ? v : "null");
}
else {
//~ of.echo ("tag is %s".printf (g.tag));
if (g.tag == "int") {
int z = -1;
db[node.name].get(p.name, ref z);
of.keyval (p.name, z.to_string ());
}
}
}
} }
node = node.next_sibling (); node = node.next_sibling ();
} }
} }
of.echo ("param [%s] port as int %d".printf ("bo", db["bo"].port)); foreach (var entry in list.entries) {
of.echo ("param [%s] port as int %d".printf ("therapy", db["therapy"].port)); of.action ("Getting values", entry.key);
of.keyval("type_int" , "%d".printf(entry.value.type_int));
of.keyval("type_bool", "%s".printf(entry.value.type_bool.to_string ()));
of.keyval("type_char", "%c".printf(entry.value.type_char));
}
of.rs (done); of.rs (done);
of.echo (); of.echo ();

View File

@ -100,6 +100,11 @@ void yaml_parse_file(const char *srcPath, const char *destPath)
fprintf(wh, "%lu, %d, %d, %d\n", line, token.type, token.data.version_directive.major, token.data.version_directive.minor); fprintf(wh, "%lu, %d, %d, %d\n", line, token.type, token.data.version_directive.major, token.data.version_directive.minor);
break; break;
case YAML_TAG_DIRECTIVE_TOKEN :
printf ("YAML_TAG_DIRECTIVE_TOKEN : handle : %s, prefix : %s\n", token.data.tag_directive.handle, token.data.tag_directive.prefix);
fprintf(wh, "%lu, %d, \"%s\", \"%s\"\n", line, token.type, token.data.tag_directive.handle, token.data.tag_directive.prefix);
break;
case YAML_NO_TOKEN : case YAML_NO_TOKEN :
fprintf(wh, "%lu, %d, \"%s\"\n", line, token.type, escape_string((char *)parser.problem)); fprintf(wh, "%lu, %d, \"%s\"\n", line, token.type, escape_string((char *)parser.problem));
stop = 1; stop = 1;

View File

@ -54,7 +54,7 @@ public abstract class Pluie.Yaml.AbstractChild : Yaml.AbstractNode
/** /**
* *
*/ */
public string? tag { get; internal set; default = null; } public Yaml.Tag? tag { get; internal set; default = null; }
/** /**
* default Yaml.Node constructor * default Yaml.Node constructor

View File

@ -0,0 +1,60 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* @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.Example : Yaml.Object
{
public string type_string { get; set; }
public int type_int { get; set; }
public uint type_uint { get; set; }
public float type_float { get; set; }
public double type_double { get; set; }
public char type_char { get; set; }
public uchar type_uchar { get; set; }
public unichar type_unichar { get; set; }
public short type_short { get; set; }
public ushort type_ushort { get; set; }
public long type_long { get; set; }
public ulong type_ulong { get; set; }
public size_t type_size_t { get; set; }
public ssize_t type_ssize_t { get; set; }
public int8 type_int8 { get; set; }
public uint8 type_uint8 { get; set; }
public int16 type_int16 { get; set; }
public uint16 type_uint16 { get; set; }
public int32 type_int32 { get; set; }
public uint32 type_uint32 { get; set; }
public int64 type_int64 { get; set; }
public uint64 type_uint64 { get; set; }
public bool type_bool { get; set; }
}

View File

@ -40,7 +40,7 @@ public class Pluie.Yaml.Node : Yaml.AbstractChild, Pluie.Yaml.Collection
*/ */
public ArrayList<Yaml.Node> list { get; internal set; } public ArrayList<Yaml.Node> list { get; internal set; }
bool container { get; internal set; default = true; } bool container { get; internal set; default = true; }
/** /**
* default Yaml.Node constructor * default Yaml.Node constructor
@ -294,7 +294,7 @@ public class Pluie.Yaml.Node : Yaml.AbstractChild, Pluie.Yaml.Collection
)), )),
withCount ? of.c (ECHO.MICROTIME).s (" %d".printf(this.count ())) : "", withCount ? of.c (ECHO.MICROTIME).s (" %d".printf(this.count ())) : "",
withUuid ? of.c (ECHO.COMMENT).s (" %s".printf(this.uuid[0:8]+"...")) : "", 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)) : "", 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.NUM).s ("%d".printf (this.level)),
of.c (ECHO.OPTION).s ("]") of.c (ECHO.OPTION).s ("]")
); );

View File

@ -45,19 +45,27 @@ public abstract class Pluie.Yaml.Object : GLib.Object
if (node!= null && !node.empty ()) { if (node!= null && !node.empty ()) {
Iterator<Yaml.Node> it = node.iterator (); Iterator<Yaml.Node> it = node.iterator ();
foreach (var child in node) { foreach (var child in node) {
foreach (var p in this.get_class ().list_properties ()) { of.action ("yamlize ", child.to_string ());
if (p.name == child.name) { var pspec = this.get_class ().find_property (child.name);
if (child.tag != null) { if (pspec != null) {
switch (child.tag) { if (child.first ().tag != null) {
case "int" : of.keyval ("found tag", child.first ().tag.@value);
this.set (p.name, int.parse(child.first ().data)); //~ of.keyval ("value is `%s`", child.first ().data);
break; switch (child.first ().tag.@value) {
} case "char" :
} this.set (child.name, child.first ().data[0]);
else { break;
this.set (p.name, child.first ().data); case "bool" :
this.set (child.name, bool.parse(child.first ().data.down ()));
break;
case "int" :
this.set (child.name, int.parse(child.first ().data));
break;
} }
} }
else {
this.set (child.name, child.first ().data);
}
} }
} }
} }

View File

@ -62,15 +62,20 @@ public class Pluie.Yaml.Processor
string? ckey; string? ckey;
/** /**
* current tag suffix * current key tag
*/ */
string? tagSuffix; Yaml.Tag? keyTag;
/**
* current key tag
*/
Yaml.Tag? valueTag;
/** /**
* current tag handle * current tag handle
*/ */
string? tagHandle; string? tagHandle;
/** /**
* Events list * Events list
*/ */
@ -81,6 +86,11 @@ public class Pluie.Yaml.Processor
*/ */
Gee.HashMap<string, Yaml.Node> anchors { get; internal set; } Gee.HashMap<string, Yaml.Node> anchors { get; internal set; }
/**
* Tags map definition
*/
Gee.HashMap<string, string> tags { get; internal set; }
/** /**
* Error event * Error event
*/ */
@ -123,6 +133,7 @@ public class Pluie.Yaml.Processor
{ {
this.events = new Gee.ArrayList<Yaml.Event>(); this.events = new Gee.ArrayList<Yaml.Event>();
this.anchors = new Gee.HashMap<string, Yaml.Node>(); this.anchors = new Gee.HashMap<string, Yaml.Node>();
this.tags = new Gee.HashMap<string, string>();
} }
/** /**
@ -160,6 +171,9 @@ public class Pluie.Yaml.Processor
this.reset (); this.reset ();
for (var has_next = this.iterator.next (); has_next; has_next = this.iterator.next ()) { for (var has_next = this.iterator.next (); has_next; has_next = this.iterator.next ()) {
this.event = this.iterator.get (); this.event = this.iterator.get ();
if (this.event.evtype.is_tag_directive ()) {
this.on_tag_directive ();
}
if (this.event.evtype.is_error ()) { if (this.event.evtype.is_error ()) {
this.on_error (); this.on_error ();
break; break;
@ -209,7 +223,8 @@ public class Pluie.Yaml.Processor
this.ckey = null; this.ckey = null;
this.idAnchor = null; this.idAnchor = null;
this.tagHandle = null; this.tagHandle = null;
this.tagSuffix = null; this.keyTag = null;
this.valueTag = null;
this.beginFlowSeq = false; this.beginFlowSeq = false;
} }
@ -251,6 +266,15 @@ public class Pluie.Yaml.Processor
return evt; return evt;
} }
/**
*
*/
private void on_tag_directive ()
{
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"];
}
/** /**
* *
*/ */
@ -284,11 +308,34 @@ public class Pluie.Yaml.Processor
} }
} }
/**
*
*/
private void on_tag (bool onKey = false)
{
if (this.event.evtype.is_tag ()) {
of.keyval ("tag %s".printf (this.event.data["handle"]), this.event.data["suffix"]);
if (this.tags.contains (this.event.data["handle"])) {
var tag = new Yaml.Tag (this.event.data["suffix"], this.tags[this.event.data["handle"]]);
if (onKey)
this.keyTag = tag;
else
this.valueTag = tag;
this.event = this.next_event ();
}
else {
of.warn ("tag handle %s not found in directive".printf (this.event.data["handle"]));
}
}
}
/** /**
* *
*/ */
private void on_key () private void on_key ()
{ {
this.on_tag (true);
this.ckey = this.event.data["data"]; this.ckey = this.event.data["data"];
} }
@ -297,12 +344,7 @@ public class Pluie.Yaml.Processor
*/ */
private void on_value () private void on_value ()
{ {
if (this.event.evtype.is_tag ()) { this.on_tag (false);
of.keyval ("tag", this.event.evtype.to_string ());
this.tagSuffix = this.event.data["suffix"];
this.tagHandle = this.event.data["handle"];
this.event = this.next_event ();
}
if (this.event.evtype.is_scalar ()) { if (this.event.evtype.is_scalar ()) {
this.on_scalar (); this.on_scalar ();
} }
@ -406,11 +448,22 @@ public class Pluie.Yaml.Processor
*/ */
private void on_update () private void on_update ()
{ {
if (this.node != null) {
of.echo (this.node.name);
}
if (this.change) { if (this.change) {
if (this.tagSuffix != null) { of.action ("on change", this.node.name);
of.action ("setting tag"); if (this.keyTag != null) {
this.node.tag = this.tagSuffix; of.action ("setting tag", this.keyTag.@value);
of.echo (this.node.to_string ()); this.node.tag = this.keyTag;
}
else if (this.valueTag != null) {
if (this.node.ntype.is_scalar ()) {
this.node.tag = this.valueTag;
}
else if (!this.node.empty () && this.node.first().ntype.is_scalar ()) {
this.node.first ().tag = this.valueTag;
}
} }
if (this.node.ntype.is_collection () && (this.node.empty() || (!this.node.first().ntype.is_scalar ()))) { if (this.node.ntype.is_collection () && (this.node.empty() || (!this.node.first().ntype.is_scalar ()))) {
this.parent_node = this.node; this.parent_node = this.node;
@ -420,7 +473,8 @@ public class Pluie.Yaml.Processor
} }
this.prev_node = this.node; this.prev_node = this.node;
this.tagHandle = null; this.tagHandle = null;
this.tagSuffix = null; this.keyTag = null;
this.valueTag = null;
this.node = null; this.node = null;
this.change = false; this.change = false;
} }

View File

@ -187,6 +187,25 @@ public class Pluie.Yaml.Scanner
this.processor.events.add(new Yaml.Event(EVT.VERSION_DIRECTIVE, line, null, data)); this.processor.events.add(new Yaml.Event(EVT.VERSION_DIRECTIVE, line, null, data));
} }
/**
* register event tag
* @param evtdata the current data event
* @param line the current line
* @throws GLib.RegexError
*/
private void register_event_tag_directive(string evtdata, int line) throws GLib.RegexError
{
MatchInfo mi = null;
Regex reg = new Regex (REG_TAG);
HashMap<string, string>? data = null;
if (reg.match (evtdata, 0, out mi)) {
data = new HashMap<string, string>();
data.set("handle", mi.fetch (MIEVT_TAG.HANDLE));
data.set("prefix", mi.fetch (MIEVT_TAG.SUFFIX));
}
this.processor.events.add(new Yaml.Event(EVT.TAG_DIRECTIVE, line, null, data));
}
/** /**
* register event tag * register event tag
* @param evtdata the current data event * @param evtdata the current data event
@ -299,25 +318,28 @@ public class Pluie.Yaml.Scanner
string evtdata = mi.fetch (MIEVT.DATA); string evtdata = mi.fetch (MIEVT.DATA);
switch(type) { switch(type) {
case EVT.SCALAR : case EVT.SCALAR :
this.register_event_scalar(evtdata, line); this.register_event_scalar (evtdata, line);
break; break;
case EVT.ANCHOR : case EVT.ANCHOR :
this.register_event_anchor(evtdata, line); this.register_event_anchor (evtdata, line);
break; break;
case EVT.ALIAS : case EVT.ALIAS :
this.register_event_alias(evtdata, line); this.register_event_alias (evtdata, line);
break; break;
case EVT.TAG : case EVT.TAG :
this.register_event_tag(evtdata, line); this.register_event_tag (evtdata, line);
break;
case EVT.TAG_DIRECTIVE :
this.register_event_tag_directive (evtdata, line);
break; break;
case EVT.VERSION_DIRECTIVE : case EVT.VERSION_DIRECTIVE :
this.register_event_version(evtdata, line); this.register_event_version (evtdata, line);
break; break;
case EVT.NONE : case EVT.NONE :
this.register_event_error(evtdata, line); this.register_event_error (evtdata, line);
break; break;
default : default :
this.processor.events.add(new Yaml.Event((Yaml.EVT)type, line, null, null)); this.processor.events.add(new Yaml.Event ((Yaml.EVT) type, line, null, null));
break; break;
} }
} }

View File

@ -26,19 +26,21 @@
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/ */
using GLib;
using Gee;
/** /**
* a test class to implements yamlize * a test class to implements yamlize
*/ */
public class Pluie.Db.Profile : Yaml.Object public class Pluie.Yaml.Tag : GLib.Object
{ {
public string driver { get; set; } public string handle { get; internal set; }
public string host { get; set; } public string @value { get; internal set; }
public string dbname { get; set; }
public string user { get; set; } public Tag (string @value, string handle = "")
public string password { get; set; } {
public string charset { get; set; } this.@value = @value;
public int port { get; set; } this.handle = handle;
}
} }

View File

@ -132,6 +132,14 @@ namespace Pluie
return this == EVT.TAG; return this == EVT.TAG;
} }
/**
* @return event is tag
*/
public bool is_tag_directive ()
{
return this == EVT.TAG_DIRECTIVE;
}
/** /**
* @return event is key * @return event is key
*/ */