diff --git a/README.md b/README.md index 30c03c3..6dafa8a 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,6 @@ see samples files in ./samples directory * ~~rewrite nodes classes~~ * ~~put doc online~~ * ~~add docker image~~ +* manage tag directives & tag * improve doc * dumper -* manage tag directives & tag diff --git a/meson.build b/meson.build index 24ad614..3f34509 100644 --- a/meson.build +++ b/meson.build @@ -60,26 +60,28 @@ sources = [ 'build/install.vala', 'src/vala/Pluie/Io.Reader.vala', 'src/vala/Pluie/Io.StreamLineMark.vala', - 'src/vala/Pluie/Db.Profile.vala', 'src/vala/Pluie/Yaml.global.vala', 'src/vala/Pluie/Yaml.AbstractChild.vala', 'src/vala/Pluie/Yaml.AbstractNode.vala', 'src/vala/Pluie/Yaml.Collection.vala', 'src/vala/Pluie/Yaml.Config.vala', 'src/vala/Pluie/Yaml.Event.vala', + 'src/vala/Pluie/Yaml.Example.vala', 'src/vala/Pluie/Yaml.Finder.vala', 'src/vala/Pluie/Yaml.Loader.vala', 'src/vala/Pluie/Yaml.Mapping.vala', 'src/vala/Pluie/Yaml.Node.vala', 'src/vala/Pluie/Yaml.Object.vala', + 'src/vala/Pluie/Yaml.Processor.vala', 'src/vala/Pluie/Yaml.Scalar.vala', 'src/vala/Pluie/Yaml.Scanner.vala', 'src/vala/Pluie/Yaml.Sequence.vala', - 'src/vala/Pluie/Yaml.Processor.vala', + 'src/vala/Pluie/Yaml.Tag.vala', 'src/c/yaml.c' ] 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.json', install_dir : datadir) install_data('resources/config/db.yml', install_dir : join_paths(datadir, 'config')) diff --git a/resources/config/db.yml b/resources/config/db.yml index f82cfa5..dba7569 100644 --- a/resources/config/db.yml +++ b/resources/config/db.yml @@ -14,4 +14,4 @@ therapy : user : dev password : mysql charset : utf8 - port : !!int 3306 + port : 3306 diff --git a/resources/tag.yml b/resources/tag.yml new file mode 100644 index 0000000..c69c87f --- /dev/null +++ b/resources/tag.yml @@ -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 diff --git a/samples/yamlize.vala b/samples/yamlize.vala index 6780896..b487960 100644 --- a/samples/yamlize.vala +++ b/samples/yamlize.vala @@ -35,43 +35,33 @@ int main (string[] args) { Echo.init(false); - var path = Yaml.DATA_PATH + "/config/db.yml"; + var path = Yaml.DATA_PATH + "/tag.yml"; var done = false; 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); Yaml.Node root = (Yaml.Node) config.root_node (); - Gee.HashMap db = new Gee.HashMap (); + root.display_childs (); + Gee.HashMap list = new Gee.HashMap (); if ((done = root != null)) { foreach (var node in root) { - of.action ("Yamlize DB profile", node.name); - db[node.name] = new Db.Profile (); - if (db[node.name].yamlize (node)) { - foreach (var p in db[node.name].get_class().list_properties ()) { - var g = (node as Yaml.Mapping).item (p.name); - 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 ()); - } - } - } + of.action ("Yamlize Yaml.Example", node.name); + of.echo (node.to_string ()); + if (node.tag != null && node.tag.@value == "Pluie.Yaml.Example") { + list[node.name] = new Yaml.Example (); + of.state (list[node.name].yamlize (node)); } node = node.next_sibling (); } } - of.echo ("param [%s] port as int %d".printf ("bo", db["bo"].port)); - of.echo ("param [%s] port as int %d".printf ("therapy", db["therapy"].port)); - + foreach (var entry in list.entries) { + 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.echo (); diff --git a/src/c/yaml.c b/src/c/yaml.c index b49efd9..b023606 100644 --- a/src/c/yaml.c +++ b/src/c/yaml.c @@ -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); 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 : fprintf(wh, "%lu, %d, \"%s\"\n", line, token.type, escape_string((char *)parser.problem)); stop = 1; diff --git a/src/vala/Pluie/Yaml.AbstractChild.vala b/src/vala/Pluie/Yaml.AbstractChild.vala index 1935289..7a2b182 100644 --- a/src/vala/Pluie/Yaml.AbstractChild.vala +++ b/src/vala/Pluie/Yaml.AbstractChild.vala @@ -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 diff --git a/src/vala/Pluie/Yaml.Example.vala b/src/vala/Pluie/Yaml.Example.vala new file mode 100644 index 0000000..be3aac6 --- /dev/null +++ b/src/vala/Pluie/Yaml.Example.vala @@ -0,0 +1,60 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * + * @software : lib-yaml + * @version : 0.4 + * @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 . + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ + +/** + * 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; } + +} diff --git a/src/vala/Pluie/Yaml.Node.vala b/src/vala/Pluie/Yaml.Node.vala index 57f26c8..916703a 100644 --- a/src/vala/Pluie/Yaml.Node.vala +++ b/src/vala/Pluie/Yaml.Node.vala @@ -40,7 +40,7 @@ public class Pluie.Yaml.Node : Yaml.AbstractChild, Pluie.Yaml.Collection */ public ArrayList list { get; internal set; } - bool container { get; internal set; default = true; } + bool container { get; internal set; default = true; } /** * 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 ())) : "", 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.OPTION).s ("]") ); diff --git a/src/vala/Pluie/Yaml.Object.vala b/src/vala/Pluie/Yaml.Object.vala index b17885c..73a7ea8 100644 --- a/src/vala/Pluie/Yaml.Object.vala +++ b/src/vala/Pluie/Yaml.Object.vala @@ -45,19 +45,27 @@ public abstract class Pluie.Yaml.Object : GLib.Object if (node!= null && !node.empty ()) { Iterator it = node.iterator (); foreach (var child in node) { - foreach (var p in this.get_class ().list_properties ()) { - if (p.name == child.name) { - if (child.tag != null) { - switch (child.tag) { - case "int" : - this.set (p.name, int.parse(child.first ().data)); - break; - } - } - else { - this.set (p.name, child.first ().data); + of.action ("yamlize ", child.to_string ()); + var pspec = this.get_class ().find_property (child.name); + if (pspec != null) { + if (child.first ().tag != null) { + of.keyval ("found tag", child.first ().tag.@value); +//~ of.keyval ("value is `%s`", child.first ().data); + switch (child.first ().tag.@value) { + case "char" : + this.set (child.name, child.first ().data[0]); + break; + 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); + } } } } diff --git a/src/vala/Pluie/Yaml.Processor.vala b/src/vala/Pluie/Yaml.Processor.vala index 0ac5c28..438e7f1 100644 --- a/src/vala/Pluie/Yaml.Processor.vala +++ b/src/vala/Pluie/Yaml.Processor.vala @@ -62,15 +62,20 @@ public class Pluie.Yaml.Processor string? ckey; /** - * current tag suffix + * current key tag */ - string? tagSuffix; + Yaml.Tag? keyTag; + + /** + * current key tag + */ + Yaml.Tag? valueTag; /** * current tag handle */ string? tagHandle; - + /** * Events list */ @@ -81,6 +86,11 @@ public class Pluie.Yaml.Processor */ Gee.HashMap anchors { get; internal set; } + /** + * Tags map definition + */ + Gee.HashMap tags { get; internal set; } + /** * Error event */ @@ -123,6 +133,7 @@ public class Pluie.Yaml.Processor { this.events = new Gee.ArrayList(); this.anchors = new Gee.HashMap(); + this.tags = new Gee.HashMap(); } /** @@ -160,6 +171,9 @@ public class Pluie.Yaml.Processor this.reset (); for (var has_next = this.iterator.next (); has_next; has_next = this.iterator.next ()) { this.event = this.iterator.get (); + if (this.event.evtype.is_tag_directive ()) { + this.on_tag_directive (); + } if (this.event.evtype.is_error ()) { this.on_error (); break; @@ -209,7 +223,8 @@ public class Pluie.Yaml.Processor this.ckey = null; this.idAnchor = null; this.tagHandle = null; - this.tagSuffix = null; + this.keyTag = null; + this.valueTag = null; this.beginFlowSeq = false; } @@ -251,6 +266,15 @@ public class Pluie.Yaml.Processor 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 () { + this.on_tag (true); this.ckey = this.event.data["data"]; } @@ -297,12 +344,7 @@ public class Pluie.Yaml.Processor */ private void on_value () { - if (this.event.evtype.is_tag ()) { - 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 (); - } + this.on_tag (false); if (this.event.evtype.is_scalar ()) { this.on_scalar (); } @@ -406,11 +448,22 @@ public class Pluie.Yaml.Processor */ private void on_update () { + if (this.node != null) { + of.echo (this.node.name); + } if (this.change) { - if (this.tagSuffix != null) { - of.action ("setting tag"); - this.node.tag = this.tagSuffix; - of.echo (this.node.to_string ()); + of.action ("on change", this.node.name); + if (this.keyTag != null) { + of.action ("setting tag", this.keyTag.@value); + 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 ()))) { this.parent_node = this.node; @@ -420,7 +473,8 @@ public class Pluie.Yaml.Processor } this.prev_node = this.node; this.tagHandle = null; - this.tagSuffix = null; + this.keyTag = null; + this.valueTag = null; this.node = null; this.change = false; } diff --git a/src/vala/Pluie/Yaml.Scanner.vala b/src/vala/Pluie/Yaml.Scanner.vala index 19c895b..124b8ed 100644 --- a/src/vala/Pluie/Yaml.Scanner.vala +++ b/src/vala/Pluie/Yaml.Scanner.vala @@ -187,6 +187,25 @@ public class Pluie.Yaml.Scanner 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? data = null; + if (reg.match (evtdata, 0, out mi)) { + data = new HashMap(); + 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 * @param evtdata the current data event @@ -299,25 +318,28 @@ public class Pluie.Yaml.Scanner string evtdata = mi.fetch (MIEVT.DATA); switch(type) { case EVT.SCALAR : - this.register_event_scalar(evtdata, line); + this.register_event_scalar (evtdata, line); break; case EVT.ANCHOR : - this.register_event_anchor(evtdata, line); + this.register_event_anchor (evtdata, line); break; case EVT.ALIAS : - this.register_event_alias(evtdata, line); + this.register_event_alias (evtdata, line); break; 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; case EVT.VERSION_DIRECTIVE : - this.register_event_version(evtdata, line); + this.register_event_version (evtdata, line); break; case EVT.NONE : - this.register_event_error(evtdata, line); + this.register_event_error (evtdata, line); break; 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; } } diff --git a/src/vala/Pluie/Db.Profile.vala b/src/vala/Pluie/Yaml.Tag.vala similarity index 79% rename from src/vala/Pluie/Db.Profile.vala rename to src/vala/Pluie/Yaml.Tag.vala index 9b77c39..81429a0 100644 --- a/src/vala/Pluie/Db.Profile.vala +++ b/src/vala/Pluie/Yaml.Tag.vala @@ -26,19 +26,21 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +using GLib; +using Gee; /** * 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 host { get; set; } - public string dbname { get; set; } - public string user { get; set; } - public string password { get; set; } - public string charset { get; set; } - public int port { get; set; } - + public string handle { get; internal set; } + public string @value { get; internal set; } + + public Tag (string @value, string handle = "") + { + this.@value = @value; + this.handle = handle; + } } diff --git a/src/vala/Pluie/Yaml.global.vala b/src/vala/Pluie/Yaml.global.vala index 4ec6da2..af7ab3b 100644 --- a/src/vala/Pluie/Yaml.global.vala +++ b/src/vala/Pluie/Yaml.global.vala @@ -132,6 +132,14 @@ namespace Pluie return this == EVT.TAG; } + /** + * @return event is tag + */ + public bool is_tag_directive () + { + return this == EVT.TAG_DIRECTIVE; + } + /** * @return event is key */