The value returned by this function will be stored in the resulting node.
Only one constructor function can be set for one tag.
Structs and classes must implement the opCmp() operator for D:YAML support. The signature of the operator that must be implemented is const int opCmp(ref const MyStruct s) for structs where MyStruct is the struct type, and int opCmp(Object o) for classes. Note that the class opCmp() should not alter the compared values - it is not const for compatibility reasons.
1 import std.string; 2 3 import dyaml.all; 4 5 struct MyStruct 6 { 7 int x, y, z; 8 9 //Any D:YAML type must have a custom opCmp operator. 10 //This is used for ordering in mappings. 11 const int opCmp(ref const MyStruct s) 12 { 13 if(x != s.x){return x - s.x;} 14 if(y != s.y){return y - s.y;} 15 if(z != s.z){return z - s.z;} 16 return 0; 17 } 18 } 19 20 MyStruct constructMyStructScalar(ref Node node) 21 { 22 //Guaranteed to be string as we construct from scalar. 23 //!mystruct x:y:z 24 auto parts = node.as!string().split(":"); 25 // If this throws, the D:YAML will handle it and throw a YAMLException. 26 return MyStruct(to!int(parts[0]), to!int(parts[1]), to!int(parts[2])); 27 } 28 29 void main() 30 { 31 auto loader = Loader("file.yaml"); 32 auto constructor = new Constructor; 33 constructor.addConstructorScalar("!mystruct", &constructMyStructScalar); 34 loader.constructor = constructor; 35 Node node = loader.load(); 36 }
Add a constructor function from scalar.
The function must take a reference to Node to construct from. The node contains a string for scalars, Node[] for sequences and Node.Pair[] for mappings.
Any exception thrown by this function will be caught by D:YAML and its message will be added to a YAMLException that will also tell the user which type failed to construct, and position in the file.