8000 [Yaml] respect inline level when dumping objects as maps by xabbuh · Pull Request #22409 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Yaml] respect inline level when dumping objects as maps #22409

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 2 commits into from
May 11, 2017

Conversation

xabbuh
Copy link
Member
@xabbuh xabbuh commented Apr 12, 2017
Q A
Branch? 3.2
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #22392
License MIT
Doc PR

Copy link
Member
@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

👍

@stof
Copy link
Member
stof commented Apr 13, 2017

Please also add a test with an empty ArrayObject or stdClass, to ensure it keeps being dumped as {} and not [].

@xabbuh
Copy link
Member Author
xabbuh commented Apr 13, 2017

done

@goetas
Copy link
Contributor
goetas commented Apr 13, 2017

Hi, I'm not sure is the best approach to solve the issue.
The following test will fail:

// currently fails
public function testDumpingArrayObjectInstancesWithNumericKeysRespectsInlineLevel()
{
    $deep = new \ArrayObject(array('d', 'e'));
    $inner = new \ArrayObject(array('b', 'c', $deep));
    $outer = new \ArrayObject(array('a', $inner));

    $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
    $expected = <<<YAML
0: a
1:
    0: b
    1: c
    2: { 0: d, 1: e }

YAML;
    $this->assertEquals($expected, $yaml);
}
// This works with 3.1 branch
public function testDumpingArrayObjectInstancesWithNumericKeysInlined()
{
    $deep = new \ArrayObject(array('d', 'e'));
    $inner = new \ArrayObject(array('b', 'c', $deep));
    $outer = new \ArrayObject(array('a', $inner));

    $yaml = $this->dumper->dump($outer, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
    $expected = <<<YAML
{ 0: a, 1: { 0: b, 1: c, 2: { 0: d, 1: e } } }

YAML;
    $this->assertEquals($expected, $yaml);
}

and the strategy (casting array object ot array) will make really hard (if not impossible) to solve this case

and with the current strategy, https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Yaml/Inline.php#L176 is never reached since castObjectToArray removes all the ArrarObject instances

@goetas
Copy link
Contributor
goetas commented Apr 13, 2017

I will also try to find a solution

@xabbuh
Copy link
Member Author
xabbuh commented Apr 13, 2017

Status: Needs work

@goetas
Copy link
Contributor
goetas commented Apr 13, 2017

Here is my version of the fix #22419

@goetas
Copy link
Contributor
goetas commented Apr 13, 2017

Question: why #21471 is considered as a new feature? IMO should be a bugfix and included in the 3.2 branch.
Empty arrays should be serialized as [ ], and empty arrayobject instances should be serialized as { }. The same behavior is used with json_encode.

The only feature in #21471 should be this that allows to dump an empty arrayobject (or a stdclass) as [ ] instead of { }

@xabbuh xabbuh force-pushed the pr-22392 branch 3 times, most recently from e52ff8e to fe515b3 Compare April 13, 2017 12:13
@stof
Copy link
Member
stof commented Apr 13, 2017

Question: why #21471 is considered as a new feature?

This relies on adding a new constant for the configuration flag => this is a new feature for the component

@goetas
Copy link
Contributor
goetas commented Apr 13, 2017

@xabbuh did you check my work at #22419

@goetas
Copy link
Contributor
goetas commented Apr 13, 2017

@stof i see, thanks

@goetas
Copy link
Contributor
goetas commented Apr 13, 2017

@xabbuh if this is the candidate PR to be merged, do you mind adding:

    public function testDumpingArrayObjectInstancesWithNumericKeysRespectsInlineLevel()
    {
        $deep = new \ArrayObject(array('d', 'e'));
        $inner = new \ArrayObject(array('b', 'c', $deep));
        $outer = new \ArrayObject(array('a', $inner));
        $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
        $expected = <<<YAML
0: a
1:
    0: b
    1: c
    2: { 0: d, 1: e }
YAML;
        $this->assertEquals($expected, $yaml);
    }

?

$willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);
$dumpObjectAsInlineMap = true;

if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
Copy link
Contributor
@goetas goetas Apr 13, 2017

Choose a reason for hiding this comment

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

@xabbuh Should this code be put in a private static method? is identical to what we have on line 86

@goetas
Copy link
Contributor
goetas commented Apr 13, 2017

Did not know stdClass can be used in a foreach loop. Looks to be "iterable" even if is_iterable says is not https://3v4l.org/bY9IC

@stof
Copy link
Member
stof commented Apr 13, 2017

@goetas it does not pass the iterable typehint either. but iterating over a non-iterable object iterates over public properties in PHP.

@goetas
Copy link
Contributor
goetas commented Apr 13, 2017

@stof Is so beautiful to learn things about PHP every day after so many years ☺️

@stof
Copy link
Member
stof commented Apr 13, 2017

See https://3v4l.org/tu1Z0 for the proof btw

@goetas
Copy link
Contributor
goetas commented Apr 17, 2017

Do this PR need still work?

For me is 👍

@fabpot
Copy link
Member
fabpot commented May 11, 2017

Thank you @xabbuh.

@fabpot fabpot merged commit 3cca48c into symfony:3.2 May 11, 2017
fabpot added a commit that referenced this pull request May 11, 2017
…goetas, xabbuh)

This PR was merged into the 3.2 branch.

Discussion
----------

[Yaml] respect inline level when dumping objects as maps

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #22392
| License       | MIT
| Doc PR        |

Commits
-------

3cca48c respect inline level when dumping objects as maps
4f5c149 Test case for not in-lined map-objects
@xabbuh xabbuh deleted the pr-22392 branch May 11, 2017 18:45
@fabpot fabpot mentioned this pull request May 17, 2017
@fabpot fabpot mentioned this pull request May 29, 2017
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.

6 participants
0