yaml-cpp icon indicating copy to clipboard operation
yaml-cpp copied to clipboard

Merge 2 YAML::Node

Open choucavalier opened this issue 8 years ago • 4 comments

I use YAML for configuring a software. It looks like this

foo: bar
barafoo:
  footabar: foofoofoo
  tabafoorab: batabataba
fabfab: 23

I hard-code default values for all of these settings in a default YAML::Node and retrieve user-specified settings from an optional file in /etc/foo/config.yml.

Say /etc/foo/config.yml contains the following

barafoo:
  tabafoorab: heyhey
fabfab: 42

I would like to use all the settings from the default YAML::Node (hardcoded in the software) but override those settings if they have different values in /etc/foo/config.yml.

In a sense, I would like to merge 2 YAML::Node, taking all keys from the first one and giving priority to keys in the second one.

Is there something already implemented in yaml-cpp or do I need to write my own function to do that?

choucavalier avatar Jun 06 '17 10:06 choucavalier

I found an answer here https://stackoverflow.com/questions/41326112/how-to-merge-node-in-yaml-cpp but I wonder if there's a better way.

choucavalier avatar Jun 06 '17 12:06 choucavalier

I have exactly the same use case, default configuration plus (partial) override by secondary configuration files. And I would expect this to be a very, very common and recurring use case for many people..

mfrigerio17 avatar Mar 09 '22 10:03 mfrigerio17

@mfrigerio17 that was my use-case and i ended up using the stackoverflow solution above

choucavalier avatar Mar 09 '22 10:03 choucavalier

This is kind of an auxiliary issue, I think, but I'd accept a PR into the /contrib folder if someone wants to write one or clean up the SO post.

jbeder avatar Apr 01 '22 03:04 jbeder