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:
parent
fb67e775e4
commit
13ea5f0c24
33 changed files with 236 additions and 215 deletions
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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 && y == t.y && 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)];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue