Skip to content

Commit

Permalink
Fix sequence sometimes not turning into a map (#450)
Browse files Browse the repository at this point in the history
Previously, just referencing the next element in the sequence (and so constructing it, as an undefined element) would allow you to skip defining an element without turning the sequence into a map. E.g:

node[0] = "foo"; // sequence of size 1
node[1]; // sequence of size 1, with an undefined element at 1
node[2] = "bar"; // FIX: should be map of size 2 (since there's no element at index 1)
  • Loading branch information
butataatawa authored and jbeder committed Jan 2, 2017
1 parent 3757b20 commit f828610
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/yaml-cpp/node/detail/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct get_idx<Key,

static node* get(std::vector<node*>& sequence, const Key& key,
shared_memory_holder pMemory) {
if (key > sequence.size())
if (key > sequence.size() || (key > 0 && !sequence[key-1]->is_defined()))
return 0;
if (key == sequence.size())
sequence.push_back(&pMemory->create_node());
Expand Down
13 changes: 12 additions & 1 deletion test/node/node_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,22 @@ TEST(NodeTest, MapWithUndefinedValues) {
EXPECT_EQ(2, node.size());
}

TEST(NodeTest, SeqIntoMap) {
Node node;
node[0] = "test";
node[1];
node[2] = "value";
EXPECT_TRUE(node.IsMap());
EXPECT_EQ("test", node[0].as<std::string>());
EXPECT_EQ("value", node[2].as<std::string>());
EXPECT_EQ(2, node.size());
}

TEST(NodeTest, RemoveUnassignedNode) {
Node node(NodeType::Map);
node["key"];
node.remove("key");
EXPECT_EQ(node.size(), 0);
EXPECT_EQ(0, node.size());
}

TEST(NodeTest, MapForceInsert) {
Expand Down

0 comments on commit f828610

Please sign in to comment.