0

I'm essentially trying to achieve the following within my yaml file

Classroom:
    year: &year 5
    subject: &subject Math
    classroom_name: classroom_*year_*subject

What I expect

'Classroom': {
    'year': 5
    'subject': 'Math'
    'classroom_name': 'classroom_5_Math'
}

when I load it in (via Python but I don't think that changes anything?).

What I'm getting

'Classroom': {
    'year': 5
    'subject': 'Math'
    'classroom_name': 'classroom_*year_*subject'
}

What am I doing wrong / how can I improve this?

IanQ
  • 1,831
  • 5
  • 20
  • 29

2 Answers2

0

I ended up just using custom tags - StackOverflow Post and defining my own handler for that specific case. I don't think it's very scalable (not being able to self reference) but we'll see

IanQ
  • 1,831
  • 5
  • 20
  • 29
0

What is wrong is that your expectations are incorrect. In the YAML specification it clearly states:

In the representation graph, a node may appear in more than one collection. When serializing such data, the first occurrence of the node is identified by an anchor. Each subsequent occurrence is serialized as an alias node which refers back to this anchor

Your classroom_*year_*subject is a node, and this scalar is going to be loaded as a string, which happens to have two embedded asterisks, which have no special meaning except when they occur at the beginning of a scalar (refer to production rule [104] in the spec).

So that node is not a composition of a scalar (node) followed by two aliases as you seem to think (hoped). Only complete nodes can be aliased.

Anthon
  • 69,918
  • 32
  • 186
  • 246