Skip to content

Commit 1d9e55f

Browse files
committed
feat: add doc for std.parseCsv and std.manifestCsv
1 parent 5a9d395 commit 1d9e55f

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

doc/_stdlib_gen/stdlib-content.jsonnet

+75
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,42 @@ local html = import 'html.libsonnet';
618618
},
619619
],
620620
},
621+
{
622+
name: 'parseCsvWithHeader',
623+
params: ['str', 'delimiter=","', 'overwrite_duplicate_headers=true'],
624+
availableSince: 'upcoming',
625+
description: [
626+
html.p({},|||
627+
Parses a CSV string into JSON. The CSV string would use the passed <code>delimiter</code> to split the columns.
628+
|||),
629+
html.p({},|||
630+
In case of duplicate headers, the value would be overwritten by default. <code>overwrite_duplicate_headers</code>
631+
provides an option handle duplicate headers by appending a sequence number at the end of header.
632+
This example should make it clear:
633+
|||),
634+
html.pre({}, |||
635+
std.parseCsvWithHeaders("head1,head1,head1\nvalue1,value2,value3", overwrite_duplicate_headers=false)
636+
|||),
637+
html.p({},|||
638+
This would result in following output:
639+
|||),
640+
html.pre({}, |||
641+
[
642+
{
643+
"head1": "value1",
644+
"head1__1": "value2",
645+
"head1__2": "value3",
646+
},
647+
],
648+
|||),
649+
],
650+
examples: [
651+
{
652+
input: "std.parseCsvWithHeader('id,name\n1,foo\n2,bar')",
653+
output: std.parseCsvWithHeader('id,name\n1,foo\n2,bar'),
654+
},
655+
],
656+
},
621657
{
622658
name: 'encodeUTF8',
623659
params: ['str'],
@@ -932,6 +968,45 @@ local html = import 'html.libsonnet';
932968
|||),
933969
],
934970
},
971+
{
972+
name: 'manifestCsv',
973+
params: ['json', 'headers=null'],
974+
availableSince: 'upcoming',
975+
description: [
976+
html.p({}, |||
977+
Convert the given csv compatible json to a CSV.
978+
|||),
979+
html.pre({}, |||
980+
std.manifestCsv(
981+
[
982+
{
983+
"id": 1,
984+
"name": "foo",
985+
"x": "baz",
986+
},
987+
{
988+
"id": 2,
989+
"name": "bar",
990+
},
991+
],
992+
["id", "name"],
993+
|||),
994+
html.p({}, |||
995+
Yields a string containing this CSV:
996+
|||),
997+
html.pre({}, |||
998+
id,name
999+
1,foo
1000+
2,bar
1001+
|||),
1002+
html.p({}, |||
1003+
If <code>json</code> param is not a valid csv compatible object, it would be an error.
1004+
|||),
1005+
html.p({}, |||
1006+
The <code>headers</code> param adds is an optional which would default to all fields in the first object.
1007+
|||),
1008+
],
1009+
},
9351010
{
9361011
name: 'manifestXmlJsonml',
9371012
params: ['value'],

stdlib/std.jsonnet

+2
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,8 @@ limitations under the License.
17831783
sha256(str):: go_only_function,
17841784
sha512(str):: go_only_function,
17851785
sha3(str):: go_only_function,
1786+
parseCsvWithHeader(str, delimiter, overwrite_duplicate_headers):: go_only_function,
1787+
manifestCsv(json, headers):: go_only_function,
17861788

17871789
trim(str):: std.stripChars(str, ' \t\n\f\r\u0085\u00A0'),
17881790
}

0 commit comments

Comments
 (0)