Skip to content

Commit

Permalink
bug #25043 [Yaml] added ability for substitute aliases when mapping i…
Browse files Browse the repository at this point in the history
…s on single line (Michał Strzelecki, xabbuh)

This PR was merged into the 2.7 branch.

Discussion
----------

[Yaml] added ability for substitute aliases when mapping is on single line

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #11569
| License       | MIT
| Doc PR        |

Commits
-------

dd26c80 substitute aliases in inline mappings
675a3fe added ability for substitute aliases when mapping in YAML is on single line
  • Loading branch information
fabpot committed Nov 23, 2017
2 parents a809ab2 + dd26c80 commit abe6e92
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
22 changes: 19 additions & 3 deletions src/Symfony/Component/Yaml/Inline.php
Expand Up @@ -365,6 +365,7 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
$output = array();
$len = strlen($mapping);
++$i;
$allowOverwrite = false;

// {foo: bar, bar:foo, ...}
while ($i < $len) {
Expand All @@ -384,6 +385,10 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
// key
$key = self::parseScalar($mapping, array(':', ' '), array('"', "'"), $i, false);

if ('<<' === $key) {
$allowOverwrite = true;
}

// value
$done = false;

Expand All @@ -395,7 +400,12 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
// Spec: Keys MUST be unique; first one wins.
// Parser cannot abort this mapping earlier, since lines
// are processed sequentially.
if (!isset($output[$key])) {
// But overwriting is allowed when a merge node is used in current block.
if ('<<' === $key) {
foreach ($value as $parsedValue) {
$output += $parsedValue;
}
} elseif ($allowOverwrite || !isset($output[$key])) {
$output[$key] = $value;
}
$done = true;
Expand All @@ -406,7 +416,10 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
// Spec: Keys MUST be unique; first one wins.
// Parser cannot abort this mapping earlier, since lines
// are processed sequentially.
if (!isset($output[$key])) {
// But overwriting is allowed when a merge node is used in current block.
if ('<<' === $key) {
$output += $value;
} elseif ($allowOverwrite || !isset($output[$key])) {
$output[$key] = $value;
}
$done = true;
Expand All @@ -419,7 +432,10 @@ private static function parseMapping($mapping, &$i = 0, $references = array())
// Spec: Keys MUST be unique; first one wins.
// Parser cannot abort this mapping earlier, since lines
// are processed sequentially.
if (!isset($output[$key])) {
// But overwriting is allowed when a merge node is used in current block.
if ('<<' === $key) {
$output += $value;
} elseif ($allowOverwrite || !isset($output[$key])) {
$output[$key] = $value;
}
$done = true;
Expand Down
8 changes: 7 additions & 1 deletion src/Symfony/Component/Yaml/Tests/Fixtures/sfMergeKey.yml
Expand Up @@ -22,6 +22,7 @@ yaml: |
foo: bar
foo: ignore
bar: foo
bar_inline: {a: before, d: other, <<: *foo, b: new, x: Oren, c: { foo: bar, foo: ignore, bar: foo}}
duplicate:
foo: bar
foo: ignore
Expand All @@ -46,15 +47,20 @@ yaml: |
p: 12345
z:
<<: *nestedref
head_inline: &head_inline { <<: [ *foo , *dong , *foo2 ] }
recursive_inline: { <<: *head_inline, c: { <<: *foo2 } }
php: |
array(
'foo' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'e' => 'notnull'),
'bar' => array('a' => 'before', 'd' => 'other', 'e' => null, 'b' => 'new', 'c' => array('foo' => 'bar', 'bar' => 'foo'), 'x' => 'Oren'),
'bar_inline' => array('a' => 'before', 'd' => 'other', 'b' => 'new', 'c' => array('foo' => 'bar', 'bar' => 'foo'), 'e' => 'notnull', 'x' => 'Oren'),
'duplicate' => array('foo' => 'bar'),
'foo2' => array('a' => 'Ballmer'),
'ding' => array('fi', 'fei', 'fo', 'fam'),
'check' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'e' => 'notnull', 'fi', 'fei', 'fo', 'fam', 'isit' => 'tested'),
'head' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'e' => 'notnull', 'fi', 'fei', 'fo', 'fam'),
'taz' => array('a' => 'Steve', 'w' => array('p' => 1234)),
'nested' => array('a' => 'Steve', 'w' => array('p' => 12345), 'd' => 'Doug', 'z' => array('p' => 12345))
'nested' => array('a' => 'Steve', 'w' => array('p' => 12345), 'd' => 'Doug', 'z' => array('p' => 12345)),
'head_inline' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'e' => 'notnull', 'fi', 'fei', 'fo', 'fam'),
'recursive_inline' => array('a' => 'Steve', 'b' => 'Clark', 'c' => array('a' => 'Ballmer'), 'e' => 'notnull', 'fi', 'fei', 'fo', 'fam'),
)
12 changes: 12 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Expand Up @@ -1207,6 +1207,18 @@ public function testParseReferencesOnMergeKeys()

$this->assertSame($expected, $this->parser->parse($yaml));
}

/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
* @expectedExceptionMessage Reference "foo" does not exist
*/
public function testEvalRefException()
{
$yaml = <<<EOE
foo: { &foo { a: Steve, <<: *foo} }
EOE;
$this->parser->parse($yaml);
}
}

class B
Expand Down

0 comments on commit abe6e92

Please sign in to comment.