Skip to content

Commit

Permalink
Fix order for merging iterator
Browse files Browse the repository at this point in the history
Consider the following YAML:

    trait1: &t1
      foo: 1

    trait2: &t2
      foo: 2

    merged:
      <<: *t1
      <<: *t2

yq reports:

    $ yq .merged.foo < /tmp/yaml
    2

while the order that yaml-cpp returns is different, since it will
firstly handle 1, and will not replace it with 2:

    $ util/parse < /tmp/yaml
    trait1:
      ? &1 foo
      : &2 1
    trait2:
      foo: 2
    merged:
      *1 : *2

(Don't mix up "*2" with "2", it is trait1)
  • Loading branch information
azat committed May 3, 2024
1 parent bcf7604 commit 2acec3d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/nodebuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ 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);
/// 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);
}
m_mapDepth--;
m_mergeDicts.pop_back();
Expand Down
19 changes: 18 additions & 1 deletion test/integration/load_node_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,23 @@ TEST(LoadNodeTest, MergeKeyAIterator) {
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: "
Expand All @@ -211,7 +228,7 @@ TEST(LoadNodeTest, MergeKeyB) {
EXPECT_EQ(3, node["z"]["b"].as<int>());
EXPECT_EQ(1, node["z"]["c"].as<int>());

EXPECT_EQ(1, node["w"]["a"].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>());
Expand Down

0 comments on commit 2acec3d

Please sign in to comment.