8000 [Yaml] Add tags support by GuilhemN · Pull Request #21194 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Yaml] Add tags support #21194

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 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Swap ParsedValue arguments
  • Loading branch information
GuilhemN committed Feb 6, 2017
commit bf51585b4ab47647a0d936e244f0c94764b134c5
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private function dumpValue($value)
if ($value instanceof IteratorArgument || $value instanceof ClosureProxyArgument) {
Copy link
Member

Choose a reason for hiding this comment

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

instanceof ArgumentInterface?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we don't know the tag corresponding to other instances so I think it's better this way here.

$tag = $value instanceof IteratorArgument ? 'iterator' : 'closure_proxy';

return new TaggedValue($this->dumpValue($value->getValues()), $tag);
return new TaggedValue($tag, $this->dumpValue($value->getValues()));
}

if (is_array($value)) {
Expand Down 8000
6 changes: 3 additions & 3 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static function parse($value, $flags = 0, $references = array())
}

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

// some comments are allowed at the end
Expand Down Expand Up @@ -430,7 +430,7 @@ private static function parseSequence($sequence, $flags, &$i = 0, $references =
}

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

$output[] = $value;
Expand Down Expand Up @@ -531,7 +531,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar

if (!$duplicate) {
if (null !== $tag) {
$output[$key] = new TaggedValue($value, $tag);
$output[$key] = new TaggedValue($tag, $value);
} else {
$output[$key] = $value;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function parse($value, $flags = 0)

// Resolves the tag and returns if end of the document
if (null !== ($tag = $this->getLineTag($this->currentLine, $flags, false)) && !$this->moveToNextLine()) {
return new TaggedValue('', $tag);
return new TaggedValue($tag, '');
}

do {
Expand Down Expand Up @@ -149,8 +149,8 @@ public function parse($value, $flags = 0)
$data[] = $thi 8000 s->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $flags);
} elseif (null !== $subTag = $this->getLineTag(ltrim($values['value'], ' '), $flags)) {
$data[] = new TaggedValue(
$this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $flags),
$subTag
$subTag,
$this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $flags)
);
} else {
if (isset($values['leadspaces'])
Expand Down Expand Up @@ -266,7 +266,7 @@ public function parse($value, $flags = 0)
// But overwriting is allowed when a merge node is used in current block.
if ($allowOverwrite || !isset($data[$key])) {
if (null !== $subTag) {
$data[$key] = new TaggedValue('', $subTag);
$data[$key] = new TaggedValue($subTag, '');
} else {
$data[$key] = null;
}
Expand All @@ -281,7 +281,7 @@ public function parse($value, $flags = 0)
// But overwriting is allowed when a merge node is used in current block.
if ($allowOverwrite || !isset($data[$key])) {
if (null !== $subTag) {
$data[$key] = new TaggedValue($value, $subTag);
$data[$key] = new TaggedValue($subTag, $value);
} else {
$data[$key] = $value;
}
Expand Down Expand Up @@ -388,7 +388,7 @@ public function parse($value, $flags = 0)
} while ($this->moveToNextLine());

if (null !== $tag) {
$data = new TaggedValue($data, $tag);
$data = new TaggedValue($tag, $data);
}

if (isset($mbEncoding)) {
Expand Down
20 changes: 10 additions & 10 deletions src/Symfony/Component/Yaml/Tag/TaggedValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,32 @@
*/
final class TaggedValue
{
private $value;
private $tag;
private $value;

/**
* @param mixed $value
* @param string $tag
* @param mixed $value
*/
public function __construct($value, $tag)
public function __construct($tag, $value)
{
$this->value = $value;
$this->tag = $tag;
$this->value = $value;
}

/**
* @return mixed
* @return string
*/
public function getValue()
public function getTag()
{
return $this->value;
return $this->tag;
}

/**
* @return string
* @return mixed
*/
public function getTag()
public function getValue()
{
return $this->tag;
return $this->value;
}
}
8 changes: 4 additions & 4 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,7 @@ public function testParseMultiLineMappingValue()

public function testTaggedInlineMapping()
{
$this->assertEquals(new TaggedValue(array('foo' => 'bar'), 'foo'), $this->parser->parse('!foo {foo: bar}', Yaml::PARSE_CUSTOM_TAGS));
$this->assertEquals(new TaggedValue('foo', array('foo' => 'bar')), $this->parser->parse('!foo {foo: bar}', Yaml::PARSE_CUSTOM_TAGS));
}

/**
Expand All @@ -1510,15 +1510,15 @@ public function taggedValuesProvider()
{
return array(
'sequences' => array(
array(new TaggedValue(array('yaml'), 'foo'), new TaggedValue(array('bar'), '!quz')),
array(new TaggedValue('foo', array('yaml')), new TaggedValue('!quz', array('bar'))),
<<<YAML
- !foo
- yaml
- !!quz [bar]
Copy link
Member

Choose a reason for hiding this comment

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

Tags starting with two exclamation marks are tags defined by the YAML spec. I do not think we should support them through a flag that indicates to parse custom tags (and if we want to parse built-in tags, we will have to validate their names IMO).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

should we throw an exception then?

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @xabbuh - we need to keep BC of course

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

YAML
),
'mappings' => array(
new TaggedValue(array('foo' => new TaggedValue(array('bar'), '!quz'), 'quz' => new TaggedValue(array('quz' => 'bar'), 'foo')), 'foo'),
new TaggedValue('foo', array('foo' => new TaggedValue('!quz', array('bar')), 'quz' => new TaggedValue('foo', array('quz' => 'bar')))),
<<<YAML
!foo
foo: !!quz [bar]
Expand All @@ -1527,7 +1527,7 @@ public function taggedValuesProvider()
YAML
),
'inline' => array(
array(new TaggedValue(array('foo', 'bar'), 'foo'), new TaggedValue(array('foo' => 'bar', 'quz' => new TaggedValue(array('one' => 'bar'), 'bar')), '!quz')),
array(new TaggedValue('foo', array('foo', 'bar')), new TaggedValue('!quz', array('foo' => 'bar', 'quz' => new TaggedValue('bar', array('one' => 'bar'))))),
<<<YAML
- !foo [foo, bar]
- !!quz {foo: bar, quz: !bar {one: bar}}
Expand Down
0