Constructor

Constructs YAML values.

Each YAML scalar, sequence or mapping has a tag specifying its data type. Constructor uses user-specifyable functions to create a node of desired data type from a scalar, sequence or mapping.

More...

Constructors

this
this(const Flag!"useDefaultConstructors" defaultConstructors = Yes.useDefaultConstructors)

Construct a Constructor.

Members

Functions

addConstructorMapping
void addConstructorMapping(const string tag, T function(ref Node) @safe ctor)

Add a constructor function from a mapping.

addConstructorScalar
void addConstructorScalar(const string tag, T function(ref Node) @safe ctor)

Add a constructor function from scalar.

addConstructorSequence
void addConstructorSequence(const string tag, T function(ref Node) @safe ctor)

Add a constructor function from sequence.

Detailed Description

Each of these functions is associated with a tag, and can process either a scalar, a sequence, or a mapping. The constructor passes each value to the function with corresponding tag, which then returns the resulting value that can be stored in a node.

If a tag is detected with no known constructor function, it is considered an error.

Examples

Construct a struct from a scalar

1 static struct MyStruct
2 {
3     int x, y, z;
4 
5     int opCmp(ref const MyStruct s) const pure @safe nothrow
6     {
7         if(x != s.x){return x - s.x;}
8         if(y != s.y){return y - s.y;}
9         if(z != s.z){return z - s.z;}
10         return 0;
11     }
12 }
13 
14 static MyStruct constructMyStructScalar(ref Node node) @safe
15 {
16     // Guaranteed to be string as we construct from scalar.
17     auto parts = node.as!string().split(":");
18     return MyStruct(to!int(parts[0]), to!int(parts[1]), to!int(parts[2]));
19 }
20 
21 import dyaml.loader : Loader;
22 string data = "!mystruct 1:2:3";
23 auto loader = Loader.fromString(data);
24 auto constructor = new Constructor;
25 constructor.addConstructorScalar("!mystruct", &constructMyStructScalar);
26 loader.constructor = constructor;
27 Node node = loader.load();
28 
29 assert(node.as!MyStruct == MyStruct(1, 2, 3));

Construct a struct from a sequence

1 static struct MyStruct
2 {
3     int x, y, z;
4 
5     int opCmp(ref const MyStruct s) const pure @safe nothrow
6     {
7         if(x != s.x){return x - s.x;}
8         if(y != s.y){return y - s.y;}
9         if(z != s.z){return z - s.z;}
10         return 0;
11     }
12 }
13 static MyStruct constructMyStructSequence(ref Node node) @safe
14 {
15     // node is guaranteed to be sequence.
16     return MyStruct(node[0].as!int, node[1].as!int, node[2].as!int);
17 }
18 
19 import dyaml.loader : Loader;
20 string data = "!mystruct [1, 2, 3]";
21 auto loader = Loader.fromString(data);
22 auto constructor = new Constructor;
23 constructor.addConstructorSequence("!mystruct", &constructMyStructSequence);
24 loader.constructor = constructor;
25 Node node = loader.load();
26 
27 assert(node.as!MyStruct == MyStruct(1, 2, 3));

Construct a struct from a mapping

1 static struct MyStruct
2 {
3     int x, y, z;
4 
5     int opCmp(ref const MyStruct s) const pure @safe nothrow
6     {
7         if(x != s.x){return x - s.x;}
8         if(y != s.y){return y - s.y;}
9         if(z != s.z){return z - s.z;}
10         return 0;
11     }
12 }
13 static MyStruct constructMyStructMapping(ref Node node) @safe
14 {
15     // node is guaranteed to be mapping.
16     return MyStruct(node["x"].as!int, node["y"].as!int, node["z"].as!int);
17 }
18 
19 import dyaml.loader : Loader;
20 string data = "!mystruct {x: 1, y: 2, z: 3}";
21 auto loader = Loader.fromString(data);
22 auto constructor = new Constructor;
23 constructor.addConstructorMapping("!mystruct", &constructMyStructMapping);
24 loader.constructor = constructor;
25 Node node = loader.load();
26 
27 assert(node.as!MyStruct == MyStruct(1, 2, 3));

Meta