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

Support for "<<: reference"

Open JordiPolo opened this issue 8 years ago • 9 comments

I'm surprised there is no reference to this in the documentation as in my experience is widely used.

This is a good example of the expected output given "<<": https://gist.github.com/bowsersenior/979804

But I get: https://gist.github.com/JordiPolo/ce19f90de8ffc8ac31721a5961d1cc77

JordiPolo avatar May 09 '17 09:05 JordiPolo

FWIW I also needed this and came across https://gitlab.kitware.com/utils/rust-yaml-merge-keys, which adds support for << on top on this library.

Would still love it if it could be supported by yaml-rust by default, though!

alyssais avatar May 10 '17 22:05 alyssais

Thanks for the pointer. I've tried to make it work (I'm using serde-yaml) but it seems I'm not able to. Support here would be ideal as it would just work™

JordiPolo avatar May 11 '17 13:05 JordiPolo

yaml-merge-keys developer here. It supports serde-yaml using the serde_yaml feature. We use it in our code just fine.

mathstuf avatar Nov 08 '17 22:11 mathstuf

@mathstuf I don't think it was questioned whether it works. The relevant function merge_keys_serde simply does not show up in the documentation on docs.rs (feature not enabled), so one has to find it in the source code of your library.

Since this issue comes up in a google search, here's a minimal example:

let value = serde_yaml::from_str(&input_yaml_string).unwrap(); // Value
let merged = yaml_merge_keys::merge_keys_serde(value).unwrap(); // merged Value
let typed_value: T = serde_yaml::from_value(merged).unwrap(); // merged T

sinesc avatar Feb 11 '18 20:02 sinesc

I looked into it and it now seems possible to specify features for documentation builds. I'll do that for the crate.

mathstuf avatar Feb 12 '18 13:02 mathstuf

Ah, I had done so, but hadn't made a release since then.

mathstuf avatar Feb 12 '18 13:02 mathstuf

I've just published 0.2.1.

mathstuf avatar Feb 12 '18 13:02 mathstuf

There is a spec for this, althouth it is not part of 1.2 itself: https://yaml.org/type/merge.html

tmccombs avatar Jan 12 '20 07:01 tmccombs

I've seen multiple parsers get very loose around the merge key spec. Apparently this is valid for the Ruby parser:

foo:
  <<: *bar
  <<: *baz

(given anchors named bar and baz) but seems like…quite cavalier YAML without the supplemental spec. If you want to apply the merge keys spec, my crate handles that for you and it works with plain yaml-rust and serde_yaml. Other YAML libraries can be considered (through feature flags).

mathstuf avatar Jan 14 '20 20:01 mathstuf