8000 [Yaml] added ability for substitute aliases when mapping is on single line by xabbuh · Pull Request #25043 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
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])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These conditions are a bit hard to understand because above you have this: if ('<<' === $key) { $allowOverwrite = true; }. So, if('<<' === $key) and if($allowOverwrite) are interchangeable and this if ... elseif is a bit confusing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is executed several times (for each entry in the mapping). So $allowOverwrite can be true in a subsequent iteration where $key is different from <<.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javiereguiluz Is there still anything you would like to see changed?

$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
Original file line number Diff line number Diff line change
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' => 8000 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
Original file line number Diff line number Diff line change
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