Skip to content

Commit

Permalink
docs/howto: use encoding/base64.{De,En}code
Browse files Browse the repository at this point in the history
This adds a pair of Commented CUE guides demonstrating how to use the
built-in functions encoding/base64.Decode and encoding/base64.Encode.

The Decode example needs to include a string interpolation that's not
strictly related to the function's use case, as otherwise "cue export"
Base64-encodes the function's []byte return in the terminal output,
which would likely confuse the reader.

Preview-Path: /docs/howto/use-the-built-in-function-encoding-base64-decode/
Preview-Path: /docs/howto/use-the-built-in-function-encoding-base64-encode/
Signed-off-by: Jonathan Matthews <[email protected]>
Change-Id: I631c77d8028596e1420067d24ed9b99d07b23465
Dispatch-Trailer: {"type":"trybot","CL":1174178,"patchset":7,"ref":"refs/changes/78/1174178/7","targetBranch":"alpha"}
  • Loading branch information
jpluscplusm authored and cueckoo committed Jan 18, 2024
1 parent 84b0150 commit df989e3
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: Using the built-in function "encoding/base64.Decode"
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/base64.Decode` to decode
data that is encoded as Base64.

{{{with code "en" "cc"}}}
#location left right

exec cue export
cmp stdout out
-- file.cue --
package example

import "encoding/base64"

// encoded contains some Base64-encoded data
encoded: "c29tZSBCYXNlNjQtZW5jb2RlZCBkYXRh"

// decoded contains the string interpolation of
// the byte array returned by base64.Decode
decoded: "\(base64.Decode(null, encoded))"
-- out --
{
"encoded": "c29tZSBCYXNlNjQtZW5jb2RlZCBkYXRh",
"decoded": "some Base64-encoded data"
}
{{{end}}}

## Related content

- The [`encoding/base64`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/base64) 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-base64-decode": {
page: {
cache: {
code: {
cc: "5qOOo7lThAkQspaLSGP6So5m0bZynVbxIz6YDilXheY="
}
}
}
}
}
}
}
}
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-base64-decode": {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
title: Using the built-in function "encoding/base64.Encode"
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/base64.Encode` to
encode a string as Base64.

{{{with code "en" "cc"}}}
#location left right

exec cue export
cmp stdout out
-- file.cue --
package example

import "encoding/base64"

decoded: "some string"
encoded: base64.Encode(null, decoded)
-- out --
{
"decoded": "some string",
"encoded": "c29tZSBzdHJpbmc="
}
{{{end}}}

## Related content

- The [`encoding/base64`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/base64) 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-base64-encode": {
page: {
cache: {
code: {
cc: "QokSidgCx1xEkbL8TgJpHrJ9gfxIoDOxQElKSlf0JSY="
}
}
}
}
}
}
}
}
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-base64-encode": {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
title: Using the built-in function "encoding/base64.Decode"
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/base64.Decode` to decode
data that is encoded as Base64.

{{< code-tabs >}}
{{< code-tab name="file.cue" language="cue" area="left" >}}
package example

import "encoding/base64"

// encoded contains some Base64-encoded data
encoded: "c29tZSBCYXNlNjQtZW5jb2RlZCBkYXRh"

// decoded contains the string interpolation of
// the byte array returned by base64.Decode
decoded: "\(base64.Decode(null, encoded))"
{{< /code-tab >}}
{{< code-tab name="TERMINAL" language="" type="terminal" area="right" >}}
$ cue export
{
"encoded": "c29tZSBCYXNlNjQtZW5jb2RlZCBkYXRh",
"decoded": "some Base64-encoded data"
}
{{< /code-tab >}}
{{< /code-tabs >}}

## Related content

- The [`encoding/base64`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/base64) built-in package
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
title: Using the built-in function "encoding/base64.Encode"
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/base64.Encode` to
encode a string as Base64.

{{< code-tabs >}}
{{< code-tab name="file.cue" language="cue" area="left" >}}
package example

import "encoding/base64"

decoded: "some string"
encoded: base64.Encode(null, decoded)
{{< /code-tab >}}
{{< code-tab name="TERMINAL" language="" type="terminal" area="right" >}}
$ cue export
{
"decoded": "some string",
"encoded": "c29tZSBzdHJpbmc="
}
{{< /code-tab >}}
{{< /code-tabs >}}

## Related content

- The [`encoding/base64`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/base64) built-in package

0 comments on commit df989e3

Please sign in to comment.