Skip to content

Commit ec3f12b

Browse files
committed
feature #59315 [Yaml] Add compact nested mapping support to Dumper (gr8b)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [Yaml] Add compact nested mapping support to `Dumper` | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | - | License | MIT Feature allow to decrease count of lines in YAML output using list hash start line also as hash dump first line. Original output ```yaml planets: - name: Mercury distance: 57910000 - name: Jupiter distance: 778500000 ``` With `Yaml::DUMP_COMPACT_NESTED_MAPPING` flag enabled. ```yaml planets: - name: Mercury distance: 57910000 - name: Jupiter distance: 778500000 ``` <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Commits ------- f67e636 [Yaml] Add compact nested mapping support to `Dumper`
2 parents 348781c + f67e636 commit ec3f12b

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed

src/Symfony/Component/Yaml/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Add compact nested mapping support by using the `Yaml::DUMP_COMPACT_NESTED_MAPPING` flag
8+
49
7.2
510
---
611

src/Symfony/Component/Yaml/Dumper.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ private function doDump(mixed $input, int $inline = 0, int $indent = 0, int $fla
6565
$output .= $this->dumpTaggedValue($input, $inline, $indent, $flags, $prefix, $nestingLevel);
6666
} else {
6767
$dumpAsMap = Inline::isHash($input);
68+
$compactNestedMapping = Yaml::DUMP_COMPACT_NESTED_MAPPING & $flags && !$dumpAsMap;
6869

6970
foreach ($input as $key => $value) {
7071
if ('' !== $output && "\n" !== $output[-1]) {
@@ -134,8 +135,8 @@ private function doDump(mixed $input, int $inline = 0, int $indent = 0, int $fla
134135
$output .= \sprintf('%s%s%s%s',
135136
$prefix,
136137
$dumpAsMap ? Inline::dump($key, $flags).':' : '-',
137-
$willBeInlined ? ' ' : "\n",
138-
$this->doDump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags, $nestingLevel + 1)
138+
$willBeInlined || ($compactNestedMapping && \is_array($value)) ? ' ' : "\n",
139+
$compactNestedMapping && \is_array($value) ? substr($this->doDump($value, $inline - 1, $indent + 2, $flags, $nestingLevel + 1), $indent + 2) : $this->doDump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags, $nestingLevel + 1)
139140
).($willBeInlined ? "\n" : '');
140141
}
141142
}

src/Symfony/Component/Yaml/Tests/DumperTest.php

+91
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,97 @@ public static function getDateTimeData()
10611061
];
10621062
}
10631063

1064+
public static function getDumpCompactNestedMapping()
1065+
{
1066+
$data = [
1067+
'planets' => [
1068+
[
1069+
'name' => 'Mercury',
1070+
'distance' => 57910000,
1071+
'properties' => [
1072+
['name' => 'size', 'value' => 4879],
1073+
['name' => 'moons', 'value' => 0],
1074+
[[[]]],
1075+
],
1076+
],
1077+
[
1078+
'name' => 'Jupiter',
1079+
'distance' => 778500000,
1080+
'properties' => [
1081+
['name' => 'size', 'value' => 139820],
1082+
['name' => 'moons', 'value' => 79],
1083+
[[]],
1084+
],
1085+
],
1086+
],
1087+
];
1088+
$expected = <<<YAML
1089+
planets:
1090+
\t- name: Mercury
1091+
\t distance: 57910000
1092+
\t properties:
1093+
\t\t - name: size
1094+
\t\t value: 4879
1095+
\t\t - name: moons
1096+
\t\t value: 0
1097+
\t\t - - - { }
1098+
\t- name: Jupiter
1099+
\t distance: 778500000
1100+
\t properties:
1101+
\t\t - name: size
1102+
\t\t value: 139820
1103+
\t\t - name: moons
1104+
\t\t value: 79
1105+
\t\t - - { }
1106+
1107+
YAML;
1108+
1109+
for ($indentation = 1; $indentation < 5; ++$indentation) {
1110+
yield \sprintf('Compact nested mapping %d', $indentation) => [
1111+
$data,
1112+
strtr($expected, ["\t" => str_repeat(' ', $indentation)]),
1113+
$indentation,
1114+
];
1115+
}
1116+
1117+
$indentation = 2;
1118+
$inline = 4;
1119+
$expected = <<<YAML
1120+
planets:
1121+
- name: Mercury
1122+
distance: 57910000
1123+
properties:
1124+
- { name: size, value: 4879 }
1125+
- { name: moons, value: 0 }
1126+
- [[{ }]]
1127+
- name: Jupiter
1128+
distance: 778500000
1129+
properties:
1130+
- { name: size, value: 139820 }
1131+
- { name: moons, value: 79 }
1132+
- [{ }]
1133+
1134+
YAML;
1135+
1136+
yield \sprintf('Compact nested mapping %d and inline %d', $indentation, $inline) => [
1137+
$data,
1138+
$expected,
1139+
$indentation,
1140+
$inline,
1141+
];
1142+
}
1143+
1144+
/**
1145+
* @dataProvider getDumpCompactNestedMapping
1146+
*/
1147+
public function testDumpCompactNestedMapping(array $data, string $expected, int $indentation, int $inline = 10)
1148+
{
1149+
$dumper = new Dumper($indentation);
1150+
$actual = $dumper->dump($data, $inline, 0, Yaml::DUMP_COMPACT_NESTED_MAPPING);
1151+
$this->assertSame($expected, $actual);
1152+
$this->assertSameData($data, $this->parser->parse($actual));
1153+
}
1154+
10641155
private function assertSameData($expected, $actual)
10651156
{
10661157
$this->assertEquals($expected, $actual);

src/Symfony/Component/Yaml/Yaml.php

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Yaml
3636
public const DUMP_NULL_AS_TILDE = 2048;
3737
public const DUMP_NUMERIC_KEY_AS_STRING = 4096;
3838
public const DUMP_NULL_AS_EMPTY = 8192;
39+
public const DUMP_COMPACT_NESTED_MAPPING = 16384;
3940

4041
/**
4142
* Parses a YAML file into a PHP value.

0 commit comments

Comments
 (0)