From 1d9e55f208b9185f4be20797ae9ab00fa0560992 Mon Sep 17 00:00:00 2001 From: Rohit Jangid Date: Thu, 25 May 2023 11:29:17 +0530 Subject: [PATCH] feat: add doc for std.parseCsv and std.manifestCsv --- doc/_stdlib_gen/stdlib-content.jsonnet | 75 ++++++++++++++++++++++++++ stdlib/std.jsonnet | 2 + 2 files changed, 77 insertions(+) diff --git a/doc/_stdlib_gen/stdlib-content.jsonnet b/doc/_stdlib_gen/stdlib-content.jsonnet index dd662773d..bd87053c9 100644 --- a/doc/_stdlib_gen/stdlib-content.jsonnet +++ b/doc/_stdlib_gen/stdlib-content.jsonnet @@ -618,6 +618,42 @@ local html = import 'html.libsonnet'; }, ], }, + { + name: 'parseCsvWithHeader', + params: ['str', 'delimiter=","', 'overwrite_duplicate_headers=true'], + availableSince: 'upcoming', + description: [ + html.p({},||| + Parses a CSV string into JSON. The CSV string would use the passed delimiter to split the columns. + |||), + html.p({},||| + In case of duplicate headers, the value would be overwritten by default. overwrite_duplicate_headers + provides an option handle duplicate headers by appending a sequence number at the end of header. + This example should make it clear: + |||), + html.pre({}, ||| + std.parseCsvWithHeaders("head1,head1,head1\nvalue1,value2,value3", overwrite_duplicate_headers=false) + |||), + html.p({},||| + This would result in following output: + |||), + html.pre({}, ||| + [ + { + "head1": "value1", + "head1__1": "value2", + "head1__2": "value3", + }, + ], + |||), + ], + examples: [ + { + input: "std.parseCsvWithHeader('id,name\n1,foo\n2,bar')", + output: std.parseCsvWithHeader('id,name\n1,foo\n2,bar'), + }, + ], + }, { name: 'encodeUTF8', params: ['str'], @@ -932,6 +968,45 @@ local html = import 'html.libsonnet'; |||), ], }, + { + name: 'manifestCsv', + params: ['json', 'headers=null'], + availableSince: 'upcoming', + description: [ + html.p({}, ||| + Convert the given csv compatible json to a CSV. + |||), + html.pre({}, ||| + std.manifestCsv( + [ + { + "id": 1, + "name": "foo", + "x": "baz", + }, + { + "id": 2, + "name": "bar", + }, + ], + ["id", "name"], + |||), + html.p({}, ||| + Yields a string containing this CSV: + |||), + html.pre({}, ||| + id,name + 1,foo + 2,bar + |||), + html.p({}, ||| + If json param is not a valid csv compatible object, it would be an error. + |||), + html.p({}, ||| + The headers param adds is an optional which would default to all fields in the first object. + |||), + ], + }, { name: 'manifestXmlJsonml', params: ['value'], diff --git a/stdlib/std.jsonnet b/stdlib/std.jsonnet index c861a4aec..36875ef1a 100644 --- a/stdlib/std.jsonnet +++ b/stdlib/std.jsonnet @@ -1783,6 +1783,8 @@ limitations under the License. sha256(str):: go_only_function, sha512(str):: go_only_function, sha3(str):: go_only_function, + parseCsvWithHeader(str, delimiter, overwrite_duplicate_headers):: go_only_function, + manifestCsv(json, headers):: go_only_function, trim(str):: std.stripChars(str, ' \t\n\f\r\u0085\u00A0'), }