Skip to content

[Yaml] Allow dumping empty array as YAML sequence #21471

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 3 commits into from
Feb 19, 2017

Conversation

c960657
Copy link
Contributor

@c960657 c960657 commented Jan 31, 2017

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #9870, #15937, #16266
License MIT
Doc PR

PHP arrays are dumped as either YAML sequences or mappings, depending on whether the array has continuos integer keys or not.

An empty array is always dumped as a YAML mapping. Sometimes you want it dumped as a YAML sequence instead.

$dump = $this->dumper->dump(array(), 9, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
$this->assertEquals('[]', $dump);

$dump = $this->dumper->dump(new \ArrayObject(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP);
Copy link
Member

Choose a reason for hiding this comment

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

I would not expect an empty sequence as the result with this input.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. Fixed.

$dump = $this->dumper->dump(new \ArrayObject(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP);
$this->assertEquals('[]', $dump);

$dump = $this->dumper->dump(new \stdClass(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP);
Copy link
Member

Choose a reason for hiding this comment

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

same here

@nicolas-grekas nicolas-grekas added this to the 3.x milestone Jan 31, 2017
@fabpot
Copy link
Member

fabpot commented Feb 16, 2017

@xabbuh I let you merge this one?

@xabbuh
Copy link
Member

xabbuh commented Feb 17, 2017

@fabpot Sure, can I understand your comment as a +1 vote?

@@ -29,6 +29,7 @@ class Yaml
const DUMP_OBJECT_AS_MAP = 64;
const DUMP_MULTI_LINE_LITERAL_BLOCK = 128;
const PARSE_CONSTANT = 256;
const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 512;
Copy link
Member

Choose a reason for hiding this comment

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

alpha order?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you want me to sort all constants alphabetically? I don't see this convention being used elsewhere in Symfony.

Copy link
Member

Choose a reason for hiding this comment

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

I would keep it this way. Otherwise, it's becoming hard to add new flags as you need to be extra careful to not reuse one of the existing flag values.

Copy link
Member

Choose a reason for hiding this comment

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

can't we change the values of the consts?

Copy link
Member

Choose a reason for hiding this comment

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

I like the way it is now, the more recent addition are last with a higher number. I don't see why we should order them.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for being late here, but I had no time during the weekend to do the final review. This value must be adjusted as 512 is already used by the new tags support feature (see #21678).

@@ -1,6 +1,15 @@
CHANGELOG
=========

3.2.3
Copy link
Member

Choose a reason for hiding this comment

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

wrong version

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@@ -1,6 +1,15 @@
CHANGELOG
=========

3.2.5
Copy link
Member

Choose a reason for hiding this comment

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

That's still not right. New features are only added in minor versions. So this needs to be 3.3.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed again :-)

@fabpot
Copy link
Member

fabpot commented Feb 19, 2017

Thank you @c960657.

@fabpot fabpot closed this Feb 19, 2017
@fabpot fabpot merged commit 87ffaf2 into symfony:master Feb 19, 2017
fabpot added a commit that referenced this pull request Feb 19, 2017
…0657)

This PR was squashed before being merged into the 3.3-dev branch (closes #21471).

Discussion
----------

[Yaml] Allow dumping empty array as YAML sequence

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #9870, #15937, #16266
| License       | MIT
| Doc PR        |

PHP arrays are dumped as either YAML sequences or mappings, depending on whether the array has continuos integer keys or not.

An empty array is always dumped as a YAML mapping. Sometimes you want it dumped as a YAML sequence instead.

Commits
-------

87ffaf2 Bump version number
af7067c Dump empty object as mapping
a6d94c1 [Yaml] Allow dumping empty array as YAML sequence
@nicolas-grekas nicolas-grekas modified the milestones: 3.x, 3.3 Mar 24, 2017
@fabpot fabpot mentioned this pull request May 1, 2017
@c960657 c960657 deleted the yaml-empty-array branch May 22, 2017 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants