James A. Rosen
James A. Rosen

Reputation: 65232

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

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.

Upvotes: 109

Views: 77046

Answers (6)

kdavh
kdavh

Reputation: 414

Here is a valid example using just yaml that includes more complicated items (like objects with multiple keys) in the list:

sammy:
  - &A1
    public/javascripts/vendor/sammy.js: asdf
    public/javascripts/vendor/sammyz.js: asdfz
  - &A2
    public/javascripts/vendor/sammy2.js: asdf2
    public/javascripts/vendor/sammy2z.js: asdf2z

mobile: [*A1, *A2, { 
  public/javascripts/something_else.js: else,
  public/javascripts/something_else2.js: else2,

}]

JSON representation of output:

{
  "mobile": [
    {
      "public/javascripts/vendor/sammy.js": "asdf", 
      "public/javascripts/vendor/sammyz.js": "asdfz"
    }, 
    {
      "public/javascripts/vendor/sammy2.js": "asdf2", 
      "public/javascripts/vendor/sammy2z.js": "asdf2z"
    }, 
    {
      "public/javascripts/something_else.js": "else", 
      "public/javascripts/something_else2.js": "else2"
    }
  ], 
  "sammy": [
    ...
  ]
}

You get brevity, but the trade-off is needing to use explicit bracket syntax.

Upvotes: 0

flu
flu

Reputation: 14683

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]))

Upvotes: 1

Jesse Beder
Jesse Beder

Reputation: 34034

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.)

Upvotes: 37

Eric Woodruff
Eric Woodruff

Reputation: 6410

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.

Upvotes: -4

yngve
yngve

Reputation: 1546

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.

Upvotes: 79

sepp2k
sepp2k

Reputation: 370112

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.

Upvotes: 50

Related Questions