yaml-rust
yaml-rust copied to clipboard
Support for "<<: reference"
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
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!
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™
yaml-merge-keys developer here. It supports serde-yaml using the serde_yaml feature. We use it in our code just fine.
@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
I looked into it and it now seems possible to specify features for documentation builds. I'll do that for the crate.
Ah, I had done so, but hadn't made a release since then.
I've just published 0.2.1.
There is a spec for this, althouth it is not part of 1.2 itself: https://yaml.org/type/merge.html
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).