Skip to content

Commit 5605db0

Browse files
committed
Make ... optional in YAML output
1 parent 6f4c0d5 commit 5605db0

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

doc/ref/stdlib.html

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,11 +1112,12 @@ <h4 id="manifestYamlDoc">std.manifestYamlDoc(value)</h4>
11121112
</p>
11131113

11141114
<pre>std.manifestYamlDoc(
1115-
{
1115+
{
11161116
x: [1, 2, 3, true, false, null,
11171117
"string\nstring\n"],
11181118
y: { a: 1, b: 2, c: [1, 2] },
1119-
})</pre>
1119+
},
1120+
indent_array_in_object=false)</pre>
11201121

11211122
<p>Yields a string containing this YAML:</p>
11221123

@@ -1136,6 +1137,10 @@ <h4 id="manifestYamlDoc">std.manifestYamlDoc(value)</h4>
11361137
"c":
11371138
- 1
11381139
- 2</pre>
1140+
1141+
<p>The <code>indent_array_in_object</code> param adds additional indentation which some people
1142+
may find easier to read.</p>
1143+
11391144
</div>
11401145
<div style="clear: both"></div>
11411146
</div>
@@ -1156,10 +1161,13 @@ <h4 id="manifestYamlStream">std.manifestYamlStream(value)</h4>
11561161
<div class="panel">
11571162
<p>
11581163
Given an array of values, emit a YAML "stream", which is a sequence of documents separated
1159-
by <code>---</code>.
1164+
by <code>---</code> and ending with <code>...</code>.
11601165
</p>
11611166

1162-
<code>std.manifestYamlStream(['a', 1, []])</code>
1167+
<pre>std.manifestYamlStream(
1168+
['a', 1, []],
1169+
indent_array_in_object=false,
1170+
c_document_end=true)</pre>
11631171

11641172
<p>Yields this string:</p>
11651173

@@ -1170,6 +1178,9 @@ <h4 id="manifestYamlStream">std.manifestYamlStream(value)</h4>
11701178
---
11711179
[]
11721180
...</pre>
1181+
1182+
<p>The <code>indent_array_in_object</code> param is the same as in <code>manifestYamlDoc</code>.</p>
1183+
<p>The <code>c_document_end</code> param adds the optional terminating <code>...</code>.</p>
11731184
</div>
11741185
<div style="clear: both"></div>
11751186
</div>

stdlib/std.jsonnet

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,13 +1004,13 @@ limitations under the License.
10041004
std.join('\n' + cindent, lines);
10051005
aux(value, [], ''),
10061006

1007-
manifestYamlStream(value, indent_array_in_object=false)::
1007+
manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::
10081008
if std.type(value) != 'array' then
10091009
error 'manifestYamlStream only takes arrays, got ' + std.type(value)
10101010
else
10111011
'---\n' + std.join(
10121012
'\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]
1013-
) + '\n...\n',
1013+
) + if c_document_end then '\n...\n' else '\n',
10141014

10151015

10161016
manifestPython(o)::
@@ -1134,7 +1134,7 @@ limitations under the License.
11341134

11351135
// Merge-sort for long arrays and naive quicksort for shorter ones
11361136
sort(arr, keyF=id)::
1137-
local quickSort(arr, keyF=id) =
1137+
local quickSort(arr, keyF=id) =
11381138
local l = std.length(arr);
11391139
if std.length(arr) <= 1 then
11401140
arr
@@ -1148,23 +1148,23 @@ limitations under the License.
11481148

11491149
local merge(a, b) =
11501150
local la = std.length(a), lb = std.length(b);
1151-
local aux(i, j, prefix) =
1152-
if i == la then
1153-
prefix + b[j:]
1154-
else if j == lb then
1155-
prefix + a[i:]
1156-
else
1157-
if keyF(a[i]) <= keyF(b[j]) then
1158-
aux(i + 1, j, prefix + [a[i]]) tailstrict
1151+
local aux(i, j, prefix) =
1152+
if i == la then
1153+
prefix + b[j:]
1154+
else if j == lb then
1155+
prefix + a[i:]
11591156
else
1160-
aux(i, j + 1, prefix + [b[j]]) tailstrict;
1157+
if keyF(a[i]) <= keyF(b[j]) then
1158+
aux(i + 1, j, prefix + [a[i]]) tailstrict
1159+
else
1160+
aux(i, j + 1, prefix + [b[j]]) tailstrict;
11611161
aux(0, 0, []);
11621162

11631163
local l = std.length(arr);
11641164
if std.length(arr) <= 30 then
11651165
quickSort(arr, keyF=keyF)
11661166
else
1167-
local mid = std.floor(l/ 2);
1167+
local mid = std.floor(l / 2);
11681168
local left = arr[:mid], right = arr[mid:];
11691169
merge(std.sort(left, keyF=keyF), std.sort(right, keyF=keyF)),
11701170

test_suite/stdlib.jsonnet

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,20 @@ std.assertEqual(
822822
|||
823823
) &&
824824

825+
std.assertEqual(
826+
std.manifestYamlStream([{}, [], 3, '"'], c_document_end=false),
827+
|||
828+
---
829+
{}
830+
---
831+
[]
832+
---
833+
3
834+
---
835+
"\""
836+
|||
837+
) &&
838+
825839
std.assertEqual(std.parseInt('01234567890'), 1234567890) &&
826840
std.assertEqual(std.parseInt('-01234567890'), -1234567890) &&
827841
std.assertEqual(std.parseOctal('755'), 493) &&

0 commit comments

Comments
 (0)