From a612189a1001fccc0de18a2e64fc64c5b84e2044 Mon Sep 17 00:00:00 2001 From: Jonathan Matthews Date: Tue, 2 Jan 2024 17:28:43 +0000 Subject: [PATCH] docs/howto: use encoding/csv.{De,En}code This adds 3 Commented CUE guides demonstrating how to use the built-in functions encoding/csv.Decode and encoding/csv.Encode. The Decode function merits 2 separate guides, explaining both how to access CSV data stored natively inside CUE as a multi-line string, and also the (potentially) more common use case of how to use the cue CLI to access data stored in discrete CSV files. Preview-Path: /docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string/ Preview-Path: /docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file/ Preview-Path: /docs/howto/use-the-built-in-function-encoding-csv-encode-to-emit-csv-data/ Signed-off-by: Jonathan Matthews Change-Id: I060a297b20335f6e36a51efbc36cc7a01e868fb8 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cuelang.org/+/1174183 TryBot-Result: CUEcueckoo Reviewed-by: Paul Jolly --- .../en.md | 71 ++++++++++++++++++ .../gen_cache.cue | 18 +++++ .../page.cue | 3 + .../en.md | 69 ++++++++++++++++++ .../gen_cache.cue | 18 +++++ .../page.cue | 3 + .../en.md | 44 ++++++++++++ .../gen_cache.cue | 18 +++++ .../page.cue | 3 + .../index.md | 72 +++++++++++++++++++ .../index.md | 71 ++++++++++++++++++ .../index.md | 45 ++++++++++++ 12 files changed, 435 insertions(+) create mode 100644 content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string/en.md create mode 100644 content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string/gen_cache.cue create mode 100644 content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string/page.cue create mode 100644 content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file/en.md create mode 100644 content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file/gen_cache.cue create mode 100644 content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file/page.cue create mode 100644 content/docs/howto/use-the-built-in-function-encoding-csv-encode-to-emit-csv-data/en.md create mode 100644 content/docs/howto/use-the-built-in-function-encoding-csv-encode-to-emit-csv-data/gen_cache.cue create mode 100644 content/docs/howto/use-the-built-in-function-encoding-csv-encode-to-emit-csv-data/page.cue create mode 100644 hugo/content/en/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string/index.md create mode 100644 hugo/content/en/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file/index.md create mode 100644 hugo/content/en/docs/howto/use-the-built-in-function-encoding-csv-encode-to-emit-csv-data/index.md diff --git a/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string/en.md b/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string/en.md new file mode 100644 index 000000000..3f8ff8971 --- /dev/null +++ b/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string/en.md @@ -0,0 +1,71 @@ +--- +title: Using the built-in function "encoding/csv.Decode" to access CSV data stored as a string +tags: +- commented cue +- encodings +authors: +- jpluscplusm +toc_hide: true +--- + +This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}}) +demonstrates how to use the built-in function +[`encoding/csv.Decode`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/csv#Decode) +to decode a string containing comma-separated values (CSV) into a list of +lists. + +{{{with code "en" "cc"}}} +exec cue export -e output +cmp stdout out +-- file.cue -- +package example + +import "encoding/csv" + +data: """ + Id,Name,Location,Species + 1,Charlie,"Ripon, North Yorkshire",cat + 2,Fred,San Francisco,cat + 3,Greyfriars Bobby,Edinburgh,dog + 4,Nemo,???,fish + """ + +output: csv.Decode(data) +-- out -- +[ + [ + "Id", + "Name", + "Location", + "Species" + ], + [ + "1", + "Charlie", + "Ripon, North Yorkshire", + "cat" + ], + [ + "2", + "Fred", + "San Francisco", + "cat" + ], + [ + "3", + "Greyfriars Bobby", + "Edinburgh", + "dog" + ], + [ + "4", + "Nemo", + "???", + "fish" + ] +] +{{{end}}} + +## Related content + +- The [`encoding/csv`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/csv) built-in package diff --git a/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string/gen_cache.cue b/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string/gen_cache.cue new file mode 100644 index 000000000..764b88201 --- /dev/null +++ b/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string/gen_cache.cue @@ -0,0 +1,18 @@ +package site +{ + content: { + docs: { + howto: { + "use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string": { + page: { + cache: { + code: { + cc: "qb3Ovwt0UuGd59488zDWr8EyBEfjJok2ic1/QIAjP4E=" + } + } + } + } + } + } + } +} diff --git a/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string/page.cue b/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string/page.cue new file mode 100644 index 000000000..7926ceff2 --- /dev/null +++ b/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string/page.cue @@ -0,0 +1,3 @@ +package site + +content: docs: howto: "use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string": {} diff --git a/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file/en.md b/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file/en.md new file mode 100644 index 000000000..c993a7b5c --- /dev/null +++ b/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file/en.md @@ -0,0 +1,69 @@ +--- +title: Using the built-in function "encoding/csv.Decode" to access data stored in a CSV file +tags: +- commented cue +- encodings +authors: +- jpluscplusm +toc_hide: true +--- + +This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}}) +demonstrates how to use the built-in function +[`encoding/csv.Decode`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/csv#Decode) +to decode a file containing comma-separated values (CSV) into a list of lists. + +{{{with code "en" "cc"}}} +exec cue export file.cue -l input: text: data.csv -e output +cmp stdout out +-- data.csv -- +Id,Name,Location,Species +1,Charlie,"Ripon, North Yorkshire",cat +2,Fred,San Francisco,cat +3,Greyfriars Bobby,Edinburgh,dog +4,Nemo,???,fish +-- file.cue -- +package example + +import "encoding/csv" + +input: string +output: csv.Decode(input) +-- out -- +[ + [ + "Id", + "Name", + "Location", + "Species" + ], + [ + "1", + "Charlie", + "Ripon, North Yorkshire", + "cat" + ], + [ + "2", + "Fred", + "San Francisco", + "cat" + ], + [ + "3", + "Greyfriars Bobby", + "Edinburgh", + "dog" + ], + [ + "4", + "Nemo", + "???", + "fish" + ] +] +{{{end}}} + +## Related content + +- The [`encoding/csv`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/csv) built-in package diff --git a/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file/gen_cache.cue b/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file/gen_cache.cue new file mode 100644 index 000000000..df0790aff --- /dev/null +++ b/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file/gen_cache.cue @@ -0,0 +1,18 @@ +package site +{ + content: { + docs: { + howto: { + "use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file": { + page: { + cache: { + code: { + cc: "tHyh1em7sHbIHYawISEep1prc5vp/XZwo7rqukH2l9Y=" + } + } + } + } + } + } + } +} diff --git a/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file/page.cue b/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file/page.cue new file mode 100644 index 000000000..4d281f22a --- /dev/null +++ b/content/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file/page.cue @@ -0,0 +1,3 @@ +package site + +content: docs: howto: "use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file": {} diff --git a/content/docs/howto/use-the-built-in-function-encoding-csv-encode-to-emit-csv-data/en.md b/content/docs/howto/use-the-built-in-function-encoding-csv-encode-to-emit-csv-data/en.md new file mode 100644 index 000000000..f69421274 --- /dev/null +++ b/content/docs/howto/use-the-built-in-function-encoding-csv-encode-to-emit-csv-data/en.md @@ -0,0 +1,44 @@ +--- +title: Using the built-in function "encoding/csv.Encode" to emit CSV data +tags: +- commented cue +- encodings +authors: +- jpluscplusm +toc_hide: true +--- + +This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}}) +demonstrates how to use the built-in function +[`encoding/csv.Encode`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/csv#Encode) +to encode a list of lists into a string as comma-separated values (CSV). + +{{{with code "en" "cc"}}} +exec cue export -e output --out text +cmp stdout out +-- file.cue -- +package example + +import "encoding/csv" + +data: [ + ["Id", "Name", "Location", "Species"], + ["1", "Charlie", "Ripon, North Yorkshire", "cat"], + ["2", "Fred", "San Francisco", "cat"], + ["3", "Greyfriars Bobby", "Edinburgh", "dog"], + ["4", "Nemo", "???", "fish"], +] + +output: csv.Encode(data) +-- out -- +Id,Name,Location,Species +1,Charlie,"Ripon, North Yorkshire",cat +2,Fred,San Francisco,cat +3,Greyfriars Bobby,Edinburgh,dog +4,Nemo,???,fish + +{{{end}}} + +## Related content + +- The [`encoding/csv`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/csv) built-in package diff --git a/content/docs/howto/use-the-built-in-function-encoding-csv-encode-to-emit-csv-data/gen_cache.cue b/content/docs/howto/use-the-built-in-function-encoding-csv-encode-to-emit-csv-data/gen_cache.cue new file mode 100644 index 000000000..583c4b188 --- /dev/null +++ b/content/docs/howto/use-the-built-in-function-encoding-csv-encode-to-emit-csv-data/gen_cache.cue @@ -0,0 +1,18 @@ +package site +{ + content: { + docs: { + howto: { + "use-the-built-in-function-encoding-csv-encode-to-emit-csv-data": { + page: { + cache: { + code: { + cc: "Mg2lD5dfuXSK/A2VfdIpOvbj1ontkBweDL3sOQ9HtfY=" + } + } + } + } + } + } + } +} diff --git a/content/docs/howto/use-the-built-in-function-encoding-csv-encode-to-emit-csv-data/page.cue b/content/docs/howto/use-the-built-in-function-encoding-csv-encode-to-emit-csv-data/page.cue new file mode 100644 index 000000000..70645c630 --- /dev/null +++ b/content/docs/howto/use-the-built-in-function-encoding-csv-encode-to-emit-csv-data/page.cue @@ -0,0 +1,3 @@ +package site + +content: docs: howto: "use-the-built-in-function-encoding-csv-encode-to-emit-csv-data": {} diff --git a/hugo/content/en/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string/index.md b/hugo/content/en/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string/index.md new file mode 100644 index 000000000..c4b3540a6 --- /dev/null +++ b/hugo/content/en/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-csv-data-stored-as-a-string/index.md @@ -0,0 +1,72 @@ +--- +title: Using the built-in function "encoding/csv.Decode" to access CSV data stored as a string +tags: +- commented cue +- encodings +authors: +- jpluscplusm +toc_hide: true +--- + +This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}}) +demonstrates how to use the built-in function +[`encoding/csv.Decode`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/csv#Decode) +to decode a string containing comma-separated values (CSV) into a list of +lists. + +{{< code-tabs >}} +{{< code-tab name="file.cue" language="cue" area="top-left" >}} +package example + +import "encoding/csv" + +data: """ + Id,Name,Location,Species + 1,Charlie,"Ripon, North Yorkshire",cat + 2,Fred,San Francisco,cat + 3,Greyfriars Bobby,Edinburgh,dog + 4,Nemo,???,fish + """ + +output: csv.Decode(data) +{{< /code-tab >}} +{{< code-tab name="TERMINAL" language="" type="terminal" area="top-right" >}} +$ cue export -e output +[ + [ + "Id", + "Name", + "Location", + "Species" + ], + [ + "1", + "Charlie", + "Ripon, North Yorkshire", + "cat" + ], + [ + "2", + "Fred", + "San Francisco", + "cat" + ], + [ + "3", + "Greyfriars Bobby", + "Edinburgh", + "dog" + ], + [ + "4", + "Nemo", + "???", + "fish" + ] +] +{{< /code-tab >}} +{{< /code-tabs >}} + +## Related content + +- The [`encoding/csv`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/csv) built-in package diff --git a/hugo/content/en/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file/index.md b/hugo/content/en/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file/index.md new file mode 100644 index 000000000..979654eb0 --- /dev/null +++ b/hugo/content/en/docs/howto/use-the-built-in-function-encoding-csv-decode-to-access-data-stored-in-a-csv-file/index.md @@ -0,0 +1,71 @@ +--- +title: Using the built-in function "encoding/csv.Decode" to access data stored in a CSV file +tags: +- commented cue +- encodings +authors: +- jpluscplusm +toc_hide: true +--- + +This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}}) +demonstrates how to use the built-in function +[`encoding/csv.Decode`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/csv#Decode) +to decode a file containing comma-separated values (CSV) into a list of lists. + +{{< code-tabs >}} +{{< code-tab name="data.csv" language="csv" area="top-left" >}} +Id,Name,Location,Species +1,Charlie,"Ripon, North Yorkshire",cat +2,Fred,San Francisco,cat +3,Greyfriars Bobby,Edinburgh,dog +4,Nemo,???,fish +{{< /code-tab >}} +{{< code-tab name="file.cue" language="cue" area="top-right" >}} +package example + +import "encoding/csv" + +input: string +output: csv.Decode(input) +{{< /code-tab >}} +{{< code-tab name="TERMINAL" language="" type="terminal" area="bottom" >}} +$ cue export file.cue -l input: text: data.csv -e output +[ + [ + "Id", + "Name", + "Location", + "Species" + ], + [ + "1", + "Charlie", + "Ripon, North Yorkshire", + "cat" + ], + [ + "2", + "Fred", + "San Francisco", + "cat" + ], + [ + "3", + "Greyfriars Bobby", + "Edinburgh", + "dog" + ], + [ + "4", + "Nemo", + "???", + "fish" + ] +] +{{< /code-tab >}} +{{< /code-tabs >}} + +## Related content + +- The [`encoding/csv`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/csv) built-in package diff --git a/hugo/content/en/docs/howto/use-the-built-in-function-encoding-csv-encode-to-emit-csv-data/index.md b/hugo/content/en/docs/howto/use-the-built-in-function-encoding-csv-encode-to-emit-csv-data/index.md new file mode 100644 index 000000000..f6e250f9c --- /dev/null +++ b/hugo/content/en/docs/howto/use-the-built-in-function-encoding-csv-encode-to-emit-csv-data/index.md @@ -0,0 +1,45 @@ +--- +title: Using the built-in function "encoding/csv.Encode" to emit CSV data +tags: +- commented cue +- encodings +authors: +- jpluscplusm +toc_hide: true +--- + +This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}}) +demonstrates how to use the built-in function +[`encoding/csv.Encode`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/csv#Encode) +to encode a list of lists into a string as comma-separated values (CSV). + +{{< code-tabs >}} +{{< code-tab name="file.cue" language="cue" area="top-left" >}} +package example + +import "encoding/csv" + +data: [ + ["Id", "Name", "Location", "Species"], + ["1", "Charlie", "Ripon, North Yorkshire", "cat"], + ["2", "Fred", "San Francisco", "cat"], + ["3", "Greyfriars Bobby", "Edinburgh", "dog"], + ["4", "Nemo", "???", "fish"], +] + +output: csv.Encode(data) +{{< /code-tab >}} +{{< code-tab name="TERMINAL" language="" type="terminal" area="top-right" >}} +$ cue export -e output --out text +Id,Name,Location,Species +1,Charlie,"Ripon, North Yorkshire",cat +2,Fred,San Francisco,cat +3,Greyfriars Bobby,Edinburgh,dog +4,Nemo,???,fish + +{{< /code-tab >}} +{{< /code-tabs >}} + +## Related content + +- The [`encoding/csv`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/csv) built-in package