From 2cd2de44ffcba99e0d5c71312c7ec983d0915442 Mon Sep 17 00:00:00 2001
From: Ferdinand Majerech Example:
+ctor
Constructor function.
+ import std.string;
+
+ import yaml;
+
+ struct MyStruct
+ {
+ int x, y, z;
+ }
+
+ MyStruct constructMyStructScalar(Mark start, Mark end, ref Node node)
+ {
+ //Guaranteed to be string as we construct from scalar.
+ //!mystruct x:y:z
+ auto parts = node.get!string().split(":");
+ try
+ {
+ return MyStruct(to!int(parts[0]), to!int(parts[1]), to!int(parts[2]));
+ }
+ catch(Exception e)
+ {
+ throw new ConstructorException("Could not construct MyStruct: " ~ e.msg,
+ start, end);
+ }
+ }
+
+ void main()
+ {
+ auto loader = Loader("file.yaml");
+ auto constructor = new Constructor;
+ constructor.addConstructorScalar("!mystruct", &constructMyStructScalar);
+ loader.constructor = constructor;
+ Node node = loader.load();
+ }
+
+
Add a constructor function from sequence.
-See Also:Example:
+
import std.string; + + import yaml; + + struct MyStruct + { + int x, y, z; + } + + MyStruct constructMyStructSequence(Mark start, Mark end, ref Node node) + { + //node is guaranteed to be sequence. + //!mystruct [x, y, z] + try + { + return MyStruct(node[0].get!int, node[1].get!int, node[2].get!int); + } + catch(NodeException e) + { + throw new ConstructorException("Could not construct MyStruct: " ~ e.msg, + start, end); + } + } + + void main() + { + auto loader = Loader("file.yaml"); + auto constructor = new Constructor; + constructor.addConstructorSequence("!mystruct", &constructMyStructSequence); + loader.constructor = constructor; + Node node = loader.load(); + } ++
Add a constructor function from a mapping.
-See Also:Example:
+
import std.string; + + import yaml; + + struct MyStruct + { + int x, y, z; + } + + MyStruct constructMyStructMapping(Mark start, Mark end, ref Node node) + { + //node is guaranteed to be mapping. + //!mystruct {"x": x, "y": y, "z": z} + try + { + return MyStruct(node["x"].get!int, node["y"].get!int, node["z"].get!int); + } + catch(NodeException e) + { + throw new ConstructorException("Could not construct MyStruct: " ~ e.msg, + start, end); + } + } + + void main() + { + auto loader = Loader("file.yaml"); + auto constructor = new Constructor; + constructor.addConstructorMapping("!mystruct", &constructMyStructMapping); + loader.constructor = constructor; + Node node = loader.load(); + } ++
import std.regex; import yaml; diff --git a/dyaml/constructor.d b/dyaml/constructor.d index a4427a6..c7ebcf5 100644 --- a/dyaml/constructor.d +++ b/dyaml/constructor.d @@ -132,6 +132,44 @@ final class Constructor * * Params: tag = Tag for the function to handle. * ctor = Constructor function. + * + * Example: + * + * -------------------- + * import std.string; + * + * import yaml; + * + * struct MyStruct + * { + * int x, y, z; + * } + * + * MyStruct constructMyStructScalar(Mark start, Mark end, ref Node node) + * { + * //Guaranteed to be string as we construct from scalar. + * //!mystruct x:y:z + * auto parts = node.get!string().split(":"); + * try + * { + * return MyStruct(to!int(parts[0]), to!int(parts[1]), to!int(parts[2])); + * } + * catch(Exception e) + * { + * throw new ConstructorException("Could not construct MyStruct: " ~ e.msg, + * start, end); + * } + * } + * + * void main() + * { + * auto loader = Loader("file.yaml"); + * auto constructor = new Constructor; + * constructor.addConstructorScalar("!mystruct", &constructMyStructScalar); + * loader.constructor = constructor; + * Node node = loader.load(); + * } + * -------------------- */ void addConstructorScalar(T)(in string tag, T function(Mark, Mark, ref Node) ctor) { @@ -144,6 +182,43 @@ final class Constructor * Add a constructor function from sequence. * * See_Also: addConstructorScalar + * + * Example: + * + * -------------------- + * import std.string; + * + * import yaml; + * + * struct MyStruct + * { + * int x, y, z; + * } + * + * MyStruct constructMyStructSequence(Mark start, Mark end, ref Node node) + * { + * //node is guaranteed to be sequence. + * //!mystruct [x, y, z] + * try + * { + * return MyStruct(node[0].get!int, node[1].get!int, node[2].get!int); + * } + * catch(NodeException e) + * { + * throw new ConstructorException("Could not construct MyStruct: " ~ e.msg, + * start, end); + * } + * } + * + * void main() + * { + * auto loader = Loader("file.yaml"); + * auto constructor = new Constructor; + * constructor.addConstructorSequence("!mystruct", &constructMyStructSequence); + * loader.constructor = constructor; + * Node node = loader.load(); + * } + * -------------------- */ void addConstructorSequence(T)(in string tag, T function(Mark, Mark, ref Node) ctor) { @@ -156,6 +231,43 @@ final class Constructor * Add a constructor function from a mapping. * * See_Also: addConstructorScalar + * + * Example: + * + * -------------------- + * import std.string; + * + * import yaml; + * + * struct MyStruct + * { + * int x, y, z; + * } + * + * MyStruct constructMyStructMapping(Mark start, Mark end, ref Node node) + * { + * //node is guaranteed to be mapping. + * //!mystruct {"x": x, "y": y, "z": z} + * try + * { + * return MyStruct(node["x"].get!int, node["y"].get!int, node["z"].get!int); + * } + * catch(NodeException e) + * { + * throw new ConstructorException("Could not construct MyStruct: " ~ e.msg, + * start, end); + * } + * } + * + * void main() + * { + * auto loader = Loader("file.yaml"); + * auto constructor = new Constructor; + * constructor.addConstructorMapping("!mystruct", &constructMyStructMapping); + * loader.constructor = constructor; + * Node node = loader.load(); + * } + * -------------------- */ void addConstructorMapping(T)(in string tag, T function(Mark, Mark, ref Node) ctor) { @@ -680,3 +792,97 @@ Node.Pair[] constructMap(Mark start, Mark end, ref Node node) } return pairs; } + + +//Unittests +private: + +import std.stream; +import dyaml.loader; + +struct MyStruct +{ + int x, y, z; +} + +MyStruct constructMyStructScalar(Mark start, Mark end, ref Node node) +{ + //Guaranteed to be string as we construct from scalar. + auto parts = node.get!string().split(":"); + try + { + return MyStruct(to!int(parts[0]), to!int(parts[1]), to!int(parts[2])); + } + catch(Exception e) + { + throw new ConstructorException("Could not construct MyStruct: " ~ e.msg, + start, end); + } +} + +MyStruct constructMyStructSequence(Mark start, Mark end, ref Node node) +{ + //node is guaranteed to be sequence. + try + { + return MyStruct(node[0].get!int, node[1].get!int, node[2].get!int); + } + catch(NodeException e) + { + throw new ConstructorException("Could not construct MyStruct: " ~ e.msg, + start, end); + } +} + +MyStruct constructMyStructMapping(Mark start, Mark end, ref Node node) +{ + //node is guaranteed to be mapping. + try + { + return MyStruct(node["x"].get!int, node["y"].get!int, node["z"].get!int); + } + catch(NodeException e) + { + throw new ConstructorException("Could not construct MyStruct: " ~ e.msg, + start, end); + } +} + +unittest +{ + char[] data = cast(char[])"!mystruct 1:2:3"; + auto loadStream = new MemoryStream(data); + auto loader = Loader(loadStream); + auto constructor = new Constructor; + constructor.addConstructorScalar("!mystruct", &constructMyStructScalar); + loader.constructor = constructor; + Node node = loader.load(); + + assert(node.get!MyStruct == MyStruct(1, 2, 3)); +} + +unittest +{ + char[] data = cast(char[])"!mystruct [1, 2, 3]"; + auto loadStream = new MemoryStream(data); + auto loader = Loader(loadStream); + auto constructor = new Constructor; + constructor.addConstructorSequence("!mystruct", &constructMyStructSequence); + loader.constructor = constructor; + Node node = loader.load(); + + assert(node.get!MyStruct == MyStruct(1, 2, 3)); +} + +unittest +{ + char[] data = cast(char[])"!mystruct {x: 1, y: 2, z: 3}"; + auto loadStream = new MemoryStream(data); + auto loader = Loader(loadStream); + auto constructor = new Constructor; + constructor.addConstructorMapping("!mystruct", &constructMyStructMapping); + loader.constructor = constructor; + Node node = loader.load(); + + assert(node.get!MyStruct == MyStruct(1, 2, 3)); +} diff --git a/dyaml/representer.d b/dyaml/representer.d index c72eb70..a865364 100644 --- a/dyaml/representer.d +++ b/dyaml/representer.d @@ -108,7 +108,7 @@ final class Representer * * void main() * { - * auto dumper = Dumper("file.txt"); + * auto dumper = Dumper("file.yaml"); * auto representer = new Representer; * representer.addRepresenter!MyStruct(&representMyStruct); * dumper.representer = representer; @@ -161,7 +161,7 @@ final class Representer * * void main() * { - * auto dumper = Dumper("file.txt"); + * auto dumper = Dumper("file.yaml"); * auto representer = new Representer; * representer.addRepresenter!MyClass(&representMyClass); * dumper.representer = representer; diff --git a/dyaml/resolver.d b/dyaml/resolver.d index 722da89..a466712 100644 --- a/dyaml/resolver.d +++ b/dyaml/resolver.d @@ -91,7 +91,7 @@ final class Resolver * * Examples: * - * Resolve scalars starting with 'A' to !tag : + * Resolve scalars starting with 'A' to !_tag : * -------------------- * import std.regex; *