Merge pull request #144 from Herringway/add-for-empty-node

allow uninitialized nodes to become sequence/mapping nodes
merged-on-behalf-of: BBasile <BBasile@users.noreply.github.com>
This commit is contained in:
The Dlang Bot 2018-06-10 08:48:52 +02:00 committed by GitHub
commit 468c497465
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1531,6 +1531,10 @@ struct Node
*/ */
void add(T)(T value) void add(T)(T value)
{ {
if (!isValid)
{
setValue(Node[].init);
}
enforce(isSequence(), enforce(isSequence(),
new NodeException("Trying to add an element to a " ~ nodeTypeString ~ " node", startMark_)); new NodeException("Trying to add an element to a " ~ nodeTypeString ~ " node", startMark_));
@ -1546,6 +1550,19 @@ struct Node
add(5.0f); add(5.0f);
assert(opIndex(4).as!float == 5.0f); assert(opIndex(4).as!float == 5.0f);
} }
with(Node())
{
add(5.0f);
assert(opIndex(0).as!float == 5.0f);
}
with(Node(5.0f))
{
assertThrown!NodeException(add(5.0f));
}
with(Node([5.0f : true]))
{
assertThrown!NodeException(add(5.0f));
}
} }
/** Add a key-value pair to a mapping. /** Add a key-value pair to a mapping.
@ -1565,6 +1582,10 @@ struct Node
*/ */
void add(K, V)(K key, V value) void add(K, V)(K key, V value)
{ {
if (!isValid)
{
setValue(Node.Pair[].init);
}
enforce(isMapping(), enforce(isMapping(),
new NodeException("Trying to add a key-value pair to a " ~ new NodeException("Trying to add a key-value pair to a " ~
nodeTypeString ~ " node", nodeTypeString ~ " node",
@ -1581,6 +1602,19 @@ struct Node
add(5, "6"); add(5, "6");
assert(opIndex(5).as!string == "6"); assert(opIndex(5).as!string == "6");
} }
with(Node())
{
add(5, "6");
assert(opIndex(5).as!string == "6");
}
with(Node(5.0f))
{
assertThrown!NodeException(add(5, "6"));
}
with(Node([5.0f]))
{
assertThrown!NodeException(add(5, "6"));
}
} }
/** Determine whether a key is in a mapping, and access its value. /** Determine whether a key is in a mapping, and access its value.