Domski
Domski

Reputation: 25

Is it possible to nest YAML anchors hierarchically?

I'm new to working with YAML. I'd like to know if it is possible to 'nest' YAML anchors as I have attempted in the example below. Unfortunately, I get an error YAMLException: unidentified alias "foo" at line 13, column 19: category: *foo ^

namespace: &namespace-definition
  name: "Hellow world"
  value: "hw"
categories: &categories
  - &foo:
    name: "foo"
    namespace: *namespace-definition
  - &bar:
    name: "bar"
    namespace: *namespace-definition
keys:
  - name: "element1"
    category: *foo
  - name: "element2"
    category: *bar

Is there another way to achieve what I am trying to do, or is it impossible?

Upvotes: 1

Views: 814

Answers (1)

tinita
tinita

Reputation: 4336

The anchors in your example are named foo: and bar:, not foo and bar.

Remove the colon after them and it will work:

namespace: &namespace-definition
  name: "Hellow world"
  value: "hw"
categories: &categories
  - &foo
    name: "foo"
    namespace: *namespace-definition
  - &bar
    name: "bar"
    namespace: *namespace-definition
keys:
  - name: "element1"
    category: *foo
  - name: "element2"
    category: *bar

Upvotes: 2

Related Questions