yaml-rust
yaml-rust copied to clipboard
"<<:" merge operator is not a key
fn test_yaml()
{
let s =
"
%YAML 1.2
---
defaults: &super
a: valueA
b: valueB
sub:
<<: *super
b: valueB_new
c: valueC
";
// load:
let docs = YamlLoader::load_from_str(s).unwrap();
let doc = &docs[0];
println!("doc: {:?}", doc);
// dump & print:
let mut out_str = String::new();
{
let mut emitter = YamlEmitter::new(&mut out_str);
emitter.dump(doc).unwrap();
}
println!("rerender:\n{}", out_str);
}
Current unexpected output:
rerender:
---
defaults:
a: valueA
b: valueB
sub:
"<<":
a: valueA
b: valueB
b: valueB_new
c: valueC
There "<<": is a key.
I agree that it would be nice to have the merge key at some point, because it is popular for defining default values.
Please note, however, that it is not part of the official YAML 1.2 specification, but an add-on to the YAML 1.1 specification defined at http://yaml.org/type/merge.html
Anyway I would not recommend using the yaml::Yaml data type at this point in time for production uses, as it is not secure.
See https://crates.io/crates/yaml-merge-keys for an implementation of the spec.
Also, this is a duplicate of #68.