2015-11-29 18 views
8

Sul YAML specs c'è un paragrafo 2.11 sul punto di domanda:Che cos'è una chiave di mappatura complessa in YAML?

un punto interrogativo e spazio (“?”) Indicare una chiave di mappatura complessa. All'interno di una raccolta di blocchi, la chiave: coppie di valori possono iniziare immediatamente dopo il trattino, i due punti o il punto interrogativo.

Questo esempio è dato:

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

Questo altro esempio anche non essere convertito in 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", 
     ] 
    } 
} 

Purtroppo non capisco cosa significa. Ho provato a convertire questo in XML usando codebeautify, ma ottengo un errore.

Quindi la mia domanda è:

Che cosa fa il punto interrogativo fare?

risposta

23

La specifica non è molto chiara, ma dopo un po 'di grattacapi l'ho capito. YAML ha due tipi di chiavi di mappatura dei blocchi: implicite ed esplicite. Lo stile implicita è il tipo si ha familiarità con:

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

Se carichiamo questo YAML in Ruby (per esempio) si ottiene il seguente risultato:

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

Ma possiamo anche usare stile esplicito per esprimere la stessa cosa:

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

In altre parole, una riga che inizia con ? indica una chiave e una linea che parte con : indica un valore.

A che serve? Bene, ci consente di utilizzare qualsiasi struttura YAML come chiave di mappatura. Vuoi utilizzare una sequenza come chiave di mappatura? Puoi! Vuoi utilizzare una mappatura come chiave di mappatura? Ecco:

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 ciò produrrebbe il seguente deliziosa 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 
    } 
} 

Oh, e l'esempio "Detroit Tigers" assomiglia a questo quando Rubino analizza:

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