From 47bff1c8a3defeabdf18453142cfb7ac5c4b2440 Mon Sep 17 00:00:00 2001 From: Ralph Amissah Date: Sun, 1 Aug 2021 07:07:38 -0400 Subject: ext_depends updates: d-yaml & d2sqlite3 --- src/ext_depends/D-YAML/source/dyaml/node.d | 105 +++++++++++++++++++++++++++-- 1 file changed, 100 insertions(+), 5 deletions(-) (limited to 'src/ext_depends/D-YAML/source') diff --git a/src/ext_depends/D-YAML/source/dyaml/node.d b/src/ext_depends/D-YAML/source/dyaml/node.d index 24a62a4..5cb318e 100644 --- a/src/ext_depends/D-YAML/source/dyaml/node.d +++ b/src/ext_depends/D-YAML/source/dyaml/node.d @@ -498,17 +498,17 @@ struct Node * the value is out of range of requested type. */ inout(T) get(T, Flag!"stringConversion" stringConversion = Yes.stringConversion)() inout - if (allowed!(Unqual!T) || hasNodeConstructor!(Unqual!T)) + if (allowed!(Unqual!T) || hasNodeConstructor!(inout(Unqual!T)) || (!hasIndirections!(Unqual!T) && hasNodeConstructor!(Unqual!T))) { if(isType!(Unqual!T)){return getValue!T;} static if(!allowed!(Unqual!T)) { - static if (hasSimpleNodeConstructor!T) + static if (hasSimpleNodeConstructor!(Unqual!T) || hasSimpleNodeConstructor!(inout(Unqual!T))) { alias params = AliasSeq!(this); } - else static if (hasExpandedNodeConstructor!T) + else static if (hasExpandedNodeConstructor!(Unqual!T) || hasExpandedNodeConstructor!(inout(Unqual!T))) { alias params = AliasSeq!(this, tag_); } @@ -592,6 +592,35 @@ struct Node ". Expected: " ~ typeid(T).toString, startMark_); } } + /// ditto + T get(T)() const + if (hasIndirections!(Unqual!T) && hasNodeConstructor!(Unqual!T) && (!hasNodeConstructor!(inout(Unqual!T)))) + { + static if (hasSimpleNodeConstructor!T) + { + alias params = AliasSeq!(this); + } + else static if (hasExpandedNodeConstructor!T) + { + alias params = AliasSeq!(this, tag_); + } + else + { + static assert(0, "Unknown Node constructor?"); + } + static if (is(T == class)) + { + return new T(params); + } + else static if (is(T == struct)) + { + return T(params); + } + else + { + static assert(0, "Unhandled user type"); + } + } /// Automatic type conversion @safe unittest { @@ -2469,7 +2498,7 @@ template hasSimpleNodeConstructor(T) } else static if (is(T == class)) { - enum hasSimpleNodeConstructor = is(typeof(new inout T(Node.init))); + enum hasSimpleNodeConstructor = is(typeof(new T(Node.init))); } else enum hasSimpleNodeConstructor = false; } @@ -2481,8 +2510,74 @@ template hasExpandedNodeConstructor(T) } else static if (is(T == class)) { - enum hasExpandedNodeConstructor = is(typeof(new inout T(Node.init, ""))); + enum hasExpandedNodeConstructor = is(typeof(new T(Node.init, ""))); } else enum hasExpandedNodeConstructor = false; } enum castableToNode(T) = (is(T == struct) || is(T == class)) && is(typeof(T.opCast!Node()) : Node); + +@safe unittest +{ + import dyaml : Loader, Node; + + static struct Foo + { + string[] bars; + + this(const Node node) + { + foreach(value; node["bars"].sequence) + { + bars ~= value.as!string; + } + } + } + + Loader.fromString(`{ bars: ["a", "b"] }`) + .load + .as!(Foo); +} +@safe unittest +{ + import dyaml : Loader, Node; + import std : split, to; + + static class MyClass + { + int x, y, z; + + this(Node node) + { + auto parts = node.as!string().split(":"); + x = parts[0].to!int; + y = parts[1].to!int; + z = parts[2].to!int; + } + } + + auto loader = Loader.fromString(`"1:2:3"`); + Node node = loader.load(); + auto mc = node.get!MyClass; +} +@safe unittest +{ + import dyaml : Loader, Node; + import std : split, to; + + static class MyClass + { + int x, y, z; + + this(Node node) + { + auto parts = node.as!string().split(":"); + x = parts[0].to!int; + y = parts[1].to!int; + z = parts[2].to!int; + } + } + + auto loader = Loader.fromString(`"1:2:3"`); + const node = loader.load(); + auto mc = node.get!MyClass; +} -- cgit v1.2.3