circe-yaml
circe-yaml copied to clipboard
Support merge keys
SnakeYAML supports YAML's "merge key" extension, which makes the last four elements in this sequence resolve to the same thing:
scala> val doc = """
| ---
| - &CENTER { x: 1, y: 2 }
| - &LEFT { x: 0, y: 2 }
| - &BIG { r: 10 }
| - &SMALL { r: 1 }
|
| # All the following maps are equal:
|
| - # Explicit keys
| x: 1
| y: 2
| r: 10
| label: center/big
|
| - # Merge one map
| << : *CENTER
| r: 10
| label: center/big
|
| - # Merge multiple maps
| << : [ *CENTER, *BIG ]
| label: center/big
|
| - # Override
| << : [ *BIG, *LEFT, *SMALL ]
| x: 1
| label: center/big
| """
This functionality isn't currently available in circe-yaml, which does resolve the references but returns JSON objects with "<<"
keys in them.
I need this functionality, and while I could hack it with cursors or something I'd rather take advantage of the fact that SnakeYAML can do it for me. This seems pretty easy—you can just subclass org.yaml.snakeyaml.constructor.SafeConstructor
and call its protected flattenMapping
on your mapping nodes.
I'm assuming we don't want this to be the default behavior, so I think we'll want some way to set options either in the parser itself or in the yamlToJson
method. I'm happy to take a stab at this as soon as possible but if anyone has ideas about how it should look, please let me know.
/cc @jeremyrsmith @jeffmay @jonas @denisrosset
I'm +1 for making this the default behavior, and also +1 on making it the only behavior (in order to avoid having to add configuration cruft)
Edit: I anticipate a 0% chance that anyone using the key <<
does not intend to use it this way. But if that does happen, we can always add a way to configure it at that point.
Okay @jeremyrsmith, I'm convinced—I'll PR this now and then publish it in a quick 0.6.2, and if anyone complains we can revisit in a future release.
I agree with @jeremyrsmith as well about making this the default behavior.