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

Skip to content

added ability for substitute aliases when mapping is on single line #9

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

Closed
wants to merge 1 commit into from
Closed
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
57 changes: 56 additions & 1 deletion Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,59 @@ private function moveToPreviousLine()
$this->currentLine = $this->lines[--$this->currentLineNb];
}

/**
* Substitute from alias where is used a <<
*
* @param mixed $values Parsed YAML in which aliases are not substituted
*
* @return mixed YAML with substituted aliases
*
* @throws Exception\ParseException When indentation problem are detected
*/
private function substituteAliases($values)
{
if (is_array($values)) {
$keys = array();
foreach ($values as $key => $value) {
if ($key ==='<<' && preg_grep('/^\*.+/', (array) $value) === array_values((array) $value)) {
$values[$key] = array();
foreach ((array) $value as $ref) {
$refName = substr($ref, 1);
if (!array_key_exists($refName, $this->refs)) {
throw new ParseException(
sprintf('Reference "%s" does not exist.', $refName),
$this->getRealCurrentLineNb() + 1,
$this->currentLine
);
}

$keys = array_merge(
$keys,
array_diff(array_keys($this->refs[$refName]), $keys)
);
$values[$key] = array_replace($this->refs[$refName], $values[$key]);
}
} elseif (!isset($result[$key]) || is_array($result[$key])) {
$keys[] = $key;
$values[$key] = $this->substituteAliases($value);
}
}

if (isset($values['<<'])) {
$values = array_replace($values['<<'], $values);
unset($values['<<']);
uksort(
$values,
function ($a, $b) use ($keys) {
return array_search($a, $keys, true) - array_search($b, $keys, true);
}
);
}
}

return $values;
}

/**
* Parses a YAML value.
*
Expand Down Expand Up @@ -441,7 +494,9 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $ob
}

try {
return Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap);
$result = $this->substituteAliases(Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap));

return $result;
} catch (ParseException $e) {
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
$e->setSnippet($this->currentLine);
Expand Down
8 changes: 7 additions & 1 deletion Tests/Fixtures/sfMergeKey.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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 @@ -33,13 +34,18 @@ yaml: |
isit: tested
head:
<<: [ *foo , *dong , *foo2 ]
head_inline: &head_inline { <<: [ *foo , *dong , *foo2 ] }
recursive_inline: { <<: *head_inline, c: { <<: *foo2 } }
php: |
array(
'foo' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian'),
'bar' => array('a' => 'before', 'd' => 'other', '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'), 'x' => 'Oren'),
'duplicate' => array('foo' => 'bar'),
'foo2' => array('a' => 'Ballmer'),
'ding' => array('fi', 'fei', 'fo', 'fam'),
'check' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'fi', 'fei', 'fo', 'fam', 'isit' => 'tested'),
'head' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'fi', 'fei', 'fo', 'fam')
'head' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'fi', 'fei', 'fo', 'fam'),
'head_inline' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'fi', 'fei', 'fo', 'fam'),
'recursive_inline' => array('a' => 'Steve', 'b' => 'Clark', 'c' => array('a' => 'Ballmer'), 'fi', 'fei', 'fo', 'fam')
)
12 changes: 12 additions & 0 deletions Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,18 @@ public function testNestedFoldedStringBlockWithComments()
EOF
));
}

/**
* @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