bug #25043 [Yaml] added ability for substitute aliases when mapping is 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 -------dd26c80aa9
substitute aliases in inline mappings675a3fe7f6
added ability for substitute aliases when mapping in YAML is on single line
This commit is contained in:
commit
abe6e92593
|
@ -365,6 +365,7 @@ class Inline
|
||||||
$output = array();
|
$output = array();
|
||||||
$len = strlen($mapping);
|
$len = strlen($mapping);
|
||||||
++$i;
|
++$i;
|
||||||
|
$allowOverwrite = false;
|
||||||
|
|
||||||
// {foo: bar, bar:foo, ...}
|
// {foo: bar, bar:foo, ...}
|
||||||
while ($i < $len) {
|
while ($i < $len) {
|
||||||
|
@ -384,6 +385,10 @@ class Inline
|
||||||
// key
|
// key
|
||||||
$key = self::parseScalar($mapping, array(':', ' '), array('"', "'"), $i, false);
|
$key = self::parseScalar($mapping, array(':', ' '), array('"', "'"), $i, false);
|
||||||
|
|
||||||
|
if ('<<' === $key) {
|
||||||
|
$allowOverwrite = true;
|
||||||
|
}
|
||||||
|
|
||||||
// value
|
// value
|
||||||
$done = false;
|
$done = false;
|
||||||
|
|
||||||
|
@ -395,7 +400,12 @@ class Inline
|
||||||
// Spec: Keys MUST be unique; first one wins.
|
// Spec: Keys MUST be unique; first one wins.
|
||||||
// Parser cannot abort this mapping earlier, since lines
|
// Parser cannot abort this mapping earlier, since lines
|
||||||
// are processed sequentially.
|
// 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;
|
$output[$key] = $value;
|
||||||
}
|
}
|
||||||
$done = true;
|
$done = true;
|
||||||
|
@ -406,7 +416,10 @@ class Inline
|
||||||
// Spec: Keys MUST be unique; first one wins.
|
// Spec: Keys MUST be unique; first one wins.
|
||||||
// Parser cannot abort this mapping earlier, since lines
|
// Parser cannot abort this mapping earlier, since lines
|
||||||
// are processed sequentially.
|
// 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;
|
$output[$key] = $value;
|
||||||
}
|
}
|
||||||
$done = true;
|
$done = true;
|
||||||
|
@ -419,7 +432,10 @@ class Inline
|
||||||
// Spec: Keys MUST be unique; first one wins.
|
// Spec: Keys MUST be unique; first one wins.
|
||||||
// Parser cannot abort this mapping earlier, since lines
|
// Parser cannot abort this mapping earlier, since lines
|
||||||
// are processed sequentially.
|
// 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;
|
$output[$key] = $value;
|
||||||
}
|
}
|
||||||
$done = true;
|
$done = true;
|
||||||
|
|
|
@ -22,6 +22,7 @@ yaml: |
|
||||||
foo: bar
|
foo: bar
|
||||||
foo: ignore
|
foo: ignore
|
||||||
bar: foo
|
bar: foo
|
||||||
|
bar_inline: {a: before, d: other, <<: *foo, b: new, x: Oren, c: { foo: bar, foo: ignore, bar: foo}}
|
||||||
duplicate:
|
duplicate:
|
||||||
foo: bar
|
foo: bar
|
||||||
foo: ignore
|
foo: ignore
|
||||||
|
@ -46,15 +47,20 @@ yaml: |
|
||||||
p: 12345
|
p: 12345
|
||||||
z:
|
z:
|
||||||
<<: *nestedref
|
<<: *nestedref
|
||||||
|
head_inline: &head_inline { <<: [ *foo , *dong , *foo2 ] }
|
||||||
|
recursive_inline: { <<: *head_inline, c: { <<: *foo2 } }
|
||||||
php: |
|
php: |
|
||||||
array(
|
array(
|
||||||
'foo' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'e' => 'notnull'),
|
'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' => 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'),
|
'duplicate' => array('foo' => 'bar'),
|
||||||
'foo2' => array('a' => 'Ballmer'),
|
'foo2' => array('a' => 'Ballmer'),
|
||||||
'ding' => array('fi', 'fei', 'fo', 'fam'),
|
'ding' => array('fi', 'fei', 'fo', 'fam'),
|
||||||
'check' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'e' => 'notnull', 'fi', 'fei', 'fo', 'fam', 'isit' => 'tested'),
|
'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'),
|
'head' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'e' => 'notnull', 'fi', 'fei', 'fo', 'fam'),
|
||||||
'taz' => array('a' => 'Steve', 'w' => array('p' => 1234)),
|
'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'),
|
||||||
)
|
)
|
||||||
|
|
|
@ -1207,6 +1207,18 @@ YAML;
|
||||||
|
|
||||||
$this->assertSame($expected, $this->parser->parse($yaml));
|
$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
|
class B
|
||||||
|
|
Reference in New Issue