Skip to content

Commit

Permalink
feat(syntax/stdlib): add base64_decode helper function (#953)
Browse files Browse the repository at this point in the history
* feat(syntax/stdlib): add `base64_decode` helper function

Signed-off-by: hainenber <[email protected]>

* doc: correct title for `base64_decode` func ref

Signed-off-by: hainenber <[email protected]>

* chore: use a generic string for `base64_decode` code example

Signed-off-by: hainenber <[email protected]>

---------

Signed-off-by: hainenber <[email protected]>
  • Loading branch information
hainenber authored Jun 7, 2024
1 parent 10da36a commit fa02432
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ Main (unreleased)

- Prefix Faro measurement values with `value_` to align with the latest Faro cloud receiver updates. (@codecapitano)

- Add `base64_decode` to standard library. (@hainenber)

### Bugfixes

- Fixed an issue with `prometheus.scrape` in which targets that move from one
Expand Down
19 changes: 19 additions & 0 deletions docs/sources/reference/stdlib/base64_decode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
canonical: https://grafana.com/docs/alloy/latest/reference/stdlib/base64_decode/
description: Learn about base64_decode
title: base64_decode
---

# base64_decode

The `base64_decode` function decodes a RFC4648-compliant Base64-encoded string
into the original string.

`base64_decode` fails if the provided string argument contains invalid Base64 data.

## Examples

```
> base64_decode("dGFuZ2VyaW5l")
tangerine
```
9 changes: 9 additions & 0 deletions syntax/internal/stdlib/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package stdlib

import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
Expand Down Expand Up @@ -93,6 +94,14 @@ var Identifiers = map[string]interface{}{
return res, nil
},

"base64_decode": func(in string) (interface{}, error) {
decoded, err := base64.StdEncoding.DecodeString(in)
if err != nil {
return nil, err
}
return decoded, nil
},

"json_path": func(jsonString string, path string) (interface{}, error) {
jsonPathExpr, err := jp.ParseString(path)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions syntax/vm/vm_stdlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestVM_Stdlib(t *testing.T) {
{"yaml_decode array float", "yaml_decode(`[0.0, 1.0, 2.0]`)", []interface{}{float64(0), float64(1), float64(2)}},
{"yaml_decode nil field", "yaml_decode(`foo: null`)", map[string]interface{}{"foo": nil}},
{"yaml_decode nil array element", `yaml_decode("[0, null]")`, []interface{}{0, nil}},
{"base64_decode", `base64_decode("Zm9vYmFyMTIzIT8kKiYoKSctPUB+")`, string(`foobar123!?$*&()'-=@~`)},
}

for _, tc := range tt {
Expand Down

0 comments on commit fa02432

Please sign in to comment.