Is there a way to alias/anchor an array in YAML?

YamlAssets

Yaml Problem Overview


I'm using Jammit to package assets up for a Rails application and I have a few asset files that I'd like to be included in each of a few groups. For example, I'd like Sammy and its plugins to be in both my mobile and screen JS packages.

I've tried this:

sammy: &SAMMY
  - public/javascripts/vendor/sammy.js
  - public/javascripts/vendor/sammy*.js

mobile:
  <<: *SAMMY
  - public/javascripts/something_else.js

and this:

mobile:
  - *SAMMY

but both put the Sammy JS files in a nested Array, which Jammit can't understand. Is there a syntax for including the elements of an Array directly in another Array?

NB: I realize that in this case there are only two elements in the SAMMY Array, so it wouldn't be too bad to give each an alias and reference both in each package. That's fine for this case, but quickly gets unmaintainable when there are five or ten elements that have a specific load order.

Yaml Solutions


Solution 1 - Yaml

Closest solution I know of is this one:

sammy:
  - &SAMMY1
    public/javascripts/vendor/sammy.js
  - &SAMMY2
    public/javascripts/vendor/sammy*.js

mobile:
  - *SAMMY1
  - *SAMMY2
  - public/javascripts/something_else.js

Alternatively, as already suggested, flatten the nested lists in a code snippet.

Note: according to yaml-online-parser, your first suggestion is not a valid use of << (used to merge keys from two dictionaries. The anchor then has to point to another dictionary I believe.

Solution 2 - Yaml

If you want mobile to be equal to sammy, you can just do:

mobile: *SAMMY

However if you want mobile to contain other elements in addition to those in sammy, there's no way to do that in YAML to the best of my knowledge.

Solution 3 - Yaml

Your example is valid YAML (a convenient place to check is YPaste), but it's not defined what the merge does. Per the spec, a merge key can have a value:

  1. A mapping, in which case it's merged into the parent mapping.
  2. A sequence of mappings, in which case each is merged, one-by-one, into the parent mapping.

There's no way of merging sequences on YAML level.

You can, however, do this in code. Using the YAML from your second idea:

mobile:
  - *SAMMY

you'll get nested sequences - so flatten them! Assuming you have a mapping of such nested sequences:

data = YAML::load(File.open('test.yaml'))
data.each_pair { |key, value| value.flatten! }

(Of course, if you have a more complicated YAML file, and you don't want every sequence flattened (or they're not all sequences), you'll have to do some filtering.)

Solution 4 - Yaml

This solution is for Symfony/PHP only (considerations for other languages, see below)

Note about array keys from the PHP array manual page:

> Strings containing valid decimal ints, unless the number is preceded by a + sign, will be cast to the int type. E.g. the key "8" will actually be stored under 8. [...]

This means that if you actually index your anchor array with integer keys, you can simply add new keys by continuing the initial list. So your solution would look like this:

sammy: &SAMMY
  1: public/javascripts/vendor/sammy.js
  2: public/javascripts/vendor/sammy*.js

mobile:
  <<: *SAMMY
  3: public/javascripts/something_else.js

You can even overwrite keys and still add new ones:

laptop:
  <<: *SAMMY
  1: public/javascripts/sammy_laptop.js
  3: public/javascripts/something_else.js

In both cases the end result is a perfectly valid indexed array, just like before.


Other programming languages

Depending on your YAML implementation and how you iterate over your array, this could conceivably also be used in other programming languages. Though with a caveat.

For instance, in JS you can access numerical string keys by their integer value as well:

const sammy = {"1": "public/javascripts/vendor/sammy.js"}

sammy["1"]; // "public/javascripts/vendor/sammy.js"
sammy[1]; // "public/javascripts/vendor/sammy.js"

But you'd need to keep in mind, that your initial array is now an object, and that you would need to iterate over it accordingly, e.g.:

Object.keys(sammy).forEach(key => console.log(sammy[key]))

Solution 5 - Yaml

As it has been suggested, when you need to flatten a list, at least in ruby, it is trivial to add a "!flatten" type specifier to mobile and implement a class that extends Array, adds the yaml_tag and flattens the coder seq on init_with.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames A. RosenView Question on Stackoverflow
Solution 1 - YamlyngveView Answer on Stackoverflow
Solution 2 - Yamlsepp2kView Answer on Stackoverflow
Solution 3 - YamlJesse BederView Answer on Stackoverflow
Solution 4 - YamlfluView Answer on Stackoverflow
Solution 5 - YamlEric WoodruffView Answer on Stackoverflow