psych icon indicating copy to clipboard operation
psych copied to clipboard

deep merging

Open brewster1134 opened this issue 12 years ago • 7 comments

i noticed you cannot deep merge keys.

defaults: &defaults
  parent:
    nested_one: foo
    nested_two: bar

development:
  <<: *defaults
  parent:
    nested_one: bar

returns:

defaults:
  parent:
    nested_one: foo
    nested_two: bar
development:
  parent:
    nested_one: bar

but i was expecting:

defaults:
  parent:
    nested_one: foo
    nested_two: bar
development:
  parent:
    nested_one: bar
    nested_two: bar       # this key would exist in a proper deep merge

the yaml specs dont really cover how it is should work, but is there a deep merging solution? if it should deep merge, i can update the psych test specs.

brewster1134 avatar May 02 '12 02:05 brewster1134

I expected the same thing and am now looking for a workaround.

sethvoltz avatar Jun 04 '13 23:06 sethvoltz

This would be a great feature, I think. One could, for instance, extend the syntax so it's backwards-compatible:

# Merging in just one level
<<: *defaults

# Merging in recursively
<<: **defaults

What do you think?

sudoremo avatar Sep 02 '13 08:09 sudoremo

bump this causes me lots of config headaches.

mattbornski avatar Sep 10 '13 22:09 mattbornski

@ingydotnet do you have any opinions on how (or if) this should work?

tenderlove avatar Sep 10 '13 22:09 tenderlove

The double-asterisk @remofritzsche proposed seems like a clean backwards compatible way of handling this use case.

sethvoltz avatar Sep 10 '13 23:09 sethvoltz

First off, hash merging is one of those YAML things that is not clearly defined. It is actually in the realm of things that I don't think YAML should define. It's just a loader behavior that could be accomplished in a number of ways.

In general, a yaml loader, configged a certain way is pretty much free to interpret any YAML input, as any native memory object. The default loader config should should be as close as possible to a (cyclical) JSON-model graph.

So in terms of Psych, I think that enabling loader functionality (like merging at all), should be plugin based and not a default. However I can guess that is not practical. I would at least start towards this pattern.

Using **defaults unquoted, is a YAML 1.2, parse error. So you should pick some other syntax for the plugin to key on.

<<<: *recursive-defaults       # comes to mind

It may not be perfectly backwards compatable, But then it's not on by default.

ingydotnet avatar Sep 12 '13 21:09 ingydotnet

Any news about that? In a YAML-based configuration system, we'll need this kind of recursive merge. What is the right way to use <<: *defaults to have defaults values merged into the receiving section ? Thanks

bmedici avatar Jun 08 '16 17:06 bmedici