How to merge node in yaml-cpp

2.8k views Asked by At

I have two node object, like this:

school:
  grade:
    class:
     name: bob
school:
  grade:
    class:
      age: 18

I want to merge it, the result like this:

school:
  grade:
    class:
      name: bob
      age: 18

How to merge it? when the node size and depth do not kown.

2

There are 2 answers

6
md5i On BEST ANSWER

Here is my attempt:

#include <yaml-cpp/yaml.h>

inline const YAML::Node & cnode(const YAML::Node &n) {
    return n;
}

YAML::Node merge_nodes(YAML::Node a, YAML::Node b)
{
  if (!b.IsMap()) {
    // If b is not a map, merge result is b, unless b is null
    return b.IsNull() ? a : b;
  }
  if (!a.IsMap()) {
    // If a is not a map, merge result is b
    return b;
  }
  if (!b.size()) {
    // If a is a map, and b is an empty map, return a
    return a;
  }
  // Create a new map 'c' with the same mappings as a, merged with b
  auto c = YAML::Node(YAML::NodeType::Map);
  for (auto n : a) {
    if (n.first.IsScalar()) {
      const std::string & key = n.first.Scalar();
      auto t = YAML::Node(cnode(b)[key]);
      if (t) {
        c[n.first] = merge_nodes(n.second, t);
        continue;
      }
    }
    c[n.first] = n.second;
  }
  // Add the mappings from 'b' not already in 'c'
  for (auto n : b) {
    if (!n.first.IsScalar() || !cnode(c)[n.first.Scalar()]) {
      c[n.first] = n.second;
    }
  }
  return c;
}

For non-scalar keys I have opted to ignore node equivalence. Please note that this version does not modify a. It returns a new map c which is a merge of b into a. Values from b will replace identically keyed non-map values from a in the c map.

0
Tates On

I found an issue with md5i's answer that it doesn't merge the second node's unique sub nodes. My fix was calling the function again in the for loop of node b (which I renamed the override node). I also made everything const, because I'm not editing anything here, and so I don't have to cast it. I'm also reinforcing the fact the second node is overriding the other.

const YAML::Node mergeNodes(const YAML::Node& defaultNode, const YAML::Node& overrideNode)
{
  if (!overrideNode.IsMap()) {
    // If overrideNode is not a map, merge result is overrideNode, unless overrideNode is null
    return overrideNode.IsNull() ? defaultNode : overrideNode;
  }
  if (!defaultNode.IsMap()) {
    // If defaultNode is not a map, merge result is overrideNode
    return overrideNode;
  }
  if (!defaultNode.size()) {
    return YAML::Node(overrideNode);
  }
  // Create a new map 'newNode' with the same mappings as defaultNode, merged with overrideNode
  auto newNode = YAML::Node(YAML::NodeType::Map);
  for (auto node : defaultNode) {
    if (node.first.IsScalar()) {
      const std::string& key = node.first.Scalar();
      if (overrideNode[key]) {
        newNode[node.first] = mergeNodes(node.second, overrideNode[key]);
        continue;
      }
    }
    newNode[n.first] = node.second;
  }
  // Add the mappings from 'overrideNode' not already in 'newNode'
  for (auto node : overrideNode) {
    if (!node.first.IsScalar()) {
      const std::string& key = node.first.Scalar();
      if (defaultNode[key]) {
        newNode[node.first] = mergeNodes(defaultNode[key], node.second);
        continue;
      }
    }
    newNode[node.first] = node.second;
  }
  return YAML::Node(newNode);
}

This will probably not do the N thingy any favours, but it will take the nodes from node b/overrideNode even if they have no instance in node a/defaultNode.