allow uninitialized nodes to become sequence/mapping nodes

This commit is contained in:
Cameron Ross 2018-06-10 00:49:15 -03:00
parent eec7aa3bba
commit a6cc4ed229
No known key found for this signature in database
GPG key ID: 777897D98DC91C54

View file

@ -1534,6 +1534,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_));
@ -1549,6 +1553,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.
@ -1568,6 +1585,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",
@ -1584,6 +1605,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.