Minor opBinaryRight style, doc & test changes.

This commit is contained in:
Kiith-Sa 2013-09-09 22:39:46 +02:00
parent bf637c43ad
commit 4ecc4104da

View file

@ -1226,43 +1226,46 @@ struct Node
} }
/** /**
* Report whether the given key is found. * Determine whether a key is in a mapping, and access its value.
* *
* This method can only be called on mapping nodes. * This method can only be called on mapping nodes.
* *
* Returns:
* A pointer to the value of the given key, or null if not found.
*
* Be careful! Modifying the mapping with any other function can
* invalidate the pointer.
*
* Params: key = Key to search for. * Params: key = Key to search for.
* *
* Returns: A pointer to the value (as a Node) corresponding to key,
* or null if not found.
*
* Note: Any modification to the node can invalidate the returned
* pointer.
*
* See_Also: contains
*/ */
Node* opBinaryRight(string op, K)(K key) @trusted if (op == "in") Node* opBinaryRight(string op, K)(K key) @trusted if (op == "in")
{ {
enforce(isMapping(), enforce(isMapping, new Error("Trying to use 'in' on a " ~
new Error("Trying to use 'in' on a " ~ nodeTypeString ~ " node", startMark_));
nodeTypeString ~ " node",
startMark_));
auto idx = findPair(key); auto idx = findPair(key);
if (idx < 0) { if(idx < 0)
{
return null; return null;
} else { }
return &get!(Node.Pair[])[idx].value; else
{
return &(get!(Node.Pair[])[idx].value);
} }
} }
unittest unittest
{ {
writeln(`D:YAML Node opBinaryRight!"in" unittest`); writeln(`D:YAML Node opBinaryRight!"in" unittest`);
auto nd = Node(["foo", "baz"], ["bar", "qux"]); auto mapping = Node(["foo", "baz"], ["bar", "qux"]);
assert("bad" !in nd && ("bad" in nd) is null); assert("bad" !in mapping && ("bad" in mapping) is null);
Node* foo = "foo" in nd; Node* foo = "foo" in mapping;
assert(foo !is null); assert(foo !is null);
assert(*foo == Node("bar")); assert(*foo == Node("bar"));
assert(foo.get!string == "bar");
*foo = Node("newfoo"); *foo = Node("newfoo");
assert(nd["foo"] == Node("newfoo")); assert(mapping["foo"] == Node("newfoo"));
} }
/** /**