8000 feature #48127 [Yaml] Add flag to dump numeric key as string (alamira… · symfony/symfony@52b5bb7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 52b5bb7

Browse files
committed
feature #48127 [Yaml] Add flag to dump numeric key as string (alamirault)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [Yaml] Add flag to dump numeric key as string | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #48061 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Sometime Yaml keys must be always string. (OpenApi spec for example) This PR add support to dump int as string by using the `Yaml::DUMP_NUMERIC_KEY_AS_STRING` flag. Without flag (default) ```yaml 200: foo ``` With flag ```yaml '200': foo ``` (Unrelated fabbot failure) Commits ------- 35890e1 [Yaml] Add flag to dump numeric key as string
2 parents 5ae4dc4 + 35890e1 commit 52b5bb7

File tree

6 files changed

+196
-0
lines changed

6 files changed

+196
-0
lines changed

src/Symfony/Component/Yaml/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Add support to dump int keys as strings by using the `Yaml::DUMP_NUMERIC_KEY_AS_STRING` flag.
8+
49
6.2
510
---
611

src/Symfony/Component/Yaml/Dumper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
6666
$output .= "\n";
6767
}
6868

69+
if (\is_int($key) && Yaml::DUMP_NUMERIC_KEY_AS_STRING & $flags) {
70+
$key = (string) $key;
71+
}
72+
6973
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && str_contains($value, "\n") && !str_contains($value, "\r")) {
7074
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
7175
// http://www.yaml.org/spec/1.2/spec.html#id2793979

src/Symfony/Component/Yaml/Inline.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ private static function dumpHashArray(array|\ArrayObject|\stdClass $value, int $
239239
{
240240
$output = [];
241241
foreach ($value as $key => $val) {
242+
if (\is_int($key) && Yaml::DUMP_NUMERIC_KEY_AS_STRING & $flags) {
243+
$key = (string) $key;
244+
}
245+
242246
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
243247
}
244248

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

Lines changed: 90 additions & 0 deletions
< 8000 /tr>
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,96 @@ public function testDumpNullAsTilde()
817817
$this->assertSame('{ foo: ~ }', $this->dumper->dump(['foo' => null], 0, 0, Yaml::DUMP_NULL_AS_TILDE));
818818
}
819819

820+
/**
821+
* @dataProvider getNumericKeyData
822+
*/
823+
public function testDumpInlineNumericKeyAsString(array $input, bool $inline, int $flags, string $expected)
824+
{
825+
$this->assertSame($expected, $this->dumper->dump($input, $inline ? 0 : 4, 0, $flags));
826+
}
827+
828+
public function getNumericKeyData()
829+
{
830+
yield 'Int key with flag inline' => [
831+
[200 => 'foo'],
832+
true,
833+
Yaml::DUMP_NUMERIC_KEY_AS_STRING,
834+
"{ '200': foo }",
835+
];
836+
837+
yield 'Int key without flag inline' => [
838+
[200 => 'foo'],
839+
true,
840+
0,
841+
'{ 200: foo }',
842+
];
843+
844+
$expected = <<<'YAML'
845+
'200': foo
846+
847+
YAML;
848+
849+
yield 'Int key with flag' => [
850+
[200 => 'foo'],
851+
false,
852+
Yaml::DUMP_NUMERIC_KEY_AS_STRING,
853+
$expected,
854+
];
855+
856+
$expected = <<<'YAML'
857+
200: foo
858+
859+
YAML;
860+
861+
yield 'Int key without flag' => [
862+
[200 => 'foo'],
863+
false,
864+
0,
865+
$expected,
866+
];
867+
868+
$expected = <<<'YAML'
869+
- 200
870+
- foo
871+
872+
YAML;
873+
874+
yield 'List array with flag' => [
875+
[200, 'foo'],
876+
false,
877+
Yaml::DUMP_NUMERIC_KEY_AS_STRING,
878+
$expected,
879+
];
880+
881+
$expected = <<<'YAML'
882+
'200': !number 5
883+
884+
YAML;
885+
886+
yield 'Int tagged value with flag' => [
887+
[
888+
200 => new TaggedValue('number', 5),
889+
],
890+
false,
891+
Yaml::DUMP_NUMERIC_KEY_AS_STRING,
892+
$expected,
893+
];
894+
895+
$expected = <<<'YAML'
896+
200: !number 5
897+
898+
YAML;
899+
900+
yield 'Int tagged value without flag' => [
901+
[
902+
200 => new TaggedValue('number', 5),
903+
],
904+
false,
905+
0,
906+
$expected,
907+
];
908+
}
909+
820910
public function testDumpIdeographicSpaces()
821911
{
822912
$expected = <<<YAML

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,98 @@ public function testDumpDateTime($dateTime, $expected)
617617
$this->assertSame($expected, Inline::dump($dateTime));
618618
}
619619

620+
/**
621+
* @dataProvider getNumericKeyData
622+
*/
623+
public function testDumpNumericKeyAsString(array|int $input, int $flags, string $expected)
624+
{
625+
$this->assertSame($expected, Inline::dump($input, $flags));
626+
}
627+
628+
public function getNumericKeyData()
629+
{
630+
yield 'Int with flag' => [
631+
200,
632+
Yaml::DUMP_NUMERIC_KEY_AS_STRING,
633+ 10000
'200',
634+
];
635+
636+
yield 'Int key with flag' => [
637+
[200 => 'foo'],
638+
Yaml::DUMP_NUMERIC_KEY_AS_STRING,
639+
"{ '200': foo }",
640+
];
641+
642+
yield 'Int value with flag' => [
643+
[200 => 200],
644+
Yaml::DUMP_NUMERIC_KEY_AS_STRING,
645+
"{ '200': 200 }",
646+
];
647+
648+
yield 'String key with flag' => [
649+
['200' => 'foo'],
650+
Yaml::DUMP_NUMERIC_KEY_AS_STRING,
651+
"{ '200': foo }",
652+
];
653+
654+
yield 'Mixed with flag' => [
655+
[42 => 'a', 'b' => 'c', 'd' => 43],
656+
Yaml::DUMP_NUMERIC_KEY_AS_STRING,
657+
"{ '42': a, b: c, d: 43 }",
658+
];
659+
660+
yield 'Auto-index with flag' => [
661+
['a', 'b', 42],
662+
Yaml::DUMP_NUMERIC_KEY_AS_STRING,
663+
'[a, b, 42]',
664+
];
665+
666+
yield 'Complex mixed array with flag' => [
667+
[
668+
42 => [
669+
'foo' => 43,
670+
44 => 'bar',
671+
],
672+
45 => 'baz',
673+
46,
674+
],
675+
Yaml::DUMP_NUMERIC_KEY_AS_STRING,
676+
"{ '42': { foo: 43, '44': bar }, '45': baz, '46': 46 }",
677+
];
678+
679+
yield 'Int tagged value with flag' => [
680+
[
681+
'count' => new TaggedValue('number', 5),
682+
],
683+
Yaml::DUMP_NUMERIC_KEY_AS_STRING,
684+
'{ count: !number 5 }',
685+
];
686+
687+
yield 'Array tagged value with flag' => [
688+
[
689+
'user' => new TaggedValue('metadata', [
690+
'john',
691+
42,
692+
]),
693+
],
694+
Yaml::DUMP_NUMERIC_KEY_AS_STRING,
695+
'{ user: !metadata [john, 42] }',
696+
];
697+
698+
$arrayObject = new \ArrayObject();
699+
$arrayObject['foo'] = 'bar';
700+
$arrayObject[42] = 'baz';
701+
$arrayObject['baz'] = 43;
702+
703+
yield 'Object value with flag' => [
704+
[
705+
'user' => $arrayObject,
706+
],
707+
Yaml::DUMP_NUMERIC_KEY_AS_STRING | Yaml::DUMP_OBJECT_AS_MAP,
708+
"{ user: { foo: bar, '42': baz, baz: 43 } }",
709+
];
710+
}
711+
620712
public function testDumpUnitEnum()
621713
{
622714
$this->assertSame("!php/const Symfony\Component\Yaml\Tests\Fixtures\FooUnitEnum::BAR", Inline::dump(FooUnitEnum::BAR));

src/Symfony/Component/Yaml/Yaml.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Yaml
3434
public const PARSE_CUSTOM_TAGS = 512;
3535
public const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024;
3636
public const DUMP_NULL_AS_TILDE = 2048;
37+
public const DUMP_NUMERIC_KEY_AS_STRING = 4096;
3738

3839
/**
3940
* Parses a YAML file into a PHP value.

0 commit comments

Comments
 (0)
0