Skip to content

Commit ca541d7

Browse files
committed
Support case-insensitive base64 encode variant argument
1 parent 80911d1 commit ca541d7

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

pkg/ottl/ottlfuncs/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ Examples:
587587
The `Base64Encode` Converter takes a string and returns a base64 encoded string.
588588

589589
`value` is a string to encode.
590-
`variant` (optional) is the base64 encoding variant to use. Valid values are `base64` (standard, with padding), `base64-raw` (standard, no padding), `base64-url` (URL-safe, with padding), or `base64-raw-url` (URL-safe, no padding). Defaults to `base64` if not specified.
590+
`variant` (optional) is the base64 encoding variant to use. Valid values are `base64` (standard, with padding), `base64-raw` (standard, no padding), `base64-url` (URL-safe, with padding), or `base64-raw-url` (URL-safe, no padding). The variant name is case-insensitive. Defaults to `base64` if not specified.
591591

592592
Examples:
593593

@@ -602,6 +602,9 @@ Examples:
602602

603603
- `Base64Encode(attributes["data"], "base64-raw")`
604604

605+
606+
- `Base64Encode(attributes["data"], "BASE64-URL")` # Variant names are case-insensitive
607+
605608
### Bool
606609

607610
`Bool(value)`

pkg/ottl/ottlfuncs/func_base64encode.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/base64"
99
"errors"
1010
"fmt"
11+
"strings"
1112

1213
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
1314
)
@@ -47,7 +48,7 @@ func Base64Encode[K any](target ottl.StringGetter[K], variant ottl.Optional[ottl
4748
}
4849
}
4950

50-
switch variantVal {
51+
switch strings.ToLower(variantVal) {
5152
case "base64":
5253
return base64.StdEncoding.EncodeToString(data), nil
5354
case "base64-raw":

pkg/ottl/ottlfuncs/func_base64encode_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ func TestBase64Encode(t *testing.T) {
6969
variant: "base64-raw-url",
7070
want: "ZGF0YSt2YWx1ZXMvaXRlbXM",
7171
},
72+
{
73+
name: "case insensitive variant - BASE64",
74+
value: "test string",
75+
variant: "BASE64",
76+
want: "dGVzdCBzdHJpbmc=",
77+
},
78+
{
79+
name: "case insensitive variant - Base64-Raw",
80+
value: "test string",
81+
variant: "Base64-Raw",
82+
want: "dGVzdCBzdHJpbmc",
83+
},
84+
{
85+
name: "case insensitive variant - BASE64-URL",
86+
value: "data+values/items",
87+
variant: "BASE64-URL",
88+
want: "ZGF0YSt2YWx1ZXMvaXRlbXM=",
89+
},
7290
{
7391
name: "unsupported type int",
7492
value: 10,

0 commit comments

Comments
 (0)