是否有用于共享列表或地图的一部分的 YAML 语法? [英] Is there YAML syntax for sharing part of a list or map?

查看:18
本文介绍了是否有用于共享列表或地图的一部分的 YAML 语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我知道我可以做这样的事情:

So, I know I can do something like this:

sitelist: &sites
  - www.foo.com
  - www.bar.com

anotherlist: *sites

并且有 sitelistanotherlist 都包含 www.foo.comwww.bar.com.然而,我真正想要的是anotherlist also 包含www.baz.com,而不必重复www.foo.comwww.baz.com.

And have sitelist and anotherlist both contain www.foo.com and www.bar.com. However, what I really want is for anotherlist to also contain www.baz.com, without having to repeat www.foo.com and www.baz.com.

这样做会导致我在 YAML 解析器中出现语法错误:

Doing this gives me a syntax error in the YAML parser:

sitelist: &sites
  - www.foo.com
  - www.bar.com

anotherlist: *sites
  - www.baz.com

仅使用锚点和别名似乎不可能在不添加另一层子结构的情况下做我想做的事,例如:

Just using anchors and aliases it doesn't seem possible to do what I want without adding another level of substructure, such as:

sitelist: &sites
  - www.foo.com
  - www.bar.com

anotherlist:
  - *sites
  - www.baz.com

这意味着该 YAML 文件的使用者必须意识到这一点.

Which means the consumer of this YAML file has to be aware of it.

是否有一种纯粹的 YAML 方式来做这样的事情?或者我是否必须使用一些后 YAML 处理,例如实现变量替换或自动提升某些类型的子结构?我已经在做这种后处理来处理其他几个用例,所以我并不完全反对它.但是我的 YAML 文件将由人工编写,而不是机器生成,因此我希望在标准 YAML 语法基础上尽量减少用户需要记住的规则数量.

Is there a pure YAML way of doing something like this? Or will I have to use some post-YAML processing, such as implementing variable substitution or auto-lifting of certain kinds of substructure? I'm already doing that kind of post-processing to handle a couple of other use-cases, so I'm not totally averse to it. But my YAML files are going to be written by humans, not machine generated, so I would like to minimise the number of rules that need to be memorised by my users on top of standard YAML syntax.

我也希望能够用地图做类似的事情:

I'd also like to be able to do the analogous thing with maps:

namedsites: &sites
  Foo: www.foo.com
  Bar: www.bar.com

moresites: *sites
  Baz: www.baz.com

我已经搜索过 YAML 规范,但无法'找不到任何东西,所以我怀疑答案只是不,你不能这样做".但如果有人有任何想法,那就太好了.

I've had a search through the YAML spec, and couldn't find anything, so I suspect the answer is just "no you can't do this". But if anyone has any ideas that would be great.

由于没有答案,我假设没有人发现 YAML 规范中没有的任何内容,并且这不能在 YAML 层完成.因此,我提出了对 YAML 进行后处理的想法,以帮助解决此问题,以防将来有人发现此问题.

Since there have been no answers, I'm presuming that no one has spotted anything I haven't in the YAML spec and that this can't be done at the YAML layer. So I'm opening up the question to idea for post-processing the YAML to help with this, in case anyone finds this question in future.

推荐答案

合并键类型 可能是您想要的.它使用特殊的 << 映射键来指示合并,允许映射的别名(或此类别名的序列)用作初始化程序以合并到单个映射中.此外,您仍然可以显式覆盖值,或添加更多不存在于合并列表中的值.

The merge key type is probably what you want. It uses a special << mapping key to indicate merges, allowing an alias to a mapping (or a sequence of such aliases) to be used as an initializer to merge into a single mapping. Additionally, you can still explicitly override values, or add more that weren't present in the merge list.

请务必注意,它适用于映射,而不是作为第一个示例的序列.当您考虑它时,这是有道理的,并且您的示例看起来可能无论如何都不需要是连续的.只需将序列值更改为映射键就可以解决问题,如下面(未经测试的)示例所示:

It's important to note that it works with mappings, not sequences as your first example. This makes sense when you think about it, and your example looks like it probably doesn't need to be sequential anyway. Simply changing your sequence values to mapping keys should do the trick, as in the following (untested) example:

sitelist: &sites
  ? www.foo.com  # "www.foo.com" is the key, the value is null
  ? www.bar.com

anotherlist:
  << : *sites    # merge *sites into this mapping
  ? www.baz.com  # add extra stuff

需要注意的一些事情.首先,由于 << 是一个键,所以每个节点只能指定一次.其次,当使用序列作为值时,顺序很重要.这在此处的示例中无关紧要,因为没有关联的值,但值得注意.

Some things to notice. Firstly, since << is a key, it can only be specified once per node. Secondly, when using a sequence as the value, the order is significant. This doesn't matter in the example here, since there aren't associated values, but it's worth being aware.

这篇关于是否有用于共享列表或地图的一部分的 YAML 语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆