Hi guys, Quick q: are yaml merge keys supported? ...
# hoplite
d
Hi guys, Quick q: are yaml merge keys supported? Glimpsing over the Yaml parser I see references to anchors/aliases at the yaml parser, which are very related features, but nothing on merge keys. I know merge keys are not part of the 1.2 spec, only 1.1, but they are quite handy 😄 I’ll leave some details in a just for the sake of completion 🧵 Thank you!
Config classes:
Copy code
data class Config(val list: List<Element>)
data class Element(val a: String, val b: String, val c: String)
Yaml file (config):
Copy code
defaults: &def
  a: a-value
  b: b-value
  
list:
  - <<: *def
    c: c1-value
  - <<: *def
    c: c2-value
Error:
Copy code
- Could not instantiate 'Config' because:
  - 'list': Collection element decode failure (classpath:/test.yaml:5:2):
    - Could not instantiate 'Element' because:
      - 'a': Missing from config
      - 'b': Missing from config
    - Could not instantiate 'Element' because:
      - 'a': Missing from config
      - 'b': Missing from config
s
The engine only supports whatever snake yml supports basically
Did they remove merge keys in 1.2 ?
c
Looks like they were removed in 1.2:
• The merge
<<
and value
=
special mapping keys have been removed.
https://yaml.org/spec/1.2.2/ext/changes/#:~:text=The%20most%20significant%20difference%20between,the%20YAML%201.1%20type%20library.
d
Yep, unfortunately. If I recall correctly, they were meant to draft an alternative for 1.3, but I have no idea of the status tbh
I’m not familiar at all with SnakeYAML, only have used it indirectly through another dependency, but I’m pretty sure I’ve used merge keys with some of those. Not sure how they manage that (maybe bind the parser to a different spec version?)
Hi Sam, Had a quick look at the parser yesterday and it is indeed supported by the snake yaml version that hoplite relies on (snakeyaml is 1.1 and snakeyaml-engine is 1.2). However, when merge-keys are used, the projection of fields at map nodes essentially attaches the referenced map node directly to the current field being processed (
<<
), rather than merging its fields into the current node. So this:
Copy code
defaults: &def
  a: a-value
  b: b-value
  
list:
  - <<: *def
    c: c1-value
Sort-of turns into:
Copy code
list:
  - <<:
      a: a-value
      b: b-value
    c: c1-value
Thus it won’t be able to see
a
and
b
directly on that list element, throwing the error that I’ve included above (1st message on this thread). I’ve implemented a (quick) fix - not sure if it’s the best approach, but at least serves as a baseline to see merge-keys working. https://github.com/sksamuel/hoplite/compare/master...davidafsilva:hoplite:feature/merge-keys-support Would you be interested in supporting this in hoplite-yaml, or do you think it might not be worth the effort?