lang/funcs: Rename the base64 character encoding functions

These were initially introduced as functions with "encode" and "decode"
prefixes, but that doesn't match with our existing convention of putting
the encoding format first so that the encode and decode functions will
group together in a alphabetically-ordered function list.

"text" is not really a defined serialization format, but it's a short word
that hopefully represents well enough what these functions are aiming to
encode and decode, while being consistent with existing functions like
jsonencode/jsondecode, yamlencode/yamldecode, etc.

The "base64" at the end here is less convincing because there is precedent
for that modifier to appear both at the beginning and the end in our
existing function names. I chose to put it at the end here because that
seems to be our emergent convention for situations where the base64
encoding is a sort of secondary modifier alongside the primary purpose
of the function, as we see with "filebase64". (base64gzip is an exception
here, but it seems outvoted by the others.)
This commit is contained in:
Martin Atkins 2020-10-21 10:56:56 -07:00
parent 877399c631
commit 1dc4950bfa
9 changed files with 59 additions and 49 deletions

View File

@ -51,8 +51,8 @@ var Base64EncodeFunc = function.New(&function.Spec{
}, },
}) })
// Base64TextDecodeFunc constructs a function that encodes a string to a target encoding and then to a base64 sequence. // TextEncodeBase64Func constructs a function that encodes a string to a target encoding and then to a base64 sequence.
var Base64TextEncodeFunc = function.New(&function.Spec{ var TextEncodeBase64Func = function.New(&function.Spec{
Params: []function.Parameter{ Params: []function.Parameter{
{ {
Name: "string", Name: "string",
@ -94,8 +94,8 @@ var Base64TextEncodeFunc = function.New(&function.Spec{
}, },
}) })
// Base64TextDecodeFunc constructs a function that decodes a base64 sequence to a target encoding. // TextDecodeBase64Func constructs a function that decodes a base64 sequence to a target encoding.
var Base64TextDecodeFunc = function.New(&function.Spec{ var TextDecodeBase64Func = function.New(&function.Spec{
Params: []function.Parameter{ Params: []function.Parameter{
{ {
Name: "source", Name: "source",
@ -229,7 +229,7 @@ func URLEncode(str cty.Value) (cty.Value, error) {
return URLEncodeFunc.Call([]cty.Value{str}) return URLEncodeFunc.Call([]cty.Value{str})
} }
// Base64TextEncode applies Base64 encoding to a string that was encoded before with a target encoding. // TextEncodeBase64 applies Base64 encoding to a string that was encoded before with a target encoding.
// //
// Terraform uses the "standard" Base64 alphabet as defined in RFC 4648 section 4. // Terraform uses the "standard" Base64 alphabet as defined in RFC 4648 section 4.
// //
@ -237,17 +237,17 @@ func URLEncode(str cty.Value) (cty.Value, error) {
// Strings in the Terraform language are sequences of unicode characters rather // Strings in the Terraform language are sequences of unicode characters rather
// than bytes, so this function will first encode the characters from the string // than bytes, so this function will first encode the characters from the string
// as UTF-8, and then apply Base64 encoding to the result. // as UTF-8, and then apply Base64 encoding to the result.
func Base64TextEncode(str, enc cty.Value) (cty.Value, error) { func TextEncodeBase64(str, enc cty.Value) (cty.Value, error) {
return Base64TextEncodeFunc.Call([]cty.Value{str, enc}) return TextEncodeBase64Func.Call([]cty.Value{str, enc})
} }
// Base64TextDecode decodes a string containing a base64 sequence whereas a specific encoding of the string is expected. // TextDecodeBase64 decodes a string containing a base64 sequence whereas a specific encoding of the string is expected.
// //
// Terraform uses the "standard" Base64 alphabet as defined in RFC 4648 section 4. // Terraform uses the "standard" Base64 alphabet as defined in RFC 4648 section 4.
// //
// Strings in the Terraform language are sequences of unicode characters rather // Strings in the Terraform language are sequences of unicode characters rather
// than bytes, so this function will also interpret the resulting bytes as // than bytes, so this function will also interpret the resulting bytes as
// the target encoding. // the target encoding.
func Base64TextDecode(str, enc cty.Value) (cty.Value, error) { func TextDecodeBase64(str, enc cty.Value) (cty.Value, error) {
return Base64TextDecodeFunc.Call([]cty.Value{str, enc}) return TextDecodeBase64Func.Call([]cty.Value{str, enc})
} }

View File

@ -216,8 +216,8 @@ func TestBase64TextEncode(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
t.Run(fmt.Sprintf("encodetextbase64(%#v, %#v)", test.String, test.Encoding), func(t *testing.T) { t.Run(fmt.Sprintf("textencodebase64(%#v, %#v)", test.String, test.Encoding), func(t *testing.T) {
got, err := Base64TextEncode(test.String, test.Encoding) got, err := TextEncodeBase64(test.String, test.Encoding)
if test.Err != "" { if test.Err != "" {
if err == nil { if err == nil {
@ -296,8 +296,8 @@ func TestBase64TextDecode(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
t.Run(fmt.Sprintf("decodetextbase64(%#v, %#v)", test.String, test.Encoding), func(t *testing.T) { t.Run(fmt.Sprintf("textdecodebase64(%#v, %#v)", test.String, test.Encoding), func(t *testing.T) {
got, err := Base64TextDecode(test.String, test.Encoding) got, err := TextDecodeBase64(test.String, test.Encoding)
if test.Err != "" { if test.Err != "" {
if err == nil { if err == nil {

View File

@ -54,11 +54,9 @@ func (s *Scope) Functions() map[string]function.Function {
"concat": stdlib.ConcatFunc, "concat": stdlib.ConcatFunc,
"contains": stdlib.ContainsFunc, "contains": stdlib.ContainsFunc,
"csvdecode": stdlib.CSVDecodeFunc, "csvdecode": stdlib.CSVDecodeFunc,
"decodetextbase64": funcs.Base64TextDecodeFunc,
"dirname": funcs.DirnameFunc, "dirname": funcs.DirnameFunc,
"distinct": stdlib.DistinctFunc, "distinct": stdlib.DistinctFunc,
"element": stdlib.ElementFunc, "element": stdlib.ElementFunc,
"encodetextbase64": funcs.Base64TextEncodeFunc,
"chunklist": stdlib.ChunklistFunc, "chunklist": stdlib.ChunklistFunc,
"file": funcs.MakeFileFunc(s.BaseDir, false), "file": funcs.MakeFileFunc(s.BaseDir, false),
"fileexists": funcs.MakeFileExistsFunc(s.BaseDir), "fileexists": funcs.MakeFileExistsFunc(s.BaseDir),
@ -115,6 +113,8 @@ func (s *Scope) Functions() map[string]function.Function {
"strrev": stdlib.ReverseFunc, "strrev": stdlib.ReverseFunc,
"substr": stdlib.SubstrFunc, "substr": stdlib.SubstrFunc,
"sum": funcs.SumFunc, "sum": funcs.SumFunc,
"textdecodebase64": funcs.TextDecodeBase64Func,
"textencodebase64": funcs.TextEncodeBase64Func,
"timestamp": funcs.TimestampFunc, "timestamp": funcs.TimestampFunc,
"timeadd": stdlib.TimeAddFunc, "timeadd": stdlib.TimeAddFunc,
"title": stdlib.TitleFunc, "title": stdlib.TitleFunc,

View File

@ -282,13 +282,6 @@ func TestFunctions(t *testing.T) {
}, },
}, },
"decodetextbase64": {
{
`decodetextbase64("dABlAHMAdAA=", "UTF-16LE")`,
cty.StringVal("test"),
},
},
"dirname": { "dirname": {
{ {
`dirname("testdata/hello.txt")`, `dirname("testdata/hello.txt")`,
@ -305,13 +298,6 @@ func TestFunctions(t *testing.T) {
}, },
}, },
"encodetextbase64": {
{
`encodetextbase64("test", "UTF-16LE")`,
cty.StringVal("dABlAHMAdAA="),
},
},
"element": { "element": {
{ {
`element(["hello"], 0)`, `element(["hello"], 0)`,
@ -820,6 +806,20 @@ func TestFunctions(t *testing.T) {
}, },
}, },
"textdecodebase64": {
{
`textdecodebase64("dABlAHMAdAA=", "UTF-16LE")`,
cty.StringVal("test"),
},
},
"textencodebase64": {
{
`textencodebase64("test", "UTF-16LE")`,
cty.StringVal("dABlAHMAdAA="),
},
},
"templatefile": { "templatefile": {
{ {
`templatefile("hello.tmpl", {name = "Jodie"})`, `templatefile("hello.tmpl", {name = "Jodie"})`,

View File

@ -31,7 +31,7 @@ most cases. Various other functions with names containing "base64" can generate
or manipulate Base64 data directly. or manipulate Base64 data directly.
`base64decode` is, in effect, a shorthand for calling `base64decode` is, in effect, a shorthand for calling
[`decodetextbase64`](./decodetextbase64.html) with the encoding name set to [`textdecodebase64`](./textdecodebase64.html) with the encoding name set to
`UTF-8`. `UTF-8`.
## Examples ## Examples
@ -45,7 +45,7 @@ Hello World
* [`base64encode`](./base64encode.html) performs the opposite operation, * [`base64encode`](./base64encode.html) performs the opposite operation,
encoding the UTF-8 bytes for a string as Base64. encoding the UTF-8 bytes for a string as Base64.
* [`decodetextbase64`](./decodetextbase64.html) is a more general function that * [`textdecodebase64`](./textdecodebase64.html) is a more general function that
supports character encodings other than UTF-8. supports character encodings other than UTF-8.
* [`base64gzip`](./base64gzip.html) applies gzip compression to a string * [`base64gzip`](./base64gzip.html) applies gzip compression to a string
and returns the result with Base64 encoding. and returns the result with Base64 encoding.

View File

@ -32,7 +32,7 @@ Base64 themselves, and so this function exists primarily to allow string
data to be easily provided to resource types that expect Base64 bytes. data to be easily provided to resource types that expect Base64 bytes.
`base64encode` is, in effect, a shorthand for calling `base64encode` is, in effect, a shorthand for calling
[`encodetextbase64`](./encodetextbase64.html) with the encoding name set to [`textencodebase64`](./textencodebase64.html) with the encoding name set to
`UTF-8`. `UTF-8`.
## Examples ## Examples
@ -46,7 +46,7 @@ SGVsbG8gV29ybGQ=
* [`base64decode`](./base64decode.html) performs the opposite operation, * [`base64decode`](./base64decode.html) performs the opposite operation,
decoding Base64 data and interpreting it as a UTF-8 string. decoding Base64 data and interpreting it as a UTF-8 string.
* [`encodetextbase64`](./encodetextbase64.html) is a more general function that * [`textencodebase64`](./textencodebase64.html) is a more general function that
supports character encodings other than UTF-8. supports character encodings other than UTF-8.
* [`base64gzip`](./base64gzip.html) applies gzip compression to a string * [`base64gzip`](./base64gzip.html) applies gzip compression to a string
and returns the result with Base64 encoding all in one operation. and returns the result with Base64 encoding all in one operation.

View File

@ -1,16 +1,17 @@
--- ---
layout: "functions" layout: "functions"
page_title: "encodetextbase64 - Functions - Configuration Language" page_title: "textdecodebase64 - Functions - Configuration Language"
sidebar_current: "docs-funcs-encoding-encodetextbase64" sidebar_current: "docs-funcs-encoding-textdecodebase64"
description: |- description: |-
The encodetextbase64 function decodes a string containing a base64 sequence assuming that the target encoding was used. The textdecodebase64 function decodes a string that was previously Base64-encoded,
and then interprets the result as characters in a specified character encoding.
--- ---
# `decodetextbase64` Function # `textdecodebase64` Function
-> **Note:** This function is supported only in Terraform v0.14 and later. -> **Note:** This function is supported only in Terraform v0.14 and later.
`decodetextbase64` function decodes a string that was previously Base64-encoded, `textdecodebase64` function decodes a string that was previously Base64-encoded,
and then interprets the result as characters in a specified character encoding. and then interprets the result as characters in a specified character encoding.
Terraform uses the "standard" Base64 alphabet as defined in Terraform uses the "standard" Base64 alphabet as defined in
@ -28,13 +29,13 @@ as [`base64decode`](./base64decode.html).
## Examples ## Examples
``` ```
> decodetextbase64("SABlAGwAbABvACAAVwBvAHIAbABkAA==", "UTF-16LE") > textdecodebase64("SABlAGwAbABvACAAVwBvAHIAbABkAA==", "UTF-16LE")
Hello World Hello World
``` ```
## Related Functions ## Related Functions
* [`encodetextbase64`](./encodetextbase64.html) performs the opposite operation, * [`textencodebase64`](./textencodebase64.html) performs the opposite operation,
applying target encoding and then Base64 to a string. applying target encoding and then Base64 to a string.
* [`base64decode`](./base64decode.html) is effectively a shorthand for * [`base64decode`](./base64decode.html) is effectively a shorthand for
`decodetextbase64` where the character encoding is fixed as `UTF-8`. `textdecodebase64` where the character encoding is fixed as `UTF-8`.

View File

@ -1,16 +1,17 @@
--- ---
layout: "functions" layout: "functions"
page_title: "encodetextbase64 - Functions - Configuration Language" page_title: "textencodebase64 - Functions - Configuration Language"
sidebar_current: "docs-funcs-encoding-encodetextbase64" sidebar_current: "docs-funcs-encoding-textencodebase64"
description: |- description: |-
The encodetextbase64 function applies Base64 encoding to a string that was encoded to target encoding before. The textencodebase64 function encodes the unicode characters in a given string using a
specified character encoding, returning the result base64 encoded.
--- ---
# `encodetextbase64` Function # `textencodebase64` Function
-> **Note:** This function is supported only in Terraform v0.14 and later. -> **Note:** This function is supported only in Terraform v0.14 and later.
`encodetextbase64` encodes the unicode characters in a given string using a `textencodebase64` encodes the unicode characters in a given string using a
specified character encoding, returning the result base64 encoded because specified character encoding, returning the result base64 encoded because
Terraform language strings are always sequences of unicode characters. Terraform language strings are always sequences of unicode characters.
@ -35,13 +36,13 @@ result as [`base64encode`](./base64encode.html).
## Examples ## Examples
``` ```
> encodetextbase64("Hello World", "UTF-16LE") > textencodebase64("Hello World", "UTF-16LE")
SABlAGwAbABvACAAVwBvAHIAbABkAA== SABlAGwAbABvACAAVwBvAHIAbABkAA==
``` ```
## Related Functions ## Related Functions
* [`decodetextbase64`](./decodetextbase64.html) performs the opposite operation, * [`textdecodebase64`](./textdecodebase64.html) performs the opposite operation,
decoding Base64 data and interpreting it as a particular character encoding. decoding Base64 data and interpreting it as a particular character encoding.
* [`base64encode`](./base64encode.html) applies Base64 encoding of the UTF-8 * [`base64encode`](./base64encode.html) applies Base64 encoding of the UTF-8
encoding of a string. encoding of a string.

View File

@ -289,6 +289,14 @@
<a href="/docs/configuration/functions/jsonencode.html">jsonencode</a> <a href="/docs/configuration/functions/jsonencode.html">jsonencode</a>
</li> </li>
<li>
<a href="/docs/configuration/functions/textdecodebase64.html">textdecodebase64</a>
</li>
<li>
<a href="/docs/configuration/functions/textencodebase64.html">textencodebase64</a>
</li>
<li> <li>
<a href="/docs/configuration/functions/urlencode.html">urlencode</a> <a href="/docs/configuration/functions/urlencode.html">urlencode</a>
</li> </li>