Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for handling YAML Merge Key (#41) - proper keys comparison for merging #1279

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/yaml-cpp/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ const char* const INVALID_ANCHOR = "invalid anchor";
const char* const INVALID_ALIAS = "invalid alias";
const char* const INVALID_TAG = "invalid tag";
const char* const BAD_FILE = "bad file";
const char* const MERGE_KEY_NEEDS_SINGLE_OR_SEQUENCE_OF_MAPS =
"merge key needs either single map or sequence of maps";

template <typename T>
inline const std::string KEY_NOT_FOUND_WITH_KEY(
Expand Down
60 changes: 57 additions & 3 deletions src/nodebuilder.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <algorithm>
#include <cassert>

#include "nodebuilder.h"
Expand All @@ -15,6 +16,7 @@ NodeBuilder::NodeBuilder()
m_stack{},
m_anchors{},
m_keys{},
m_mergeDicts{},
m_mapDepth(0) {
m_anchors.push_back(nullptr); // since the anchors start at 1
}
Expand Down Expand Up @@ -69,11 +71,38 @@ void NodeBuilder::OnMapStart(const Mark& mark, const std::string& tag,
node.set_tag(tag);
node.set_style(style);
m_mapDepth++;
m_mergeDicts.emplace_back();
}

void MergeMapCollection(detail::node& map_to, detail::node& map_from,
detail::shared_memory_holder& pMemory) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void MergeMapCollection(detail::node& map_to, detail::node& map_from,
detail::shared_memory_holder& pMemory) {
static void MergeMapCollection(detail::node& map_to, detail::node&& map_from,
detail::shared_memory_holder& pMemory) {

This function should be static or in an anonymous namespace to not leak any symbols to other translation units.

I also believe this is also a good case for a r-value reference for map_from. So I would use a && to make clear that map_from will be in an unclear state after this call.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should be static or in an anonymous namespace to not leak any symbols to other translation units.

Done

I also believe this is also a good case for a r-value reference for map_from. So I would use a && to make clear that map_from will be in an unclear state after this call.

I'm not sure about this one, since map_from is in valid state upon return from MergeMapCollection, what make you thinks the opposite?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what I was thinking last month....It seems fine way it is 😅

for (auto j = map_from.begin(); j != map_from.end(); j++) {
const auto from_key = j->first;
/// NOTE: const_map_to.get(*j->first) cannot be used here, since it
/// compares only the shared_ptr's, while we need to compare the key
/// itself.
///
/// NOTE: get() also iterates over elements
bool found = std::any_of(map_to.begin(), map_to.end(), [&](const detail::node_iterator_value<detail::node> & kv)
{
const auto key_node = kv.first;
return key_node->scalar() == from_key->scalar();
});
if (!found)
map_to.insert(*from_key, *j->second, pMemory);
}
}

void NodeBuilder::OnMapEnd() {
assert(m_mapDepth > 0);
detail::node& collection = *m_stack.back();
auto& toMerge = *m_mergeDicts.rbegin();
/// The elements for merging should be traversed in reverse order to prefer last values.
for (auto it = toMerge.rbegin(); it != toMerge.rend(); ++it) {
MergeMapCollection(collection, **it, m_pMemory);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If MergeMapCollection is being adjusted as suggested above **it must be changed to std::move(**it)

}
m_mapDepth--;
m_mergeDicts.pop_back();
Pop();
}

Expand Down Expand Up @@ -107,15 +136,40 @@ void NodeBuilder::Pop() {
m_stack.pop_back();

detail::node& collection = *m_stack.back();

if (collection.type() == NodeType::Sequence) {
collection.push_back(node, m_pMemory);
} else if (collection.type() == NodeType::Map) {
assert(!m_keys.empty());
PushedKey& key = m_keys.back();
if (key.second) {
collection.insert(*key.first, node, m_pMemory);
m_keys.pop_back();
detail::node& nk = *key.first;
if (nk.type() == NodeType::Scalar &&
((nk.tag() == "tag:yaml.org,2002:merge" && nk.scalar() == "<<") ||
(nk.tag() == "?" && nk.scalar() == "<<"))) {
if (node.type() == NodeType::Map) {
m_mergeDicts.rbegin()->emplace_back(&node);
m_keys.pop_back();
} else if (node.type() == NodeType::Sequence) {
for (auto i = node.begin(); i != node.end(); i++) {
auto v = *i;
if ((*v).type() == NodeType::Map) {
m_mergeDicts.rbegin()->emplace_back(&(*v));
} else {
throw ParserException(
node.mark(),
ErrorMsg::MERGE_KEY_NEEDS_SINGLE_OR_SEQUENCE_OF_MAPS);
}
}
m_keys.pop_back();
} else {
throw ParserException(
node.mark(),
ErrorMsg::MERGE_KEY_NEEDS_SINGLE_OR_SEQUENCE_OF_MAPS);
}
} else {
collection.insert(*key.first, node, m_pMemory);
m_keys.pop_back();
}
} else {
key.second = true;
}
Expand Down
1 change: 1 addition & 0 deletions src/nodebuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class NodeBuilder : public EventHandler {

using PushedKey = std::pair<detail::node*, bool>;
std::vector<PushedKey> m_keys;
std::vector<Nodes> m_mergeDicts;
std::size_t m_mapDepth;
};
} // namespace YAML
Expand Down
73 changes: 73 additions & 0 deletions test/integration/load_node_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "yaml-cpp/yaml.h" // IWYU pragma: keep

#include "gtest/gtest.h"
#include <algorithm>

namespace YAML {
namespace {
Expand Down Expand Up @@ -173,6 +174,78 @@ TEST(LoadNodeTest, CloneAlias) {
EXPECT_EQ(clone[0], clone);
}

TEST(LoadNodeTest, MergeKeyA) {
Node node = Load(
"{x: &foo {a : 1,b : 1,c : 1}, y: &bar {d: 2, e : 2, f : 2, a : 2}, z: "
"&stuff { << : *foo, b : 3} }");
EXPECT_EQ(NodeType::Map, node["z"].Type());
EXPECT_FALSE(node["z"]["<<"]);
EXPECT_EQ(1, node["z"]["a"].as<int>());
EXPECT_EQ(3, node["z"]["b"].as<int>());
EXPECT_EQ(1, node["z"]["c"].as<int>());
}

TEST(LoadNodeTest, MergeKeyAIterator) {
Node node = Load(
"{x: &foo {a : 1,b : 1,c : 1}, y: &bar {d: 2, e : 2, f : 2, a : 2}, z: "
"&stuff { << : *foo, b : 3} }");
EXPECT_EQ(NodeType::Map, node["z"].Type());

const auto& z = node["z"];
size_t z_b_keys = std::count_if(z.begin(), z.end(), [&](const detail::iterator_value & kv)
{
return kv.first.as<std::string>() == "b";
});
ASSERT_EQ(z_b_keys, 1);
}

TEST(LoadNodeTest, MergeKeyTwoOverrides) {
Node node = Load(R"(
trait1: &t1
foo: 1

trait2: &t2
foo: 2

merged:
<<: *t1
<<: *t2
)");
EXPECT_EQ(NodeType::Map, node["merged"].Type());
EXPECT_FALSE(node["merged"]["<<"]);
EXPECT_EQ(2, node["merged"]["foo"].as<int>());
}

TEST(LoadNodeTest, MergeKeyB) {
Node node = Load(
"{x: &foo {a : 1,b : 1,c : 1}, y: &bar {d: 2, e : 2, f : 2, a : 2}, z: "
"&stuff { << : *foo, b : 3}, w: { << : [*stuff, *bar], c: 4 }, v: { '<<' "
": *foo } , u : {!!merge << : *bar}, t: {!!merge << : *bar, h: 3} }");
EXPECT_EQ(NodeType::Map, node["z"].Type());
EXPECT_EQ(NodeType::Map, node["w"].Type());
EXPECT_FALSE(node["z"]["<<"]);
EXPECT_EQ(1, node["z"]["a"].as<int>());
EXPECT_EQ(3, node["z"]["b"].as<int>());
EXPECT_EQ(1, node["z"]["c"].as<int>());

EXPECT_EQ(2, node["w"]["a"].as<int>());
EXPECT_EQ(3, node["w"]["b"].as<int>());
EXPECT_EQ(4, node["w"]["c"].as<int>());
EXPECT_EQ(2, node["w"]["d"].as<int>());
EXPECT_EQ(2, node["w"]["e"].as<int>());
EXPECT_EQ(2, node["w"]["f"].as<int>());

EXPECT_TRUE(node["v"]["<<"]);
EXPECT_EQ(1, node["v"]["<<"]["a"].as<int>());

EXPECT_FALSE(node["u"]["<<"]);
EXPECT_EQ(2, node["u"]["d"].as<int>());

EXPECT_FALSE(node["t"]["<<"]);
EXPECT_EQ(2, node["t"]["d"].as<int>());
EXPECT_EQ(3, node["t"]["h"].as<int>());
}

TEST(LoadNodeTest, ForceInsertIntoMap) {
Node node;
node["a"] = "b";
Expand Down