Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

yaml

assets

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.

like image 744
James A. Rosen Avatar asked Feb 09 '11 18:02

James A. Rosen


People also ask

How do I use anchors in Yaml?

YAML Anchors and Alias You can place Anchors ( & ) on an entity to mark a multi-line section. You can then use an Alias ( * ) call that anchor later in the document to reference that section. Anchors and Aliases are very helpful for larger projects as they cut visual clutter caused by extra lines.

How do you represent an array in Yaml?

The block sequence style of YAML uses hyphens or dashes to ( - ) to represent arrays. A hyphen ( - ) followed by white space ( ) represents an element of an array. When you enter the dashes, you need to ensure that all items are at the same indentation level.

What is an anchor in Yaml configuration?

YAML anchors are a feature which let you identify an item and then reference it elsewhere in your file. Anchors are created using the & sign. The sign is followed by an alias name. You can use this alias later to reference the value following the anchor.

What is a node in Yaml?

The native data structure in YAML includes simple representations such as nodes. It is also called as Representation Node Graph. It includes mapping, sequence and scalar quantities which is being serialized to create a serialization tree. With serialization the objects are converted with stream of bytes.


1 Answers

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.

like image 66
yngve Avatar answered Sep 21 '22 09:09

yngve