Skip to content

Commit

Permalink
docs/howto: use encoding/csv.{De,En}code
Browse files Browse the repository at this point in the history
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 <[email protected]>
Change-Id: I060a297b20335f6e36a51efbc36cc7a01e868fb8
Dispatch-Trailer: {"type":"trybot","CL":1174183,"patchset":5,"ref":"refs/changes/83/1174183/5","targetBranch":"alpha"}
  • Loading branch information
jpluscplusm authored and cueckoo committed Jan 18, 2024
1 parent ddae755 commit f441353
Show file tree
Hide file tree
Showing 12 changed files with 427 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
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` 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
Original file line number Diff line number Diff line change
@@ -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="
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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": {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
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` to decode a
file containing comma-separated values (CSV) into a list of lists.

{{{with code "en" "cc"}}}
exec cue export .:example -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
Original file line number Diff line number Diff line change
@@ -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: "Epk34AtMbOXh6Y/RXsfw1bLnFiRVZyXEi/coFHrle2w="
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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": {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
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` 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
Original file line number Diff line number Diff line change
@@ -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="
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package site

content: docs: howto: "use-the-built-in-function-encoding-csv-encode-to-emit-csv-data": {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
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` 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
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` 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 .:example -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
Loading

0 comments on commit f441353

Please sign in to comment.