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

Commit c5cbc83

Browse filesBrowse files
committed
feature #22762 [Yaml] Support tagged scalars (GuilhemN)
This PR was merged into the 4.0-dev branch. Discussion ---------- [Yaml] Support tagged scalars | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes, replacement of deprecations | BC breaks? | yes | Deprecations? | no <!-- don't forget updating UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Add support of tagged scalars in 4.0. Commits ------- b1e8ebc [Yaml] Support tagged scalars
2 parents a20426d + b1e8ebc commit c5cbc83
Copy full SHA for c5cbc83

File tree

4 files changed

+48
-16
lines changed
Filter options

4 files changed

+48
-16
lines changed

‎src/Symfony/Component/Yaml/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/CHANGELOG.md
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ CHANGELOG
2424
* the constructor arguments of the `Parser` class have been removed
2525
* the `Inline` class is internal and no longer part of the BC promise
2626
* removed support for the `!str` tag, use the `!!str` tag instead
27+
* added support for tagged scalars.
28+
29+
```yml
30+
Yaml::parse('!foo bar', Yaml::PARSE_CUSTOM_TAGS);
31+
// returns TaggedValue('foo', 'bar');
32+
```
2733

2834
3.4.0
2935
-----

‎src/Symfony/Component/Yaml/Inline.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Inline.php
+10-10Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static function parse($value, $flags = 0, $references = array())
7777
$result = self::parseScalar($value, $flags, null, $i, null === $tag, $references);
7878
}
7979

80-
if (null !== $tag) {
80+
if (null !== $tag && '' !== $tag) {
8181
return new TaggedValue($tag, $result);
8282
}
8383

@@ -379,7 +379,7 @@ private static function parseSequence($sequence, $flags, &$i = 0, $references =
379379
--$i;
380380
}
381381

382-
if (null !== $tag) {
382+
if (null !== $tag && '' !== $tag) {
383383
$value = new TaggedValue($tag, $value);
384384
}
385385

@@ -489,7 +489,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
489489
--$i;
490490
}
491491

492-
if (null !== $tag) {
492+
if (null !== $tag && '' !== $tag) {
493493
$output[$key] = new TaggedValue($tag, $value);
494494
} else {
495495
$output[$key] = $value;
@@ -582,7 +582,7 @@ private static function evaluateScalar($scalar, $flags, $references = array())
582582
case 0 === strpos($scalar, '!!binary '):
583583
return self::evaluateBinaryScalar(substr($scalar, 9));
584584
default:
585-
@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);
585+
throw new ParseException(sprintf('The string "%s" could not be parsed as it uses an unsupported built-in tag.', $scalar));
586586
}
587587

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

653-
// Is followed by a scalar
654-
if (!isset($value[$nextOffset]) || !in_array($value[$nextOffset], array('[', '{'), true)) {
655-
// Manage scalars in {@link self::evaluateScalar()}
653+
// Is followed by a scalar and is a built-in tag
654+
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:'))) {
655+
// Manage in {@link self::evaluateScalar()}
656656
return;
657657
}
658658

659+
$i = $nextOffset;
660+
659661
// Built-in tags
660662
if ($tag && '!' === $tag[0]) {
661663
throw new ParseException(sprintf('The built-in tag "!%s" is not implemented.', $tag));
662664
}
663665

664-
if (Yaml::PARSE_CUSTOM_TAGS & $flags) {
665-
$i = $nextOffset;
666-
666+
if ('' === $tag || Yaml::PARSE_CUSTOM_TAGS & $flags) {
667667
return $tag;
668668
}
669669

‎src/Symfony/Component/Yaml/Parser.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Parser.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,12 +621,12 @@ private function parseValue($value, $flags, $context)
621621

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

624-
if ('' !== $matches['tag']) {
624+
if ('' !== $matches['tag'] && '!' !== $matches['tag']) {
625625
if ('!!binary' === $matches['tag']) {
626626
return Inline::evaluateBinaryScalar($data);
627-
} elseif ('!' !== $matches['tag']) {
628-
@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);
629627
}
628+
629+
return new TaggedValue(substr($matches['tag'], 1), $data);
630630
}
631631

632632
return $data;

‎src/Symfony/Component/Yaml/Tests/ParserTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/ParserTest.php
+29-3Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,18 @@ public function testCustomTagSupport($expected, $yaml)
15421542
public function taggedValuesProvider()
15431543
{
15441544
return array(
1545+
'scalars' => array(
1546+
array(
1547+
'foo' => new TaggedValue('inline', 'bar'),
1548+
'quz' => new TaggedValue('long', 'this is a long text'),
1549+
),
1550+
<<<YAML
1551+
foo: !inline bar
1552+
quz: !long >
1553+
this is a long
1554+
text
1555+
YAML
1556+
),
15451557
'sequences' => array(
15461558
array(new TaggedValue('foo', array('yaml')), new TaggedValue('quz', array('bar'))),
15471559
<<<YAML
@@ -1569,6 +1581,11 @@ public function taggedValuesProvider()
15691581
);
15701582
}
15711583

1584+
public function testNonSpecificTagSupport()
1585+
{
1586+
$this->assertSame('12', $this->parser->parse('! 12'));
1587+
}
1588+
15721589
/**
15731590
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
15741591
* @expectedExceptionMessage Tags support is not enabled. Enable the `Yaml::PARSE_CUSTOM_TAGS` flag to use "!iterator" at line 1 (near "!iterator [foo]").
@@ -1579,12 +1596,21 @@ public function testCustomTagsDisabled()
15791596
}
15801597

15811598
/**
1582-
* @group legacy
1583-
* @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.
1599+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
1600+
* @expectedExceptionMessage Tags support is not enabled. Enable the `Yaml::PARSE_CUSTOM_TAGS` flag to use "!iterator" at line 1 (near "!iterator foo").
15841601
*/
15851602
public function testUnsupportedTagWithScalar()
15861603
{
1587-
$this->assertEquals('!iterator foo', $this->parser->parse('!iterator foo'));
1604+
$this->parser->parse('!iterator foo');
1605+
}
1606+
1607+
/**
1608+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
1609+
* @expectedExceptionMessage The string "!!iterator foo" could not be parsed as it uses an unsupported built-in tag at line 1 (near "!!iterator foo").
1610+
*/
1611+
public function testUnsupportedBuiltInTagWithScalar()
1612+
{
1613+
$this->parser->parse('!!iterator foo');
15881614
}
15891615

15901616
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.