From a3ba7001401ffea31e6282c954c832840b1df200 Mon Sep 17 00:00:00 2001 From: a-sansara Date: Wed, 8 Aug 2018 18:01:10 +0200 Subject: [PATCH] begin tag impl --- README.md | 10 ++++ meson.build | 2 + resources/config/db.yml | 2 + samples/yamlize.vala | 80 ++++++++++++++++++++++++++ src/vala/Pluie/Db.Profile.vala | 44 ++++++++++++++ src/vala/Pluie/Yaml.AbstractChild.vala | 5 ++ src/vala/Pluie/Yaml.AbstractNode.vala | 2 +- src/vala/Pluie/Yaml.Finder.vala | 2 +- src/vala/Pluie/Yaml.Node.vala | 3 +- src/vala/Pluie/Yaml.Object.vala | 72 +++++++++++++++++++++++ src/vala/Pluie/Yaml.Processor.vala | 25 ++++++++ src/vala/Pluie/Yaml.global.vala | 8 +++ 12 files changed, 252 insertions(+), 3 deletions(-) create mode 100644 samples/yamlize.vala create mode 100644 src/vala/Pluie/Db.Profile.vala create mode 100644 src/vala/Pluie/Yaml.Object.vala diff --git a/README.md b/README.md index bbde228..b2ebaa3 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,16 @@ vala code : #### via iterator +```vala + var config = new Yaml.Config (path); + var root = config.root_node (); + foreach (var child in root) { + of.echo (child.to_string ()); + } +``` + +or + ```vala var config = new Yaml.Config (path); var root = config.root_node (); diff --git a/meson.build b/meson.build index e6f756c..24ad614 100644 --- a/meson.build +++ b/meson.build @@ -60,6 +60,7 @@ 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', @@ -70,6 +71,7 @@ sources = [ '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.Scalar.vala', 'src/vala/Pluie/Yaml.Scanner.vala', 'src/vala/Pluie/Yaml.Sequence.vala', diff --git a/resources/config/db.yml b/resources/config/db.yml index 9e42ab6..f82cfa5 100644 --- a/resources/config/db.yml +++ b/resources/config/db.yml @@ -5,6 +5,7 @@ bo : user : dev password : mysql charset : utf8 + port : 3306 therapy : driver : mysql @@ -13,3 +14,4 @@ therapy : user : dev password : mysql charset : utf8 + port : !!int 3306 diff --git a/samples/yamlize.vala b/samples/yamlize.vala new file mode 100644 index 0000000..6780896 --- /dev/null +++ b/samples/yamlize.vala @@ -0,0 +1,80 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * + * @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 . + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ + +using GLib; +using Gee; +using Pluie; + +int main (string[] args) +{ + Echo.init(false); + + var path = Yaml.DATA_PATH + "/config/db.yml"; + var done = false; + + of.title ("Pluie Yaml Library", Pluie.Yaml.VERSION, "a-sansara"); + Pluie.Yaml.Scanner.DEBUG = false; + var config = new Yaml.Config (path, true); + Yaml.Node root = (Yaml.Node) config.root_node (); + Gee.HashMap db = 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 ()); + } + } + } + } + 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)); + + + of.rs (done); + of.echo (); + return (int) done; + +} diff --git a/src/vala/Pluie/Db.Profile.vala b/src/vala/Pluie/Db.Profile.vala new file mode 100644 index 0000000..9b77c39 --- /dev/null +++ b/src/vala/Pluie/Db.Profile.vala @@ -0,0 +1,44 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * + * @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.Db.Profile : Yaml.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; } + +} diff --git a/src/vala/Pluie/Yaml.AbstractChild.vala b/src/vala/Pluie/Yaml.AbstractChild.vala index 64287a2..1935289 100644 --- a/src/vala/Pluie/Yaml.AbstractChild.vala +++ b/src/vala/Pluie/Yaml.AbstractChild.vala @@ -51,6 +51,11 @@ public abstract class Pluie.Yaml.AbstractChild : Yaml.AbstractNode */ public string? anchor { get; internal set; default = null; } + /** + * + */ + public string? tag { get; internal set; default = null; } + /** * default Yaml.Node constructor * @param parent the parent node diff --git a/src/vala/Pluie/Yaml.AbstractNode.vala b/src/vala/Pluie/Yaml.AbstractNode.vala index 04a5291..fc3f987 100644 --- a/src/vala/Pluie/Yaml.AbstractNode.vala +++ b/src/vala/Pluie/Yaml.AbstractNode.vala @@ -34,7 +34,7 @@ using Pluie; /** * abstract class representing a node */ -public abstract class Pluie.Yaml.AbstractNode : Object +public abstract class Pluie.Yaml.AbstractNode : GLib.Object { /** * universal unique identifier diff --git a/src/vala/Pluie/Yaml.Finder.vala b/src/vala/Pluie/Yaml.Finder.vala index 35b2787..4dcccab 100644 --- a/src/vala/Pluie/Yaml.Finder.vala +++ b/src/vala/Pluie/Yaml.Finder.vala @@ -34,7 +34,7 @@ using Pluie; /** * Finder class used to easily retriew Yaml.Node */ -public class Pluie.Yaml.Finder : Object +public class Pluie.Yaml.Finder : GLib.Object { /** diff --git a/src/vala/Pluie/Yaml.Node.vala b/src/vala/Pluie/Yaml.Node.vala index 5e889d5..57f26c8 100644 --- a/src/vala/Pluie/Yaml.Node.vala +++ b/src/vala/Pluie/Yaml.Node.vala @@ -273,7 +273,7 @@ public class Pluie.Yaml.Node : Yaml.AbstractChild, Pluie.Yaml.Collection */ 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) { - return "%s%s%s%s%s%s%s%s%s".printf ( + return "%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 () @@ -294,6 +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)) : "", //~ 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 new file mode 100644 index 0000000..b17885c --- /dev/null +++ b/src/vala/Pluie/Yaml.Object.vala @@ -0,0 +1,72 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * + * @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 . + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ +using GLib; +using Gee; + +/** + * a test class to implements yamlize + + */ +public abstract class Pluie.Yaml.Object : GLib.Object +{ + /** + * + */ + public bool yamlize (Yaml.Node node) + { + bool done = false; + try { + 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); + } + } + } + } + } + } + catch (GLib.Error e) { + of.warn (e.message); + done = false; + } + done = true; + return done; + } +} diff --git a/src/vala/Pluie/Yaml.Processor.vala b/src/vala/Pluie/Yaml.Processor.vala index c133b95..0ac5c28 100644 --- a/src/vala/Pluie/Yaml.Processor.vala +++ b/src/vala/Pluie/Yaml.Processor.vala @@ -61,6 +61,16 @@ public class Pluie.Yaml.Processor */ string? ckey; + /** + * current tag suffix + */ + string? tagSuffix; + + /** + * current tag handle + */ + string? tagHandle; + /** * Events list */ @@ -198,6 +208,8 @@ public class Pluie.Yaml.Processor this.change = false; this.ckey = null; this.idAnchor = null; + this.tagHandle = null; + this.tagSuffix = null; this.beginFlowSeq = false; } @@ -285,6 +297,12 @@ 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 (); + } if (this.event.evtype.is_scalar ()) { this.on_scalar (); } @@ -389,6 +407,11 @@ public class Pluie.Yaml.Processor private void on_update () { if (this.change) { + if (this.tagSuffix != null) { + of.action ("setting tag"); + this.node.tag = this.tagSuffix; + of.echo (this.node.to_string ()); + } if (this.node.ntype.is_collection () && (this.node.empty() || (!this.node.first().ntype.is_scalar ()))) { this.parent_node = this.node; } @@ -396,6 +419,8 @@ public class Pluie.Yaml.Processor this.parent_node = this.node.parent; } this.prev_node = this.node; + this.tagHandle = null; + this.tagSuffix = null; this.node = null; this.change = false; } diff --git a/src/vala/Pluie/Yaml.global.vala b/src/vala/Pluie/Yaml.global.vala index f8e6036..4ec6da2 100644 --- a/src/vala/Pluie/Yaml.global.vala +++ b/src/vala/Pluie/Yaml.global.vala @@ -124,6 +124,14 @@ namespace Pluie return this == EVT.ALIAS; } + /** + * @return event is tag + */ + public bool is_tag () + { + return this == EVT.TAG; + } + /** * @return event is key */