Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Yaml] Support tagged scalars #22762

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 1 commit into from
Jul 6, 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
6 changes: 6 additions & 0 deletions 6 src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ CHANGELOG
the parser and dumper is no longer supported, pass bitmask flags instead
* the constructor arguments of the `Parser` class have been removed
* the `Inline` class is internal and no longer part of the BC promise
* added support for tagged scalars.

```yml
Yaml::parse('!foo bar', Yaml::PARSE_CUSTOM_TAGS);
// returns TaggedValue('foo', 'bar');
```

3.4.0
-----
Expand Down
20 changes: 10 additions & 10 deletions 20 src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static function parse($value, $flags = 0, $references = array())
$result = self::parseScalar($value, $flags, null, $i, null === $tag, $references);
}

if (null !== $tag) {
if (null !== $tag && '' !== $tag) {
return new TaggedValue($tag, $result);
}

Expand Down Expand Up @@ -379,7 +379,7 @@ private static function parseSequence($sequence, $flags, &$i = 0, $references =
--$i;
}

if (null !== $tag) {
if (null !== $tag && '' !== $tag) {
$value = new TaggedValue($tag, $value);
}

Expand Down Expand Up @@ -489,7 +489,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
--$i;
}

if (null !== $tag) {
if (null !== $tag && '' !== $tag) {
$output[$key] = new TaggedValue($tag, $value);
} else {
$output[$key] = $value;
Expand Down Expand Up @@ -582,7 +582,7 @@ private static function evaluateScalar($scalar, $flags, $references = array())
case 0 === strpos($scalar, '!!binary '):
return self::evaluateBinaryScalar(substr($scalar, 9));
default:
@trigger_error(sprintf('Using the unquoted scalar value "%s" is deprecated since version 3.3 and will be considered as a tagged value in 4.0. You must quote it.', $scalar), E_USER_DEPRECATED);
throw new ParseException(sprintf('The string "%s" could not be parsed as it uses an unsupported built-in tag.', $scalar));
}

// Optimize for returning strings.
Expand Down Expand Up @@ -650,20 +650,20 @@ private static function parseTag($value, &$i, $flags)
$nextOffset = $i + $tagLength + 1;
$nextOffset += strspn($value, ' ', $nextOffset);

// Is followed by a scalar
if (!isset($value[$nextOffset]) || !in_array($value[$nextOffset], array('[', '{'), true)) {
// Manage scalars in {@link self::evaluateScalar()}
// Is followed by a scalar and is a built-in tag
if ($tag && (!isset($value[$nextOffset]) || !in_array($value[$nextOffset], array('[', '{'), true)) && ('!' === $tag[0] || 'str' === $tag || 0 === strpos($tag, 'php/const:') || 0 === strpos($tag, 'php/object:'))) {
// Manage in {@link self::evaluateScalar()}
return;
}

$i = $nextOffset;

// Built-in tags
if ($tag && '!' === $tag[0]) {
throw new ParseException(sprintf('The built-in tag "!%s" is not implemented.', $tag));
}

if (Yaml::PARSE_CUSTOM_TAGS & $flags) {
$i = $nextOffset;

if ('' === $tag || Yaml::PARSE_CUSTOM_TAGS & $flags) {
return $tag;
}

Expand Down
6 changes: 3 additions & 3 deletions 6 src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -609,12 +609,12 @@ private function parseValue($value, $flags, $context)

$data = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));

if ('' !== $matches['tag']) {
if ('' !== $matches['tag'] && '!' !== $matches['tag']) {
if ('!!binary' === $matches['tag']) {
return Inline::evaluateBinaryScalar($data);
} elseif ('!' !== $matches['tag']) {
@trigger_error(sprintf('Using the custom tag "%s" for the value "%s" is deprecated since version 3.3. It will be replaced by an instance of %s in 4.0.', $matches['tag'], $data, TaggedValue::class), E_USER_DEPRECATED);
}

return new TaggedValue(substr($matches['tag'], 1), $data);
}

return $data;
Expand Down
32 changes: 29 additions & 3 deletions 32 src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,18 @@ public function testCustomTagSupport($expected, $yaml)
public function taggedValuesProvider()
{
return array(
'scalars' => array(
array(
'foo' => new TaggedValue('inline', 'bar'),
'quz' => new TaggedValue('long', 'this is a long text'),
),
<<<YAML
foo: !inline bar
quz: !long >
this is a long
text
YAML
),
'sequences' => array(
array(new TaggedValue('foo', array('yaml')), new TaggedValue('quz', array('bar'))),
<<<YAML
Expand Down Expand Up @@ -1569,6 +1581,11 @@ public function taggedValuesProvider()
);
}

public function testNonSpecificTagSupport()
{
$this->assertSame('12', $this->parser->parse('! 12'));
}

/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
* @expectedExceptionMessage Tags support is not enabled. Enable the `Yaml::PARSE_CUSTOM_TAGS` flag to use "!iterator" at line 1 (near "!iterator [foo]").
Expand All @@ -1579,12 +1596,21 @@ public function testCustomTagsDisabled()
}

/**
* @group legacy
* @expectedDeprecation Using the unquoted scalar value "!iterator foo" is deprecated since version 3.3 and will be considered as a tagged value in 4.0. You must quote it.
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
* @expectedExceptionMessage Tags support is not enabled. Enable the `Yaml::PARSE_CUSTOM_TAGS` flag to use "!iterator" at line 1 (near "!iterator foo").
*/
public function testUnsupportedTagWithScalar()
{
$this->assertEquals('!iterator foo', $this->parser->parse('!iterator foo'));
$this->parser->parse('!iterator foo');
}

/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
* @expectedExceptionMessage The string "!!iterator foo" could not be parsed as it uses an unsupported built-in tag at line 1 (near "!!iterator foo").
*/
public function testUnsupportedBuiltInTagWithScalar()
{
$this->parser->parse('!!iterator foo');
}

/**
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.