什么是 YAML 中的复杂映射键? [英] What is a complex mapping key in YAML?

查看:36
本文介绍了什么是 YAML 中的复杂映射键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 YAML specs 中有一段关于问号的 2.11:

On the YAML specs there is a paragraph 2.11 about the question mark:

问号和空格(?")表示复杂的映射键.在块集合中,key: value 对可以紧跟在破折号、冒号或问号之后.

A question mark and space ("? ") indicate a complex mapping key. Within a block collection, key: value pairs can start immediately following the dash, colon, or question mark.

给出这个例子:

---
? - Detroit Tigers
  - Chicago cubs
:
  - 2001-07-23

另一个示例也无法转换为 XML:

This other example also fail to be converted to XML:

%YAML 1.2
- - -
!!map {
  ? !!str "Not indented"
  : !!map {
      ? !!str "By one space"
      : !!str "By four\n  spaces\n",
      ? !!str "Flow style"
      : !!seq [
          !!str "By two",
          !!str "Also by two",
          !!str "Still by two",
        ]
    }
}

不幸的是,我不明白这是什么意思.我尝试使用 codebeautify 将其转换为 XML,但出现错误.

Unfortunately I don't understand what does it means. I tried to convert this to XML using codebeautify, but I get an error.

所以我的问题是:

问号有什么作用?

推荐答案

规范不是很清楚,但经过一番摸索之后我想通了.YAML 有两种块映射键:隐式和显式.隐式风格是你熟悉的那种:

The spec isn't very clear, but after a bit of head-scratching I figured it out. YAML has two kinds of block mapping keys: implicit and explicit. The implicit style is the kind you're familiar with:

mapping:
  foo: 1
  bar baz: 2
  "qux:quux": 3

如果我们在 Ruby 中加载这个 YAML(例如),我们会得到以下结果:

If we load this YAML in Ruby (for example) we get the following result:

{ "mapping" =>
  { "foo"      => 1,
    "bar baz"  => 2,
    "qux:quux" => 3
  }
}

但是我们也可以使用显式样式来表达同样的事情:

But we can also use explicit style to express the same thing:

mapping:
  ? foo
  : 1
  ? bar baz
  : 2
  ? "qux:quux"
  : 3

换句话说,以 ? 开头的行表示键,以 : 开头的行表示值.

In other words, a line starting with ? indicates a key and a line starting with : indicates a value.

那有什么用?好吧,它让我们可以使用任何 YAML 结构作为映射键.想使用序列作为映射键吗?你可以!想使用映射作为映射键吗?看:

What use is that? Well, it lets us use any YAML structure as a mapping key. Want to use a sequence as a mapping key? You can! Want to use a mapping as a mapping key? Behold:

mapping:
  # Use a sequence as a key
  ? - foo
    - bar
  : 1

  # Use a mapping as a key
  ? baz: qux
  : 2

  # You can skip the value, which implies `null`
  ? quux

  # You can leave the key blank, which implies a `null` key
  ?
  : 3

  # You can even skip both the key and value, so both will be `null`
  ?

  # Or you can use a preposterously long scalar as a key
  ? |
    We the People of the United States, in Order to form a more
    perfect Union, establish Justice, insure domestic Tranquility,
    provide for the common defence, promote the general Welfare,
    and secure the Blessings of Liberty to ourselves and our
    Posterity, do ordain and establish this Constitution for the
    United States of America.
  : 3

  # Or just be ridiculous
  ? - foo: bar
      baz:
      - { qux: quux }
    - stahp
  : 4

在 Ruby 中,这将产生以下令人愉快的哈希:

In Ruby this would yield the following delightful hash:

{ "mapping" =>
  { [ "foo", "bar" ]   => 1,
    { "baz" => "qux" } => 2,
    "quux"             => nil,
    nil                => nil,
    "We the People of the United States, in Order to form a more\nperfect Union, establish Justice, insure domestic Tranquility,\nprovide for the common defence, promote the general Welfare,\nand secure the Blessings of Liberty to ourselves and our\nPosterity, do ordain and establish this Constitution for the\nUnited States of America.\n" => 3
    [ { "foo" => "bar", "baz" => [ { "qux" => "quux" } ] }, "stahp" ] => 4
  }
}

哦,底特律老虎队"的例子在 Ruby 解析时看起来是这样的:

Oh, and the "Detroit Tigers" example looks like this when Ruby parses it:

YAML.load <<YML
? - Detroit Tigers
  - Chicago cubs
:
  - 2001-07-23

? [ New York Yankees,
    Atlanta Braves ]
: [ 2001-07-02, 2001-08-12,
    2001-08-14 ]
YML
# => { [ "Detroit Tigers", "Chicago cubs" ] =>
#          [ #<Date: 2001-07-23 ((2452114j,0s,0n),+0s,2299161j)> ],
#      [ "New York Yankees", "Atlanta Braves" ] =>
#          [ #<Date: 2001-07-02 ((2452093j,0s,0n),+0s,2299161j)>,
#            #<Date: 2001-08-12 ((2452134j,0s,0n),+0s,2299161j)>,
#            #<Date: 2001-08-14 ((2452136j,0s,0n),+0s,2299161j)> ]
#     }

这篇关于什么是 YAML 中的复杂映射键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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