Skip to content

Commit b954cdf

Browse files
committed
Fix bad indentation of YAML, add tests
1 parent fb6faa9 commit b954cdf

8 files changed

+112
-31
lines changed

stdlib/std.jsonnet

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ limitations under the License.
922922
aux(value, [], ''),
923923

924924
manifestYamlDoc(value)::
925-
local aux(v, in_object, path, cindent) =
925+
local aux(v, in_array, in_object, path, cindent) =
926926
if v == true then
927927
'true'
928928
else if v == false then
@@ -947,21 +947,37 @@ limitations under the License.
947947
'[]'
948948
else
949949
local range = std.range(0, std.length(v) - 1);
950+
// If we're in object than drop the indent we would usually have. This allows e.g.
951+
// ports:
952+
// - 80
953+
// instead of
954+
// ports:
955+
// - 80
950956
local actual_indent = if in_object then cindent[2:] else cindent;
951-
local parts = [aux(v[i], false, path + [i], cindent) for i in range];
952-
(if in_object then '\n' + actual_indent else '')
957+
local new_indent = actual_indent + ' ';
958+
local parts = [aux(v[i], true, false, path + [i], new_indent) for i in range];
959+
// While we could avoid the new line in the case of in_array, it yields YAML that is
960+
// hard to read, e.g.:
961+
// - - - 1
962+
// - 2
963+
// - - 3
964+
// - 4
965+
(if in_array || in_object then '\n' + actual_indent else '')
953966
+ '- ' + std.join('\n' + actual_indent + '- ', parts)
954967
else if std.type(v) == 'object' then
955968
if std.length(v) == 0 then
956969
'{}'
957970
else
958971
local new_indent = cindent + ' ';
959972
local lines = [
960-
std.escapeStringJson(k) + ': ' + aux(v[k], true, path + [k], new_indent)
973+
std.escapeStringJson(k) + ': ' + aux(v[k], false, true, path + [k], new_indent)
961974
for k in std.objectFields(v)
962975
];
976+
// If we're in an array, we can start on the same line as the - because the indentation
977+
// matches up then. The converse is not true, because fields are not always
978+
// 1 character long.
963979
(if in_object then '\n' + cindent else '') + std.join('\n' + cindent, lines);
964-
aux(value, false, [], ''),
980+
aux(value, false, false, [], ''),
965981

966982
manifestYamlStream(value)::
967983
if std.type(value) != 'array' then
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
RUNTIME ERROR: cannot test equality of functions
2-
std.jsonnet:1223:9-34 function <anonymous>
2+
std.jsonnet:1239:9-34 function <anonymous>
33
error.equality_function.jsonnet:17:1-33
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
RUNTIME ERROR: foobar
22
error.inside_equals_array.jsonnet:18:18-32 thunk <array_element>
3-
std.jsonnet:1203:29-33 thunk <b>
4-
std.jsonnet:1203:21-33 function <anonymous>
5-
std.jsonnet:1203:21-33 function <aux>
6-
std.jsonnet:1206:15-31 function <anonymous>
7-
std.jsonnet:1207:11-23
3+
std.jsonnet:1219:29-33 thunk <b>
4+
std.jsonnet:1219:21-33 function <anonymous>
5+
std.jsonnet:1219:21-33 function <aux>
6+
std.jsonnet:1222:15-31 function <anonymous>
7+
std.jsonnet:1223:11-23
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
RUNTIME ERROR: foobar
22
error.inside_equals_object.jsonnet:18:22-36 object <b>
3-
std.jsonnet:1217:50-54 thunk <b>
4-
std.jsonnet:1217:42-54 function <anonymous>
5-
std.jsonnet:1217:42-54 function <aux>
6-
std.jsonnet:1220:15-31 function <anonymous>
7-
std.jsonnet:1221:11-23
3+
std.jsonnet:1233:50-54 thunk <b>
4+
std.jsonnet:1233:42-54 function <anonymous>
5+
std.jsonnet:1233:42-54 function <aux>
6+
std.jsonnet:1236:15-31 function <anonymous>
7+
std.jsonnet:1237:11-23
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Object assertion failed.
22
error.invariant.equality.jsonnet:17:10-15 thunk <object_assert>
3-
std.jsonnet:1217:42-46 thunk <a>
4-
std.jsonnet:1217:42-54 function <anonymous>
5-
std.jsonnet:1217:42-54 function <anonymous>
6-
std.jsonnet:1221:11-23
3+
std.jsonnet:1233:42-46 thunk <a>
4+
std.jsonnet:1233:42-54 function <anonymous>
5+
std.jsonnet:1233:42-54 function <anonymous>
6+
std.jsonnet:1237:11-23
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Object assertion failed.
22
error.obj_assert.fail1.jsonnet:20:23-29 thunk <object_assert>
3-
std.jsonnet:1217:42-46 thunk <a>
4-
std.jsonnet:1217:42-54 function <anonymous>
5-
std.jsonnet:1217:42-54 function <anonymous>
6-
std.jsonnet:1221:11-23
3+
std.jsonnet:1233:42-46 thunk <a>
4+
std.jsonnet:1233:42-54 function <anonymous>
5+
std.jsonnet:1233:42-54 function <anonymous>
6+
std.jsonnet:1237:11-23
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: foo was not equal to bar
22
error.obj_assert.fail2.jsonnet:20:32-65 thunk <object_assert>
3-
std.jsonnet:1217:42-46 thunk <a>
4-
std.jsonnet:1217:42-54 function <anonymous>
5-
std.jsonnet:1217:42-54 function <anonymous>
6-
std.jsonnet:1221:11-23
3+
std.jsonnet:1233:42-46 thunk <a>
4+
std.jsonnet:1233:42-54 function <anonymous>
5+
std.jsonnet:1233:42-54 function <anonymous>
6+
std.jsonnet:1237:11-23

test_suite/stdlib.jsonnet

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,75 @@ std.assertEqual(
455455
|||
456456
) &&
457457

458+
459+
std.assertEqual(
460+
std.manifestYamlDoc([{ x: [1, 2, 3] }]) + '\n',
461+
|||
462+
- "x":
463+
- 1
464+
- 2
465+
- 3
466+
|||
467+
) &&
468+
469+
std.assertEqual(
470+
std.manifestYamlDoc({ x: [1, 2, 3] }) + '\n',
471+
|||
472+
"x":
473+
- 1
474+
- 2
475+
- 3
476+
|||
477+
) &&
478+
479+
std.assertEqual(
480+
std.manifestYamlDoc([[[1, 2], [3, 4]]]) + '\n',
481+
|||
482+
-
483+
-
484+
- 1
485+
- 2
486+
-
487+
- 3
488+
- 4
489+
|||
490+
) &&
491+
492+
std.assertEqual(
493+
std.manifestYamlDoc({ x: [[[1, [1], 1]]] }) + '\n',
494+
|||
495+
"x":
496+
-
497+
-
498+
- 1
499+
-
500+
- 1
501+
- 1
502+
|||
503+
) &&
504+
505+
std.assertEqual(
506+
std.manifestYamlDoc({ x: [[[1, { f: 3, g: [1, 2] }, 1]]] }) + '\n',
507+
|||
508+
"x":
509+
-
510+
-
511+
- 1
512+
- "f": 3
513+
"g":
514+
- 1
515+
- 2
516+
- 1
517+
|||
518+
) &&
519+
458520
std.assertEqual(
459521
std.manifestYamlDoc(some_json) + '\n',
460522
|||
461523
"\"": null
462524
"arr":
463-
- - []
525+
-
526+
- []
464527
"emptyArray": []
465528
"emptyObject": {}
466529
"objectInArray":
@@ -490,7 +553,8 @@ std.assertEqual(
490553
---
491554
"\"": null
492555
"arr":
493-
- - []
556+
-
557+
- []
494558
"emptyArray": []
495559
"emptyObject": {}
496560
"objectInArray":
@@ -514,7 +578,8 @@ std.assertEqual(
514578
---
515579
"\"": null
516580
"arr":
517-
- - []
581+
-
582+
- []
518583
"emptyArray": []
519584
"emptyObject": {}
520585
"objectInArray":

0 commit comments

Comments
 (0)