Added a shortcut alias called "as" for Node.get(), and replaced

get() with as() all over the code, tutorials, examples and docs.
Fixed a bug in YAML benchmark makefile.
Fixed a bug in autoddoc configuration.
This commit is contained in:
Ferdinand Majerech 2011-10-22 17:06:32 +02:00
parent fb67e775e4
commit 13ea5f0c24
33 changed files with 236 additions and 215 deletions

View file

@ -29,7 +29,7 @@ links = ../index.html Documentation home
# Source files or patterns to ignore. Supports regexp syntax.
# E.g; To ignore main.d and all source files in the test/ directory,
# you would use: "main.d test/*"
ignore = test/*, examples/*, docsrc/*, autoddoc/*, yaml.d, unittest.d, cdc.d, dyaml/composer.d, dyaml/event.d, dyaml/parser.d, dyaml/reader.d, dyaml/scanner.d, dyaml/token.d, dyaml/util.d, dyaml/anchor.d, dyaml/emitter.d, dyaml/flags.d, dyaml/serializer.d, dyaml/sharedobject.d, dyaml/tag.d, dyaml/tagdirectives.d
ignore = test/*, examples/*, docsrc/*, autoddoc/*, yaml.d, unittest.d, cdc.d, dyaml/composer.d, dyaml/event.d, dyaml/parser.d, dyaml/reader.d, dyaml/scanner.d, dyaml/token.d, dyaml/util.d, dyaml/anchor.d, dyaml/emitter.d, dyaml/flags.d, dyaml/serializer.d, dyaml/sharedobject.d, dyaml/tag.d, dyaml/tagdirectives.d, dyaml/queue.d
[DDOC]
# Command to use to generate the documentation.

Binary file not shown.

View file

@ -54,7 +54,7 @@ of these functions:
Color constructColorScalar(Mark start, Mark end, ref Node node)
{
string value = node.get!string;
string value = node.as!string;
if(value.length != 6)
{
@ -93,9 +93,9 @@ of these functions:
//Might throw if a value is missing is not an integer, or is out of range.
try
{
r = node["r"].get!ubyte;
g = node["g"].get!ubyte;
b = node["b"].get!ubyte;
r = node["r"].as!ubyte;
g = node["g"].as!ubyte;
b = node["b"].as!ubyte;
}
catch(NodeException e)
{
@ -143,10 +143,10 @@ Finally, the code to put it all together:
auto root = loader.load();
if(root["scalar-red"].get!Color == red &&
root["mapping-red"].get!Color == red &&
root["scalar-orange"].get!Color == orange &&
root["mapping-orange"].get!Color == orange)
if(root["scalar-red"].as!Color == red &&
root["mapping-red"].as!Color == red &&
root["scalar-orange"].as!Color == orange &&
root["mapping-orange"].as!Color == orange)
{
writeln("SUCCESS");
return;
@ -162,8 +162,8 @@ Finally, the code to put it all together:
First, we create a *Constructor* and pass functions to handle the ``!color``
and ``!color-mapping`` tag. We construct a *Loader* and pass the *Constructor*
to it. We then load the YAML document, and finally, read the colors using
*get()* method to test if they were loaded as expected.
to it. We then load the YAML document, and finally, read the colors to test if
they were loaded as expected.
You can find the source code for what we've done so far in the
``examples/constructor`` directory in the D:YAML package.
@ -256,7 +256,7 @@ With the following code, we will add support for dumping the our Color type.
Node representColor(ref Node node, Representer representer)
{
//The node is guaranteed to be Color as we add representer for Color.
Color color = node.get!Color;
Color color = node.as!Color;
static immutable hex = "0123456789ABCDEF";

View file

@ -90,7 +90,7 @@ into the file:
{
writeln(word);
}
writeln("The answer is ", root["Answer"].get!int);
writeln("The answer is ", root["Answer"].as!int);
//Dump the loaded document to output.yaml.
Dumper("output.yaml").dump(root);
@ -115,7 +115,7 @@ possible.
mapping (associative array) or a scalar (value). Here the root node is a
mapping, and we use the index operator to get subnodes with keys "Hello World"
and "Answer". We iterate over the first, as it is a sequence, and use the
*Node.get()* method on the second to get its value as an integer.
*Node.as()* method on the second to read its value as an integer.
You can iterate over a mapping or sequence as if it was an associative or normal
array. If you try to iterate over a scalar, it will throw a *YAMLException*.
@ -128,9 +128,9 @@ not possible to convert to iterated type, a *YAMLException* is thrown. For
instance, if we specified *int* here, we would get an error, as "Hello"
cannot be converted to an integer.
The *Node.get()* method is used to get value of a scalar node, allowing to
specify type. D:YAML will try to return the scalar as this type, converting if
needed, throwing *YAMLException* if not possible.
The *Node.as()* method is used to read value of a scalar node as specified type.
D:YAML will try to return the scalar as this type, converting if needed,
throwing *YAMLException* if not possible.
Finally we dump the document we just read to ``output.yaml`` with the
*Dumper.dump()* method. *Dumper* is a struct used to dump YAML documents.

View file

@ -127,7 +127,7 @@
{
<span class="d_comment">//Guaranteed to be string as we construct from scalar.
</span> <span class="d_comment">//!mystruct x:y:z
</span> <span class="d_keyword">auto</span> parts = node.get!string().split(<span class="d_string">":"</span>);
</span> <span class="d_keyword">auto</span> parts = node.as!string().split(<span class="d_string">":"</span>);
<span class="d_keyword">try</span>
{
<span class="d_keyword">return</span> MyStruct(to!<span class="d_keyword">int</span>(parts[0]), to!<span class="d_keyword">int</span>(parts[1]), to!<span class="d_keyword">int</span>(parts[2]));
@ -174,7 +174,7 @@
</span> <span class="d_comment">//!mystruct [x, y, z]
</span> <span class="d_keyword">try</span>
{
<span class="d_keyword">return</span> MyStruct(node[0].get!<span class="d_keyword">int</span>, node[1].get!<span class="d_keyword">int</span>, node[2].get!<span class="d_keyword">int</span>);
<span class="d_keyword">return</span> MyStruct(node[0].as!<span class="d_keyword">int</span>, node[1].as!<span class="d_keyword">int</span>, node[2].as!<span class="d_keyword">int</span>);
}
<span class="d_keyword">catch</span>(NodeException e)
{
@ -218,7 +218,7 @@
</span> <span class="d_comment">//!mystruct {"x": x, "y": y, "z": z}
</span> <span class="d_keyword">try</span>
{
<span class="d_keyword">return</span> MyStruct(node[<span class="d_string">"x"</span>].get!<span class="d_keyword">int</span>, node[<span class="d_string">"y"</span>].get!<span class="d_keyword">int</span>, node[<span class="d_string">"z"</span>].get!<span class="d_keyword">int</span>);
<span class="d_keyword">return</span> MyStruct(node[<span class="d_string">"x"</span>].as!<span class="d_keyword">int</span>, node[<span class="d_string">"y"</span>].as!<span class="d_keyword">int</span>, node[<span class="d_string">"z"</span>].as!<span class="d_keyword">int</span>);
}
<span class="d_keyword">catch</span>(NodeException e)
{

View file

@ -189,9 +189,11 @@
</table></div>
<p><b>Example:</b><br>
<pre class="d_code"> Dumper dumper = Dumper(<span class="d_string">"file.yaml"</span>);
string[string] directives;
directives[<span class="d_string">"!short!"</span>] = <span class="d_string">"tag:long.org,2011:"</span>;
<span class="d_comment">//This will emit tags starting with "tag:long.org,2011"
</span> <span class="d_comment">//with a "!short!" prefix instead.
</span> dumper.<span class="d_param">tags</span>(<span class="d_string">"short"</span>, <span class="d_string">"tag:long.org,2011:"</span>);
</span> dumper.<span class="d_psymbol">tagDirectives</span>(directives);
dumper.dump(Node(<span class="d_string">"foo"</span>));
</pre>
</p>

View file

@ -149,6 +149,12 @@
<dt class="d_decl">Node[] <a name="loadAll"></a><span class="ddoc_psymbol">loadAll</span>();
</dt>
<dd><p>Load all YAML documents.
</p>
<p>This is just a shortcut that iterates over all documents and returns
them all at once. Calling <a name="loadAll"></a><span class="ddoc_psymbol">loadAll</span> after iterating over the node or
vice versa will not return any documents, as they have all been parsed
already.
</p>
<b>Returns:</b><div class="pbr">Array of root nodes of all documents in the file/stream.

View file

@ -246,6 +246,11 @@
</dt>
<dd><p>Is this node a user defined type?</p>
</dd>
<dt class="d_decl">const @property string <a name="tag"></a><span class="ddoc_psymbol">tag</span>();
</dt>
<dd><p>Return <a name="tag"></a><span class="ddoc_psymbol">tag</span> of the node.</p>
</dd>
<dt class="d_decl">bool <a name="opEquals"></a><span class="ddoc_psymbol">opEquals</span>(T)(ref T <b>rhs</b>);
</dt>
@ -271,6 +276,11 @@
</table></div>
<b>Returns:</b><div class="pbr"><b>true</b> if equal, <b>false</b> otherwise.</div>
</dd>
<dt class="d_decl">alias <a name="as"></a><span class="ddoc_psymbol">as</span>;
</dt>
<dd><p>Shortcut for get().</p>
</dd>
<dt class="d_decl">T <a name="get"></a><span class="ddoc_psymbol">get</span>(T)();
</dt>
@ -303,9 +313,9 @@
<b>Examples:</b><div class="pbr">Automatic type conversion:
<pre class="d_code"> <span class="d_keyword">auto</span> node = Node(42);
<span class="d_keyword">assert</span>(node.<span class="d_psymbol">get</span>!<span class="d_keyword">int</span> == 42);
<span class="d_keyword">assert</span>(node.<span class="d_psymbol">get</span>!string == <span class="d_string">"42"</span>);
<span class="d_keyword">assert</span>(node.<span class="d_psymbol">get</span>!<span class="d_keyword">double</span> == 42.0);
<span class="d_keyword">assert</span>(node.as!<span class="d_keyword">int</span> == 42);
<span class="d_keyword">assert</span>(node.as!string == <span class="d_string">"42"</span>);
<span class="d_keyword">assert</span>(node.as!<span class="d_keyword">double</span> == 42.0);
</pre>
</div>

View file

@ -95,7 +95,7 @@
Node representMyStruct(<span class="d_keyword">ref</span> Node node, Representer representer)
{
<span class="d_comment">//The node is guaranteed to be MyStruct as we add representer for MyStruct.
</span> <span class="d_keyword">auto</span> value = node.get!MyStruct;
</span> <span class="d_keyword">auto</span> value = node.as!MyStruct;
<span class="d_comment">//Using custom scalar format, x:y:z.
</span> <span class="d_keyword">auto</span> scalar = format(value.x, <span class="d_string">":"</span>, value.y, <span class="d_string">":"</span>, value.z);
<span class="d_comment">//Representing as a scalar, with custom tag to specify this data type.
@ -136,7 +136,7 @@
<span class="d_keyword">return</span> x == t.x &amp;&amp; y == t.y &amp;&amp; z == t.z;
}
<span class="d_comment">///Useful for Node.get!string .
<span class="d_comment">///Useful for Node.as!string .
</span> <span class="d_keyword">override</span> string toString()
{
<span class="d_keyword">return</span> format(<span class="d_string">"MyClass("), x, <span class="d_string">", "</span>, y, <span class="d_string">", "</span>, z, <span class="d_string">"</span>"</span>);
@ -147,7 +147,7 @@
</span> Node representMyClass(<span class="d_keyword">ref</span> Node node, Representer representer)
{
<span class="d_comment">//The node is guaranteed to be MyClass as we add representer for MyClass.
</span> <span class="d_keyword">auto</span> value = node.get!MyClass;
</span> <span class="d_keyword">auto</span> value = node.as!MyClass;
<span class="d_comment">//Using custom scalar format, x:y:z.
</span> <span class="d_keyword">auto</span> scalar = format(value.x, <span class="d_string">":"</span>, value.y, <span class="d_string">":"</span>, value.z);
<span class="d_comment">//Representing as a scalar, with custom tag to specify this data type.
@ -189,7 +189,7 @@
Node representMyStruct(<span class="d_keyword">ref</span> Node node, Representer representer)
{
<span class="d_keyword">auto</span> value = node.get!MyStruct;
<span class="d_keyword">auto</span> value = node.as!MyStruct;
<span class="d_keyword">auto</span> <span class="d_param">scalar</span> = format(value.x, <span class="d_string">":"</span>, value.y, <span class="d_string">":"</span>, value.z);
<span class="d_keyword">return</span> representer.<span class="d_psymbol">representScalar</span>(<span class="d_string">"!mystruct.tag"</span>, <span class="d_param">scalar</span>);
}
@ -223,7 +223,7 @@
Node representMyStruct(<span class="d_keyword">ref</span> Node node, Representer representer)
{
<span class="d_keyword">auto</span> value = node.get!MyStruct;
<span class="d_keyword">auto</span> value = node.as!MyStruct;
<span class="d_keyword">auto</span> nodes = [Node(value.x), Node(value.y), Node(value.z)];
<span class="d_keyword">return</span> representer.<span class="d_psymbol">representSequence</span>(<span class="d_string">"!mystruct.tag"</span>, nodes);
}
@ -257,7 +257,7 @@
Node representMyStruct(<span class="d_keyword">ref</span> Node node, Representer representer)
{
<span class="d_keyword">auto</span> value = node.get!MyStruct;
<span class="d_keyword">auto</span> value = node.as!MyStruct;
<span class="d_keyword">auto</span> <span class="d_param">pairs</span> = [Node.Pair(<span class="d_string">"x"</span>, value.x),
Node.Pair(<span class="d_string">"y"</span>, value.y),
Node.Pair(<span class="d_string">"z"</span>, value.z)];

View file

@ -138,7 +138,7 @@ struct appears in Phobos.</p>
</div>
<div class="footer">
&copy; Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
Last updated on Oct 18, 2011.
Last updated on Oct 22, 2011.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
</div>
</body>

View file

@ -104,7 +104,7 @@
</div>
<div class="footer">
&copy; Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
Last updated on Oct 18, 2011.
Last updated on Oct 22, 2011.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
</div>
</body>

View file

@ -87,7 +87,7 @@
</div>
<div class="footer">
&copy; Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
Last updated on Oct 18, 2011.
Last updated on Oct 22, 2011.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
</div>
</body>

File diff suppressed because one or more lines are too long

View file

@ -87,7 +87,7 @@ from a mapping, where we use the following format: {r:RRR, g:GGG, b:BBB} . Code
of these functions:</p>
<div class="highlight-d"><div class="highlight"><pre><span class="n">Color</span> <span class="n">constructColorScalar</span><span class="p">(</span><span class="n">Mark</span> <span class="n">start</span><span class="p">,</span> <span class="n">Mark</span> <span class="n">end</span><span class="p">,</span> <span class="k">ref</span> <span class="n">Node</span> <span class="n">node</span><span class="p">)</span>
<span class="p">{</span>
<span class="nb">string</span> <span class="n">value</span> <span class="p">=</span> <span class="n">node</span><span class="p">.</span><span class="n">get</span><span class="p">!</span><span class="nb">string</span><span class="p">;</span>
<span class="nb">string</span> <span class="n">value</span> <span class="p">=</span> <span class="n">node</span><span class="p">.</span><span class="n">as</span><span class="p">!</span><span class="nb">string</span><span class="p">;</span>
<span class="k">if</span><span class="p">(</span><span class="n">value</span><span class="p">.</span><span class="n">length</span> <span class="p">!=</span> <span class="mi">6</span><span class="p">)</span>
<span class="p">{</span>
@ -126,9 +126,9 @@ of these functions:</p>
<span class="c1">//Might throw if a value is missing is not an integer, or is out of range.</span>
<span class="k">try</span>
<span class="p">{</span>
<span class="n">r</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">&quot;r&quot;</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
<span class="n">g</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">&quot;g&quot;</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
<span class="n">b</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">&quot;b&quot;</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
<span class="n">r</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">&quot;r&quot;</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
<span class="n">g</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">&quot;g&quot;</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
<span class="n">b</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">&quot;b&quot;</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">catch</span><span class="p">(</span><span class="n">NodeException</span> <span class="n">e</span><span class="p">)</span>
<span class="p">{</span>
@ -171,10 +171,10 @@ of these functions:</p>
<span class="k">auto</span> <span class="n">root</span> <span class="p">=</span> <span class="n">loader</span><span class="p">.</span><span class="n">load</span><span class="p">();</span>
<span class="k">if</span><span class="p">(</span><span class="n">root</span><span class="p">[</span><span class="s">&quot;scalar-red&quot;</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="n">Color</span> <span class="p">==</span> <span class="n">red</span> <span class="p">&amp;&amp;</span>
<span class="n">root</span><span class="p">[</span><span class="s">&quot;mapping-red&quot;</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="n">Color</span> <span class="p">==</span> <span class="n">red</span> <span class="p">&amp;&amp;</span>
<span class="n">root</span><span class="p">[</span><span class="s">&quot;scalar-orange&quot;</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="n">Color</span> <span class="p">==</span> <span class="n">orange</span> <span class="p">&amp;&amp;</span>
<span class="n">root</span><span class="p">[</span><span class="s">&quot;mapping-orange&quot;</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="n">Color</span> <span class="p">==</span> <span class="n">orange</span><span class="p">)</span>
<span class="k">if</span><span class="p">(</span><span class="n">root</span><span class="p">[</span><span class="s">&quot;scalar-red&quot;</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="n">Color</span> <span class="p">==</span> <span class="n">red</span> <span class="p">&amp;&amp;</span>
<span class="n">root</span><span class="p">[</span><span class="s">&quot;mapping-red&quot;</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="n">Color</span> <span class="p">==</span> <span class="n">red</span> <span class="p">&amp;&amp;</span>
<span class="n">root</span><span class="p">[</span><span class="s">&quot;scalar-orange&quot;</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="n">Color</span> <span class="p">==</span> <span class="n">orange</span> <span class="p">&amp;&amp;</span>
<span class="n">root</span><span class="p">[</span><span class="s">&quot;mapping-orange&quot;</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="n">Color</span> <span class="p">==</span> <span class="n">orange</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">writeln</span><span class="p">(</span><span class="s">&quot;SUCCESS&quot;</span><span class="p">);</span>
<span class="k">return</span><span class="p">;</span>
@ -191,8 +191,8 @@ of these functions:</p>
</div>
<p>First, we create a <em>Constructor</em> and pass functions to handle the <tt class="docutils literal"><span class="pre">!color</span></tt>
and <tt class="docutils literal"><span class="pre">!color-mapping</span></tt> tag. We construct a <em>Loader</em> and pass the <em>Constructor</em>
to it. We then load the YAML document, and finally, read the colors using
<em>get()</em> method to test if they were loaded as expected.</p>
to it. We then load the YAML document, and finally, read the colors to test if
they were loaded as expected.</p>
<p>You can find the source code for what we&#8217;ve done so far in the
<tt class="docutils literal"><span class="pre">examples/constructor</span></tt> directory in the D:YAML package.</p>
</div>
@ -263,7 +263,7 @@ loading the output.</p>
<div class="highlight-d"><div class="highlight"><pre><span class="n">Node</span> <span class="n">representColor</span><span class="p">(</span><span class="k">ref</span> <span class="n">Node</span> <span class="n">node</span><span class="p">,</span> <span class="n">Representer</span> <span class="n">representer</span><span class="p">)</span>
<span class="p">{</span>
<span class="c1">//The node is guaranteed to be Color as we add representer for Color.</span>
<span class="n">Color</span> <span class="n">color</span> <span class="p">=</span> <span class="n">node</span><span class="p">.</span><span class="n">get</span><span class="p">!</span><span class="n">Color</span><span class="p">;</span>
<span class="n">Color</span> <span class="n">color</span> <span class="p">=</span> <span class="n">node</span><span class="p">.</span><span class="n">as</span><span class="p">!</span><span class="n">Color</span><span class="p">;</span>
<span class="k">static</span> <span class="n">immutable</span> <span class="n">hex</span> <span class="p">=</span> <span class="s">&quot;0123456789ABCDEF&quot;</span><span class="p">;</span>
@ -369,7 +369,7 @@ directory of the D:YAML package.</p>
</div>
<div class="footer">
&copy; Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
Last updated on Oct 18, 2011.
Last updated on Oct 22, 2011.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
</div>
</body>

View file

@ -116,7 +116,7 @@ into the file:</p>
<span class="p">{</span>
<span class="n">writeln</span><span class="p">(</span><span class="n">word</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">writeln</span><span class="p">(</span><span class="s">&quot;The answer is &quot;</span><span class="p">,</span> <span class="n">root</span><span class="p">[</span><span class="s">&quot;Answer&quot;</span><span class="p">].</span><span class="n">get</span><span class="p">!</span><span class="kt">int</span><span class="p">);</span>
<span class="n">writeln</span><span class="p">(</span><span class="s">&quot;The answer is &quot;</span><span class="p">,</span> <span class="n">root</span><span class="p">[</span><span class="s">&quot;Answer&quot;</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="kt">int</span><span class="p">);</span>
<span class="c1">//Dump the loaded document to output.yaml.</span>
<span class="n">Dumper</span><span class="p">(</span><span class="s">&quot;output.yaml&quot;</span><span class="p">).</span><span class="n">dump</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
@ -137,7 +137,7 @@ possible.</p>
mapping (associative array) or a scalar (value). Here the root node is a
mapping, and we use the index operator to get subnodes with keys &#8220;Hello World&#8221;
and &#8220;Answer&#8221;. We iterate over the first, as it is a sequence, and use the
<em>Node.get()</em> method on the second to get its value as an integer.</p>
<em>Node.as()</em> method on the second to read its value as an integer.</p>
<p>You can iterate over a mapping or sequence as if it was an associative or normal
array. If you try to iterate over a scalar, it will throw a <em>YAMLException</em>.</p>
<p>You can iterate over subnodes using <em>Node</em> as the iterated type, or specify
@ -147,9 +147,9 @@ so we iterate over the &#8220;Hello World&#8221; sequence as an array of strings
not possible to convert to iterated type, a <em>YAMLException</em> is thrown. For
instance, if we specified <em>int</em> here, we would get an error, as &#8220;Hello&#8221;
cannot be converted to an integer.</p>
<p>The <em>Node.get()</em> method is used to get value of a scalar node, allowing to
specify type. D:YAML will try to return the scalar as this type, converting if
needed, throwing <em>YAMLException</em> if not possible.</p>
<p>The <em>Node.as()</em> method is used to read value of a scalar node as specified type.
D:YAML will try to return the scalar as this type, converting if needed,
throwing <em>YAMLException</em> if not possible.</p>
<p>Finally we dump the document we just read to <tt class="docutils literal"><span class="pre">output.yaml</span></tt> with the
<em>Dumper.dump()</em> method. <em>Dumper</em> is a struct used to dump YAML documents.
The <em>dump()</em> method writes one or more documents to a file, throwing
@ -237,7 +237,7 @@ example in the <tt class="docutils literal"><span class="pre">example/getting_st
</div>
<div class="footer">
&copy; Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
Last updated on Oct 18, 2011.
Last updated on Oct 22, 2011.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
</div>
</body>

View file

@ -330,7 +330,7 @@ Some of these might change in the future (especially !!map and !!set).</p>
</div>
<div class="footer">
&copy; Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
Last updated on Oct 18, 2011.
Last updated on Oct 22, 2011.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
</div>
</body>

View file

@ -54,7 +54,7 @@ of these functions:
Color constructColorScalar(Mark start, Mark end, ref Node node)
{
string value = node.get!string;
string value = node.as!string;
if(value.length != 6)
{
@ -93,9 +93,9 @@ of these functions:
//Might throw if a value is missing is not an integer, or is out of range.
try
{
r = node["r"].get!ubyte;
g = node["g"].get!ubyte;
b = node["b"].get!ubyte;
r = node["r"].as!ubyte;
g = node["g"].as!ubyte;
b = node["b"].as!ubyte;
}
catch(NodeException e)
{
@ -143,10 +143,10 @@ Finally, the code to put it all together:
auto root = loader.load();
if(root["scalar-red"].get!Color == red &&
root["mapping-red"].get!Color == red &&
root["scalar-orange"].get!Color == orange &&
root["mapping-orange"].get!Color == orange)
if(root["scalar-red"].as!Color == red &&
root["mapping-red"].as!Color == red &&
root["scalar-orange"].as!Color == orange &&
root["mapping-orange"].as!Color == orange)
{
writeln("SUCCESS");
return;
@ -162,8 +162,8 @@ Finally, the code to put it all together:
First, we create a *Constructor* and pass functions to handle the ``!color``
and ``!color-mapping`` tag. We construct a *Loader* and pass the *Constructor*
to it. We then load the YAML document, and finally, read the colors using
*get()* method to test if they were loaded as expected.
to it. We then load the YAML document, and finally, read the colors to test if
they were loaded as expected.
You can find the source code for what we've done so far in the
``examples/constructor`` directory in the D:YAML package.
@ -256,7 +256,7 @@ With the following code, we will add support for dumping the our Color type.
Node representColor(ref Node node, Representer representer)
{
//The node is guaranteed to be Color as we add representer for Color.
Color color = node.get!Color;
Color color = node.as!Color;
static immutable hex = "0123456789ABCDEF";

View file

@ -90,7 +90,7 @@ into the file:
{
writeln(word);
}
writeln("The answer is ", root["Answer"].get!int);
writeln("The answer is ", root["Answer"].as!int);
//Dump the loaded document to output.yaml.
Dumper("output.yaml").dump(root);
@ -115,7 +115,7 @@ possible.
mapping (associative array) or a scalar (value). Here the root node is a
mapping, and we use the index operator to get subnodes with keys "Hello World"
and "Answer". We iterate over the first, as it is a sequence, and use the
*Node.get()* method on the second to get its value as an integer.
*Node.as()* method on the second to read its value as an integer.
You can iterate over a mapping or sequence as if it was an associative or normal
array. If you try to iterate over a scalar, it will throw a *YAMLException*.
@ -128,9 +128,9 @@ not possible to convert to iterated type, a *YAMLException* is thrown. For
instance, if we specified *int* here, we would get an error, as "Hello"
cannot be converted to an integer.
The *Node.get()* method is used to get value of a scalar node, allowing to
specify type. D:YAML will try to return the scalar as this type, converting if
needed, throwing *YAMLException* if not possible.
The *Node.as()* method is used to read value of a scalar node as specified type.
D:YAML will try to return the scalar as this type, converting if needed,
throwing *YAMLException* if not possible.
Finally we dump the document we just read to ``output.yaml`` with the
*Dumper.dump()* method. *Dumper* is a struct used to dump YAML documents.

View file

@ -149,7 +149,7 @@ final class Constructor
* {
* //Guaranteed to be string as we construct from scalar.
* //!mystruct x:y:z
* auto parts = node.get!string().split(":");
* auto parts = node.as!string().split(":");
* try
* {
* return MyStruct(to!int(parts[0]), to!int(parts[1]), to!int(parts[2]));
@ -201,7 +201,7 @@ final class Constructor
* //!mystruct [x, y, z]
* try
* {
* return MyStruct(node[0].get!int, node[1].get!int, node[2].get!int);
* return MyStruct(node[0].as!int, node[1].as!int, node[2].as!int);
* }
* catch(NodeException e)
* {
@ -250,7 +250,7 @@ final class Constructor
* //!mystruct {"x": x, "y": y, "z": z}
* try
* {
* return MyStruct(node["x"].get!int, node["y"].get!int, node["z"].get!int);
* return MyStruct(node["x"].as!int, node["y"].as!int, node["z"].as!int);
* }
* catch(NodeException e)
* {
@ -344,7 +344,7 @@ YAMLMerge constructMerge(Mark start, Mark end, ref Node node)
///Construct a boolean node.
bool constructBool(Mark start, Mark end, ref Node node)
{
string value = node.get!string().toLower();
string value = node.as!string().toLower();
if(["yes", "true", "on"].canFind(value)) {return true;}
if(["no", "false", "off"].canFind(value)){return false;}
throw new Error("Unable to parse boolean value: " ~ value, start, end);
@ -353,7 +353,7 @@ bool constructBool(Mark start, Mark end, ref Node node)
///Construct an integer (long) node.
long constructLong(Mark start, Mark end, ref Node node)
{
string value = node.get!string().replace("_", "");
string value = node.as!string().replace("_", "");
const char c = value[0];
const long sign = c != '-' ? 1 : -1;
if(c == '-' || c == '+')
@ -421,7 +421,7 @@ unittest
///Construct a floating point (real) node.
real constructReal(Mark start, Mark end, ref Node node)
{
string value = node.get!string().replace("_", "").toLower();
string value = node.as!string().replace("_", "").toLower();
const char c = value[0];
const real sign = c != '-' ? 1.0 : -1.0;
if(c == '-' || c == '+')
@ -491,7 +491,7 @@ unittest
///Construct a binary (base64) node.
ubyte[] constructBinary(Mark start, Mark end, ref Node node)
{
string value = node.get!string;
string value = node.as!string;
//For an unknown reason, this must be nested to work (compiler bug?).
try
{
@ -520,7 +520,7 @@ unittest
///Construct a timestamp (SysTime) node.
SysTime constructTimestamp(Mark start, Mark end, ref Node node)
{
string value = node.get!string;
string value = node.as!string;
immutable YMDRegexp = regex("^([0-9][0-9][0-9][0-9])-([0-9][0-9]?)-([0-9][0-9]?)");
immutable HMSRegexp = regex("^[Tt \t]+([0-9][0-9]?):([0-9][0-9]):([0-9][0-9])(\\.[0-9]*)?");
@ -619,7 +619,7 @@ unittest
///Construct a string node.
string constructString(Mark start, Mark end, ref Node node)
{
return node.get!string;
return node.as!string;
}
///Convert a sequence of single-element mappings into a sequence of pairs.
@ -633,7 +633,7 @@ Node.Pair[] getPairs(string type, Mark start, Mark end, Node[] nodes)
new Error("While constructing " ~ type ~
", expected a mapping with single element", start, end));
pairs ~= node.get!(Node.Pair[]);
pairs ~= node.as!(Node.Pair[]);
}
return pairs;
@ -642,7 +642,7 @@ Node.Pair[] getPairs(string type, Mark start, Mark end, Node[] nodes)
///Construct an ordered map (ordered sequence of key:value pairs without duplicates) node.
Node.Pair[] constructOrderedMap(Mark start, Mark end, ref Node node)
{
auto pairs = getPairs("ordered map", start, end, node.get!(Node[]));
auto pairs = getPairs("ordered map", start, end, node.as!(Node[]));
//TODO: the map here should be replaced with something with deterministic
//memory allocation if possible.
@ -702,13 +702,13 @@ unittest
///Construct a pairs (ordered sequence of key: value pairs allowing duplicates) node.
Node.Pair[] constructPairs(Mark start, Mark end, ref Node node)
{
return getPairs("pairs", start, end, node.get!(Node[]));
return getPairs("pairs", start, end, node.as!(Node[]));
}
///Construct a set node.
Node[] constructSet(Mark start, Mark end, ref Node node)
{
auto pairs = node.get!(Node.Pair[]);
auto pairs = node.as!(Node.Pair[]);
//In future, the map here should be replaced with something with deterministic
//memory allocation if possible.
@ -772,13 +772,13 @@ unittest
///Construct a sequence (array) node.
Node[] constructSequence(Mark start, Mark end, ref Node node)
{
return node.get!(Node[]);
return node.as!(Node[]);
}
///Construct an unordered map (unordered set of key: value _pairs without duplicates) node.
Node.Pair[] constructMap(Mark start, Mark end, ref Node node)
{
auto pairs = node.get!(Node.Pair[]);
auto pairs = node.as!(Node.Pair[]);
//TODO: the map here should be replaced with something with deterministic
//memory allocation if possible.
//Detect duplicates.
@ -808,7 +808,7 @@ struct MyStruct
MyStruct constructMyStructScalar(Mark start, Mark end, ref Node node)
{
//Guaranteed to be string as we construct from scalar.
auto parts = node.get!string().split(":");
auto parts = node.as!string().split(":");
try
{
return MyStruct(to!int(parts[0]), to!int(parts[1]), to!int(parts[2]));
@ -825,7 +825,7 @@ 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);
return MyStruct(node[0].as!int, node[1].as!int, node[2].as!int);
}
catch(NodeException e)
{
@ -839,7 +839,7 @@ 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);
return MyStruct(node["x"].as!int, node["y"].as!int, node["z"].as!int);
}
catch(NodeException e)
{
@ -858,7 +858,7 @@ unittest
loader.constructor = constructor;
Node node = loader.load();
assert(node.get!MyStruct == MyStruct(1, 2, 3));
assert(node.as!MyStruct == MyStruct(1, 2, 3));
}
unittest
@ -871,7 +871,7 @@ unittest
loader.constructor = constructor;
Node node = loader.load();
assert(node.get!MyStruct == MyStruct(1, 2, 3));
assert(node.as!MyStruct == MyStruct(1, 2, 3));
}
unittest
@ -884,5 +884,5 @@ unittest
loader.constructor = constructor;
Node node = loader.load();
assert(node.get!MyStruct == MyStruct(1, 2, 3));
assert(node.as!MyStruct == MyStruct(1, 2, 3));
}

View file

@ -216,7 +216,7 @@ struct Node
with(Node(42))
{
assert(isScalar() && !isSequence && !isMapping && !isUserType);
assert(get!int == 42 && get!float == 42.0f && get!string == "42");
assert(as!int == 42 && as!float == 42.0f && as!string == "42");
assert(!isUserType());
}
with(Node(new class{int a = 5;}))
@ -278,7 +278,7 @@ struct Node
{
assert(!isScalar() && isSequence && !isMapping && !isUserType);
assert(length == 3);
assert(opIndex(2).get!int == 3);
assert(opIndex(2).as!int == 3);
}
//Will be emitted as a sequence (default for arrays)
@ -332,7 +332,7 @@ struct Node
{
assert(!isScalar() && !isSequence && isMapping && !isUserType);
assert(length == 2);
assert(opIndex("2").get!int == 2);
assert(opIndex("2").as!int == 2);
}
//Will be emitted as an unordered mapping (default for mappings)
@ -402,7 +402,7 @@ struct Node
{
assert(!isScalar() && !isSequence && isMapping && !isUserType);
assert(length == 2);
assert(opIndex("2").get!int == 2);
assert(opIndex("2").as!int == 2);
}
//Will be emitted as an unordered mapping (default for mappings)
@ -457,6 +457,9 @@ struct Node
return equals!(T, true)(rhs);
}
///Shortcut for get().
alias get as;
/**
* Get the value of the node as specified type.
*
@ -486,9 +489,9 @@ struct Node
* --------------------
* auto node = Node(42);
*
* assert(node.get!int == 42);
* assert(node.get!string == "42");
* assert(node.get!double == 42.0);
* assert(node.as!int == 42);
* assert(node.as!string == "42");
* assert(node.as!double == 42.0);
* --------------------
*
* Returns: Value of the node as specified type.
@ -524,7 +527,7 @@ struct Node
///Must go before others, as even string/int/etc could be stored in a YAMLObject.
if(isUserType)
{
auto object = get!YAMLObject;
auto object = as!YAMLObject;
if(object.type is typeid(T))
{
target = (cast(YAMLContainer!T)object).value_;
@ -534,7 +537,7 @@ struct Node
//If we're getting from a mapping and we're not getting Node.Pair[],
//we're getting the default value.
if(isMapping){return this["="].get!T;}
if(isMapping){return this["="].as!T;}
void throwUnexpectedType()
{
@ -606,8 +609,8 @@ struct Node
*/
@property size_t length()
{
if(isSequence) {return get!(Node[]).length;}
else if(isMapping){return get!(Pair[]).length;}
if(isSequence) {return as!(Node[]).length;}
else if(isMapping){return as!(Pair[]).length;}
throw new Error("Trying to get length of a scalar node", startMark_);
}
@ -640,7 +643,7 @@ struct Node
else if(isMapping)
{
auto idx = findPair(index);
if(idx >= 0){return get!(Pair[])[idx].value;}
if(idx >= 0){return as!(Pair[])[idx].value;}
string msg = "Mapping index not found" ~ (isSomeString!T ? ": " ~ to!string(index) : "");
throw new Error(msg, startMark_);
@ -669,10 +672,10 @@ struct Node
Pair(k3, n3),
Pair(k4, n4)]));
assert(narray[0].get!int == 11);
assert(narray[0].as!int == 11);
assert(null !is collectException(narray[42]));
assert(nmap["11"].get!int == 11);
assert(nmap["14"].get!int == 14);
assert(nmap["11"].as!int == 11);
assert(nmap["14"].as!int == 14);
assert(null !is collectException(nmap["42"]));
}
@ -718,7 +721,7 @@ struct Node
if(idx < 0){add(index, value);}
else
{
auto pairs = get!(Node.Pair[])();
auto pairs = as!(Node.Pair[])();
static if(is(V == Node)){pairs[idx].value = value;}
else {pairs[idx].value = Node(value);}
value_ = Value(pairs);
@ -736,15 +739,15 @@ struct Node
{
opIndexAssign(42, 3);
assert(length == 5);
assert(opIndex(3).get!int == 42);
assert(opIndex(3).as!int == 42);
}
with(Node(["1", "2", "3"], [4, 5, 6]))
{
opIndexAssign(42, "3");
opIndexAssign(123, 456);
assert(length == 4);
assert(opIndex("3").get!int == 42);
assert(opIndex(456).get!int == 123);
assert(opIndex("3").as!int == 42);
assert(opIndex(456).as!int == 123);
}
}
@ -772,7 +775,7 @@ struct Node
}
else
{
T temp = node.get!T;
T temp = node.as!T;
result = dg(temp);
}
if(result){break;}
@ -799,7 +802,7 @@ struct Node
}
foreach(Node node; narray)
{
array2 ~= node.get!int;
array2 ~= node.as!int;
}
assert(array == [11, 12, 13, 14]);
assert(array2 == [11, 12, 13, 14]);
@ -829,18 +832,18 @@ struct Node
}
else static if(is(K == Node))
{
V tempValue = pair.value.get!V;
V tempValue = pair.value.as!V;
result = dg(pair.key, tempValue);
}
else static if(is(V == Node))
{
K tempKey = pair.key.get!K;
K tempKey = pair.key.as!K;
result = dg(tempKey, pair.value);
}
else
{
K tempKey = pair.key.get!K;
V tempValue = pair.value.get!V;
K tempKey = pair.key.as!K;
V tempValue = pair.value.as!V;
result = dg(tempKey, tempValue);
}
@ -890,10 +893,10 @@ struct Node
{
switch(key)
{
case "11": assert(value.get!int == 5 ); break;
case "12": assert(value.get!bool == true ); break;
case "13": assert(value.get!float == 1.0 ); break;
case "14": assert(value.get!string == "yarly"); break;
case "11": assert(value.as!int == 5 ); break;
case "12": assert(value.as!bool == true ); break;
case "13": assert(value.as!float == 1.0 ); break;
case "14": assert(value.as!string == "yarly"); break;
default: assert(false);
}
}
@ -931,7 +934,7 @@ struct Node
with(Node([1, 2, 3, 4]))
{
add(5.0f);
assert(opIndex(4).get!float == 5.0f);
assert(opIndex(4).as!float == 5.0f);
}
}
@ -967,7 +970,7 @@ struct Node
with(Node([1, 2], [3, 4]))
{
add(5, "6");
assert(opIndex(5).get!string == "6");
assert(opIndex(5).as!string == "6");
}
}
@ -991,7 +994,7 @@ struct Node
{
foreach(idx, ref elem; get!(Node[]))
{
if(elem.convertsTo!T && elem.get!T == value)
if(elem.convertsTo!T && elem.as!T == value)
{
removeAt(idx);
return;
@ -1004,7 +1007,7 @@ struct Node
auto idx = findPair!(T, true)(value);
if(idx >= 0)
{
auto pairs = get!(Node.Pair[])();
auto pairs = as!(Node.Pair[])();
moveAll(pairs[idx + 1 .. $], pairs[idx .. $ - 1]);
pairs.length = pairs.length - 1;
value_ = Value(pairs);
@ -1020,8 +1023,8 @@ struct Node
{
remove(3);
assert(length == 4);
assert(opIndex(2).get!int == 4);
assert(opIndex(3).get!int == 3);
assert(opIndex(2).as!int == 4);
assert(opIndex(3).as!int == 3);
}
with(Node(["1", "2", "3"], [4, 5, 6]))
{
@ -1070,7 +1073,7 @@ struct Node
auto idx = findPair(index);
if(idx >= 0)
{
auto pairs = get!(Node.Pair[])();
auto pairs = as!(Node.Pair[])();
moveAll(pairs[idx + 1 .. $], pairs[idx .. $ - 1]);
pairs.length = pairs.length - 1;
value_ = Value(pairs);
@ -1086,7 +1089,7 @@ struct Node
{
removeAt(3);
assert(length == 4);
assert(opIndex(3).get!int == 3);
assert(opIndex(3).as!int == 3);
}
with(Node(["1", "2", "3"], [4, 5, 6]))
{
@ -1136,8 +1139,8 @@ struct Node
}
if(isSequence)
{
auto seq1 = get!(Node[]);
auto seq2 = rhs.get!(Node[]);
auto seq1 = as!(Node[]);
auto seq2 = rhs.as!(Node[]);
if(seq1 is seq2){return true;}
if(seq1.length != seq2.length){return false;}
foreach(node; 0 .. seq1.length)
@ -1148,8 +1151,8 @@ struct Node
}
if(isMapping)
{
auto map1 = get!(Node.Pair[]);
auto map2 = rhs.get!(Node.Pair[]);
auto map1 = as!(Node.Pair[]);
auto map2 = rhs.as!(Node.Pair[]);
if(map1 is map2){return true;}
if(map1.length != map2.length){return false;}
foreach(pair; 0 .. map1.length)
@ -1163,7 +1166,7 @@ struct Node
if(isUserType)
{
if(!rhs.isUserType){return false;}
return get!YAMLObject.equals(rhs.get!YAMLObject);
return get!YAMLObject.equals(rhs.as!YAMLObject);
}
if(isFloat)
{
@ -1287,7 +1290,7 @@ struct Node
//Get index of pair with key (or value, if value is true) matching index.
long findPair(T, bool value = false)(const ref T index)
{
auto pairs = get!(Node.Pair[])();
auto pairs = as!(Node.Pair[])();
Node* node;
foreach(idx, ref pair; pairs)
{
@ -1301,8 +1304,8 @@ struct Node
else static if(isFloatingPoint!T)
{
//Need to handle NaNs separately.
if((node.get!T == index) ||
(isFloat && isNaN(index) && isNaN(node.get!real)))
if((node.as!T == index) ||
(isFloat && isNaN(index) && isNaN(node.as!real)))
{
return idx;
}
@ -1311,7 +1314,7 @@ struct Node
{
try
{
if(node.get!T == index){return idx;}
if(node.as!T == index){return idx;}
}
catch(NodeException e)
{

View file

@ -99,7 +99,7 @@ final class Representer
* Node representMyStruct(ref Node node, Representer representer)
* {
* //The node is guaranteed to be MyStruct as we add representer for MyStruct.
* auto value = node.get!MyStruct;
* auto value = node.as!MyStruct;
* //Using custom scalar format, x:y:z.
* auto scalar = format(value.x, ":", value.y, ":", value.z);
* //Representing as a scalar, with custom tag to specify this data type.
@ -141,7 +141,7 @@ final class Representer
* return x == t.x && y == t.y && z == t.z;
* }
*
* ///Useful for Node.get!string .
* ///Useful for Node.as!string .
* override string toString()
* {
* return format("MyClass(", x, ", ", y, ", ", z, ")");
@ -152,7 +152,7 @@ final class Representer
* Node representMyClass(ref Node node, Representer representer)
* {
* //The node is guaranteed to be MyClass as we add representer for MyClass.
* auto value = node.get!MyClass;
* auto value = node.as!MyClass;
* //Using custom scalar format, x:y:z.
* auto scalar = format(value.x, ":", value.y, ":", value.z);
* //Representing as a scalar, with custom tag to specify this data type.
@ -200,7 +200,7 @@ final class Representer
*
* Node representMyStruct(ref Node node, Representer representer)
* {
* auto value = node.get!MyStruct;
* auto value = node.as!MyStruct;
* auto scalar = format(value.x, ":", value.y, ":", value.z);
* return representer.representScalar("!mystruct.tag", scalar);
* }
@ -232,7 +232,7 @@ final class Representer
*
* Node representMyStruct(ref Node node, Representer representer)
* {
* auto value = node.get!MyStruct;
* auto value = node.as!MyStruct;
* auto nodes = [Node(value.x), Node(value.y), Node(value.z)];
* return representer.representSequence("!mystruct.tag", nodes);
* }
@ -270,7 +270,7 @@ final class Representer
*
* Node representMyStruct(ref Node node, Representer representer)
* {
* auto value = node.get!MyStruct;
* auto value = node.as!MyStruct;
* auto pairs = [Node.Pair("x", value.x),
* Node.Pair("y", value.y),
* Node.Pair("z", value.z)];
@ -294,7 +294,7 @@ final class Representer
Node representData(ref Node data)
{
//User types are wrapped in YAMLObject.
auto type = data.isUserType ? data.get!YAMLObject.type : data.type;
auto type = data.isUserType ? data.as!YAMLObject.type : data.type;
enforce((type in representers_) !is null,
new RepresenterException("No representer function for type "
@ -322,7 +322,7 @@ Node representNull(ref Node node, Representer representer)
///Represent a string _node as a string scalar.
Node representString(ref Node node, Representer representer)
{
string value = node.get!string;
string value = node.as!string;
return value is null ? representNull(node, representer)
: representer.representScalar("tag:yaml.org,2002:str", value);
}
@ -330,7 +330,7 @@ Node representString(ref Node node, Representer representer)
///Represent a bytes _node as a binary scalar.
Node representBytes(ref Node node, Representer representer)
{
const ubyte[] value = node.get!(ubyte[]);
const ubyte[] value = node.as!(ubyte[]);
if(value is null){return representNull(node, representer);}
return representer.representScalar("tag:yaml.org,2002:binary",
cast(string)Base64.encode(value));
@ -340,20 +340,20 @@ Node representBytes(ref Node node, Representer representer)
Node representBool(ref Node node, Representer representer)
{
return representer.representScalar("tag:yaml.org,2002:bool",
node.get!bool ? "true" : "false");
node.as!bool ? "true" : "false");
}
///Represent a long _node as an integer scalar.
Node representLong(ref Node node, Representer representer)
{
return representer.representScalar("tag:yaml.org,2002:int",
to!string(node.get!long));
to!string(node.as!long));
}
///Represent a real _node as a floating point scalar.
Node representReal(ref Node node, Representer representer)
{
real f = node.get!real;
real f = node.as!real;
string value = isNaN(f) ? ".nan":
f == real.infinity ? ".inf":
f == -1.0 * real.infinity ? "-.inf":
@ -368,13 +368,13 @@ Node representReal(ref Node node, Representer representer)
Node representSysTime(ref Node node, Representer representer)
{
return representer.representScalar("tag:yaml.org,2002:timestamp",
node.get!SysTime.toISOExtString());
node.as!SysTime.toISOExtString());
}
///Represent a sequence _node as sequence/set.
Node representNodes(ref Node node, Representer representer)
{
auto nodes = node.get!(Node[]);
auto nodes = node.as!(Node[]);
if(node.tag_ == Tag("tag:yaml.org,2002:set"))
{
///YAML sets are mapping with null values.
@ -396,7 +396,7 @@ Node representNodes(ref Node node, Representer representer)
///Represent a mapping _node as map/ordered map/pairs.
Node representPairs(ref Node node, Representer representer)
{
auto pairs = node.get!(Node.Pair[]);
auto pairs = node.as!(Node.Pair[]);
bool hasDuplicates(Node.Pair[] pairs)
{
@ -456,7 +456,7 @@ struct MyStruct
Node representMyStruct(ref Node node, Representer representer)
{
//The node is guaranteed to be MyStruct as we add representer for MyStruct.
auto value = node.get!MyStruct;
auto value = node.as!MyStruct;
//Using custom scalar format, x:y:z.
auto scalar = format(value.x, ":", value.y, ":", value.z);
//Representing as a scalar, with custom tag to specify this data type.
@ -465,14 +465,14 @@ Node representMyStruct(ref Node node, Representer representer)
Node representMyStructSeq(ref Node node, Representer representer)
{
auto value = node.get!MyStruct;
auto value = node.as!MyStruct;
auto nodes = [Node(value.x), Node(value.y), Node(value.z)];
return representer.representSequence("!mystruct.tag", nodes);
}
Node representMyStructMap(ref Node node, Representer representer)
{
auto value = node.get!MyStruct;
auto value = node.as!MyStruct;
auto pairs = [Node.Pair("x", value.x),
Node.Pair("y", value.y),
Node.Pair("z", value.z)];
@ -498,7 +498,7 @@ class MyClass
return x == t.x && y == t.y && z == t.z;
}
///Useful for Node.get!string .
///Useful for Node.as!string .
override string toString()
{
return format("MyClass(", x, ", ", y, ", ", z, ")");
@ -509,7 +509,7 @@ class MyClass
Node representMyClass(ref Node node, Representer representer)
{
//The node is guaranteed to be MyClass as we add representer for MyClass.
auto value = node.get!MyClass;
auto value = node.as!MyClass;
//Using custom scalar format, x:y:z.
auto scalar = format(value.x, ":", value.y, ":", value.z);
//Representing as a scalar, with custom tag to specify this data type.

View file

@ -123,8 +123,8 @@ struct Serializer
{
if(node.isScalar)
{
return node.isType!string ? node.get!string.length > 64 :
node.isType!(ubyte[]) ? node.get!(ubyte[]).length > 64:
return node.isType!string ? node.as!string.length > 64 :
node.isType!(ubyte[]) ? node.as!(ubyte[]).length > 64:
false;
}
return node.length > 2;
@ -192,7 +192,7 @@ struct Serializer
if(node.isScalar)
{
assert(node.isType!string, "Scalar node type must be string before serialized");
auto value = node.get!string;
auto value = node.as!string;
Tag detectedTag = resolver_.resolve(NodeID.Scalar, Tag(null), value, true);
Tag defaultTag = resolver_.resolve(NodeID.Scalar, Tag(null), value, false);

View file

@ -12,7 +12,7 @@ struct Color
Color constructColorScalar(Mark start, Mark end, ref Node node)
{
string value = node.get!string;
string value = node.as!string;
if(value.length != 6)
{
@ -51,9 +51,9 @@ Color constructColorMapping(Mark start, Mark end, ref Node node)
//Might throw if a value is missing is not an integer, or is out of range.
try
{
r = node["r"].get!ubyte;
g = node["g"].get!ubyte;
b = node["b"].get!ubyte;
r = node["r"].as!ubyte;
g = node["g"].as!ubyte;
b = node["b"].as!ubyte;
}
catch(NodeException e)
{
@ -80,10 +80,10 @@ void main()
auto root = loader.load();
if(root["scalar-red"].get!Color == red &&
root["mapping-red"].get!Color == red &&
root["scalar-orange"].get!Color == orange &&
root["mapping-orange"].get!Color == orange)
if(root["scalar-red"].as!Color == red &&
root["mapping-red"].as!Color == red &&
root["scalar-orange"].as!Color == orange &&
root["mapping-orange"].as!Color == orange)
{
writeln("SUCCESS");
return;

View file

@ -11,7 +11,7 @@ void main()
{
writeln(word);
}
writeln("The answer is ", root["Answer"].get!int);
writeln("The answer is ", root["Answer"].as!int);
//Dump the loaded document to output.yaml.
Dumper("output.yaml").dump(root);

View file

@ -11,7 +11,7 @@ struct Color
Node representColor(ref Node node, Representer representer)
{
//The node is guaranteed to be Color as we add representer for Color.
Color color = node.get!Color;
Color color = node.as!Color;
static immutable hex = "0123456789ABCDEF";

View file

@ -12,7 +12,7 @@ struct Color
Color constructColorScalar(Mark start, Mark end, ref Node node)
{
string value = node.get!string;
string value = node.as!string;
if(value.length != 6)
{
@ -51,9 +51,9 @@ Color constructColorMapping(Mark start, Mark end, ref Node node)
//Might throw if a value is missing is not an integer, or is out of range.
try
{
r = node["r"].get!ubyte;
g = node["g"].get!ubyte;
b = node["b"].get!ubyte;
r = node["r"].as!ubyte;
g = node["g"].as!ubyte;
b = node["b"].as!ubyte;
}
catch(NodeException e)
{
@ -85,10 +85,10 @@ void main()
auto root = loader.load();
if(root["scalar-red"].get!Color == red &&
root["mapping-red"].get!Color == red &&
root["scalar-orange"].get!Color == orange &&
root["mapping-orange"].get!Color == orange)
if(root["scalar-red"].as!Color == red &&
root["mapping-red"].as!Color == red &&
root["scalar-orange"].as!Color == orange &&
root["mapping-orange"].as!Color == orange)
{
writeln("SUCCESS");
return;

View file

@ -2,4 +2,4 @@ main:
dmd -w -I../../ -L-L../../ -L-ldyaml yaml_bench.d
clean:
rm yaml_stats yaml_bench.o
rm yaml_bench yaml_bench.o

View file

@ -29,13 +29,13 @@ void extract(ref Node document)
{
if(root.isScalar) switch(root.tag)
{
case "tag:yaml.org,2002:null": auto value = root.get!YAMLNull; break;
case "tag:yaml.org,2002:bool": auto value = root.get!bool; break;
case "tag:yaml.org,2002:int": auto value = root.get!long; break;
case "tag:yaml.org,2002:float": auto value = root.get!real; break;
case "tag:yaml.org,2002:binary": auto value = root.get!(ubyte[]); break;
case "tag:yaml.org,2002:timestamp": auto value = root.get!SysTime; break;
case "tag:yaml.org,2002:str": auto value = root.get!string; break;
case "tag:yaml.org,2002:null": auto value = root.as!YAMLNull; break;
case "tag:yaml.org,2002:bool": auto value = root.as!bool; break;
case "tag:yaml.org,2002:int": auto value = root.as!long; break;
case "tag:yaml.org,2002:float": auto value = root.as!real; break;
case "tag:yaml.org,2002:binary": auto value = root.as!(ubyte[]); break;
case "tag:yaml.org,2002:timestamp": auto value = root.as!SysTime; break;
case "tag:yaml.org,2002:str": auto value = root.as!string; break;
default: writeln("Unrecognozed tag: ", root.tag);
}
else if(root.isSequence) foreach(ref Node node; root)

View file

@ -75,7 +75,7 @@ string randomType(string[] types)
auto probabilities = new uint[types.length];
foreach(index, type; types)
{
probabilities[index] = config[type]["probability"].get!uint;
probabilities[index] = config[type]["probability"].as!uint;
}
return types[dice(probabilities)];
}
@ -84,8 +84,8 @@ Node genString(bool root = false)
{
auto range = config["string"]["range"];
const chars = randomLong(range["min"].get!uint, range["max"].get!uint,
range["dist"].get!string);
const chars = randomLong(range["min"].as!uint, range["max"].as!uint,
range["dist"].as!string);
char[] result = new char[chars];
result[0] = randomChar(alphabet);
@ -101,8 +101,8 @@ Node genInt(bool root = false)
{
auto range = config["int"]["range"];
const result = randomLong(range["min"].get!int, range["max"].get!int,
range["dist"].get!string);
const result = randomLong(range["min"].as!int, range["max"].as!int,
range["dist"].as!string);
return Node(result);
}
@ -111,8 +111,8 @@ Node genFloat(bool root = false)
{
auto range = config["float"]["range"];
const result = randomReal(range["min"].get!real, range["max"].get!real,
range["dist"].get!string);
const result = randomReal(range["min"].as!real, range["max"].as!real,
range["dist"].as!string);
return Node(result);
}
@ -126,10 +126,10 @@ Node genTimestamp(bool root = false)
{
auto range = config["timestamp"]["range"];
auto hnsecs = randomLong(range["min"].get!ulong, range["max"].get!ulong,
range["dist"].get!string);
auto hnsecs = randomLong(range["min"].as!ulong, range["max"].as!ulong,
range["dist"].as!string);
if(randomNormalized() <= config["timestamp"]["round-chance"].get!real)
if(randomNormalized() <= config["timestamp"]["round-chance"].as!real)
{
hnsecs -= hnsecs % 10000000;
}
@ -141,8 +141,8 @@ Node genBinary(bool root = false)
{
auto range = config["binary"]["range"];
const bytes = randomLong(range["min"].get!uint, range["max"].get!uint,
range["dist"].get!string);
const bytes = randomLong(range["min"].as!uint, range["max"].as!uint,
range["dist"].as!string);
ubyte[] result = new ubyte[bytes];
foreach(i; 0 .. bytes)
@ -167,8 +167,8 @@ Node nodes(in bool root, Node range, in string tag, in bool set = false)
}
else
{
const elems = randomLong(range["min"].get!uint, range["max"].get!uint,
range["dist"].get!string);
const elems = randomLong(range["min"].as!uint, range["max"].as!uint,
range["dist"].as!string);
nodes = new Node[elems];
foreach(i; 0 .. elems)
@ -204,8 +204,8 @@ Node pairs(bool root, bool complex, Node range, string tag)
}
else
{
const pairs = randomLong(range["min"].get!uint, range["max"].get!uint,
range["dist"].get!string);
const pairs = randomLong(range["min"].as!uint, range["max"].as!uint,
range["dist"].as!string);
keys = new Node[pairs];
values = new Node[pairs];
@ -222,7 +222,7 @@ Node pairs(bool root, bool complex, Node range, string tag)
Node genMap(bool root = false)
{
Node range = config["map"]["range"];
const complex = config["complex-keys"].get!bool;
const complex = config["complex-keys"].as!bool;
return pairs(root, complex, range, "tag:yaml.org,2002:map");
}
@ -230,7 +230,7 @@ Node genMap(bool root = false)
Node genOmap(bool root = false)
{
Node range = config["omap"]["range"];
const complex = config["complex-keys"].get!bool;
const complex = config["complex-keys"].as!bool;
return pairs(root, complex, range, "tag:yaml.org,2002:omap");
}
@ -238,7 +238,7 @@ Node genOmap(bool root = false)
Node genPairs(bool root = false)
{
Node range = config["pairs"]["range"];
const complex = config["complex-keys"].get!bool;
const complex = config["complex-keys"].as!bool;
return pairs(root, complex, range, "tag:yaml.org,2002:pairs");
}
@ -253,12 +253,12 @@ Node[] generate(in string configFileName)
{
config = Loader(configFileName).load();
minNodesDocument = config["min-nodes-per-document"].get!long;
minNodesDocument = config["min-nodes-per-document"].as!long;
Node[] result;
foreach(i; 0 .. config["documents"].get!uint)
foreach(i; 0 .. config["documents"].as!uint)
{
result ~= generateNode(config["root-type"].get!string, true);
result ~= generateNode(config["root-type"].as!string, true);
totalNodes = 0;
}
@ -290,8 +290,8 @@ void main(string[] args)
encoding == "utf-32" ? Encoding.UTF_32:
Encoding.UTF_8;
dumper.indent = config["indent"].get!uint;
dumper.textWidth = config["text-width"].get!uint;
dumper.indent = config["indent"].as!uint;
dumper.textWidth = config["text-width"].as!uint;
dumper.dump(generated);
}
catch(YAMLException e)

View file

@ -341,7 +341,7 @@ TestClass constructClass(Mark start, Mark end, ref Node node)
{
try
{
return new TestClass(node["x"].get!int, node["y"].get!int, node["z"].get!int);
return new TestClass(node["x"].as!int, node["y"].as!int, node["z"].as!int);
}
catch(NodeException e)
{
@ -352,7 +352,7 @@ TestClass constructClass(Mark start, Mark end, ref Node node)
Node representClass(ref Node node, Representer representer)
{
auto value = node.get!TestClass;
auto value = node.as!TestClass;
auto pairs = [Node.Pair("x", value.x),
Node.Pair("y", value.y),
Node.Pair("z", value.z)];
@ -364,14 +364,14 @@ Node representClass(ref Node node, Representer representer)
///Constructor function for TestStruct.
TestStruct constructStruct(Mark start, Mark end, ref Node node)
{
return TestStruct(to!int(node.get!string));
return TestStruct(to!int(node.as!string));
}
///Representer function for TestStruct.
Node representStruct(ref Node node, Representer representer)
{
string[] keys, values;
auto value = node.get!TestStruct;
auto value = node.as!TestStruct;
return representer.representScalar("!tag2", to!string(value.value));
}

View file

@ -58,13 +58,13 @@ void testUnicodeInput(bool verbose, string unicodeFilename)
string expected = data.split().join(" ");
Node output = Loader(new MemoryStream(to!(char[])(data))).load();
assert(output.get!string == expected);
assert(output.as!string == expected);
foreach(stream; [new MemoryStream(cast(byte[])(bom16() ~ to!(wchar[])(data))),
new MemoryStream(cast(byte[])(bom32() ~ to!(dchar[])(data)))])
{
output = Loader(stream).load();
assert(output.get!string == expected);
assert(output.as!string == expected);
}
}