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

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

问题描述

所以,我知道我可以这样做:

So, I know I can do something like this:

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

anotherlist: *sites

并且 sitelist anotherlist 都包含 www.foo.com www.bar.com 。但是,我真正想要的是 anotherlist to 包含 www.baz.com ,而不必重复 www.foo.com www.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 spec ,找不到任何东西,所以我怀疑答案只是不,你不能这样做。但是如果有任何人有任何想法会很棒。

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天全站免登陆