2015-11-29 5 views
8

Auf dem YAML specs gibt es einen Absatz 2.11 über das Fragezeichen:Was ist ein komplexer Zuordnungsschlüssel in YAML?

Ein Fragezeichen und Raum („?“) Zeigt eine komplexe Mapping-Taste. Innerhalb einer Blockauflistung können Schlüssel: Wert-Paare unmittelbar nach dem Bindestrich, Doppelpunkt oder Fragezeichen beginnen.

Dieses Beispiel wird gegeben:

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

Das andere Beispiel auch in XML konvertiert werden fehlschlagen:

%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", 
     ] 
    } 
} 

Leider verstehe ich nicht, was tut es bedeutet. Ich habe versucht, dies in XML mit codebeautify zu konvertieren, aber ich bekomme einen Fehler.

Also meine Frage ist:

Was ist das Fragezeichen tun?

Antwort

23

Die Spezifikation ist nicht sehr klar, aber nach ein bisschen Kopfkratzen habe ich es herausgefunden. YAML verfügt über zwei Arten von Blockzuordnungsschlüsseln: implizit und explizit. Die implizite Stil ist die Art, die Sie vertraut sind mit:

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

Wenn wir diese YAML in Ruby (zum Beispiel) laden wir folgendes Ergebnis:

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

Aber wir können auch verwenden expliziter Stil das gleiche auszudrücken:

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

mit anderen Worten, eine Linie mit ? Start gibt einen Schlüssel und eine Zeile, die mit : zeigt einen Wert an.

Was ist das? Nun, wir können jede YAML-Struktur als Zuordnungsschlüssel verwenden. Möchten Sie eine Sequenz als Zuordnungsschlüssel verwenden? Sie können! Möchten Sie ein Mapping als Mapping-Schlüssel verwenden? Siehe da:

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 

In Ruby dies die folgenden reiz Hash ergeben würde:

{ "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 
    } 
} 

Oh, und die "Detroit Tigers" Beispiel sieht wie folgt aus, wenn Rubin parst es:

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)> ] 
#  }