mustangostang / spyc

A simple YAML loader/dumper class for PHP
MIT License
707 stars 207 forks source link

Support for compact inline notation #9

Open swt83 opened 11 years ago

swt83 commented 11 years ago

I've never used YAML before, and I am not familiar w/ the spec rules, but I need it for a certain project. In this project, I'm finding that SPYC doesn't parse multidimensional arrays properly in every case, but oddly if I add a space before every "-" character in the file it will parse correctly. Here is some sample YAML that I'm using as a test:

Does not work:

- alpha:
    foo: bar
    bar:
    - foo
  beta:
    foo: bar
    bar: foo
  charlie:
    foo: bar
    bar: foo
  delta:
  - foo: bar
    bar: foo
  - foo: bar
    bar: foo

Works:

 - alpha:
    foo: bar
    bar:
     - foo
  beta:
    foo: bar
    bar: foo
  charlie:
    foo: bar
    bar: foo
  delta:
   - foo: bar
    bar: foo
   - foo: bar
    bar: foo

Maybe the file I'm using as my source is formatted incorrectly. Maybe there is a param in the class that I am missing that covers this.

swt83 commented 11 years ago

I'm using this to patch the string before parsing:

// iterate thru string
$final = '';
$lines = explode("\n", $string);
foreach ($lines as $line)
{
    // check for dash...
    $trim = ltrim($line);
    if (substr($trim, 0, 1) === '-')
    {
        // bump space
        $line = ' '.$line;
    }

    // add back to string
    $final .= $line."\n";
}

// return
return $final;
ryanuber commented 11 years ago

What you are seeing is that the original YAML you are feeding in is using compact in-line notation for block sequences. This was introduced in YAML 1.1. Spyc, being a pure PHP implementation, only supports a subset of YAML, which means it doesn't strictly adhere to any particular version of the spec.

That aside, it would be REALLY nice to have compact in-line notations read normally in Spyc - because most YAML emitters will dump in this format.