Ruby-on-rails 有没有办法在 YAML 中别名/锚定数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4948933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:22:18  来源:igfitidea点击:

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

ruby-on-railsyamlassetsjammit

提问by James A. Rosen

I'm using Jammitto 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 Sammyand its plugins to be in both my mobileand screenJS packages.

我正在使用Jammit为 Rails 应用程序打包资产,我有一些资产文件,我希望将它们包含在几个组中。例如,我希望Sammy及其插件同时包含在 mymobilescreenJS 包中。

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?

但两者都将 Sammy JS 文件放在一个嵌套的数组中,Jammit 无法理解。是否有将 Array 的元素直接包含在另一个 Array 中的语法?

NB: I realize that in this case there are only two elements in the SAMMYArray, 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.

注意:我意识到在这种情况下,SAMMY数组中只有两个元素,因此在每个包中给每个元素一个别名和引用也不会太糟糕。对于这种情况,这很好,但是当有五个或十个具有特定加载顺序的元素时,很快就会变得无法维护。

采纳答案by Jesse Beder

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:

您的示例是有效的 YAML(一个方便的检查位置是YPaste),但它没有定义合并的作用。根据规范,合并键可以有一个值:

  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.
  1. 一个映射,在这种情况下它被合并到父映射中。
  2. 一系列映射,在这种情况下,每个映射都被一个一个地合并到父映射中。

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

无法在 YAML 级别合并序列

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

但是,您可以在代码中执行此操作。使用您的第二个想法中的 YAML:

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

(当然,如果您有一个更复杂的 YAML 文件,并且您不希望每个序列都变平(或者它们不是所有序列),则必须进行一些过滤。)

回答by yngve

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.

注意:根据yaml-online-parser,您的第一个建议不是有效使用 << (用于合并来自两个字典的键。然后锚点必须指向我相信的另一个字典。

回答by sepp2k

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

如果你想mobile等于sammy,你可以这样做:

mobile: *SAMMY

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

但是,如果您想mobile包含除sammy.

回答by Eric Woodruff

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.

正如有人建议的那样,当您需要展平列表时,至少在 ruby​​ 中,将“!flatten”类型说明符添加到 mobile 并实现一个扩展 Array 的类,添加 yaml_tag 并将编码器 seq 展平是微不足道的init_with。