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) #1243

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
50 changes: 47 additions & 3 deletions src/nodebuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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 +70,29 @@ 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) {
const detail::node& const_map_to = map_to;
for (auto j = map_from.begin(); j != map_from.end(); j++) {
detail::node* s = const_map_to.get(*j->first, pMemory);
Copy link

@azat azat May 3, 2024

Choose a reason for hiding this comment

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

This compares the shared_ptr of the keys, so it will not work correctly, this can be visible by iterating over keys, here is a fix for this case - #1279

It is based on your PR, with this fix and a test on top - da04fa3 (#1279)

if (s == nullptr) {
map_to.insert(*j->first, *j->second, pMemory);
}
}
}

void NodeBuilder::OnMapEnd() {
assert(m_mapDepth > 0);
detail::node& collection = *m_stack.back();
auto& toMerge = *m_mergeDicts.rbegin();
for (detail::node* n : toMerge) {
MergeMapCollection(collection, *n, m_pMemory);
}
m_mapDepth--;
m_mergeDicts.pop_back();
Pop();
}

Expand Down Expand Up @@ -107,15 +126,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
41 changes: 41 additions & 0 deletions test/integration/load_node_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,47 @@ 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, 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(1, node["w"]["a"].as<int>());
Copy link

Choose a reason for hiding this comment

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

I've also changed the order of merging, to make it more compatible, see - 2a1aada (#1279)

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