Skip to content

Commit 4ad19c6

Browse files
feat: add b64 raw encode and decode templating function (#535)
Signed-off-by: florian.cazals <[email protected]>
1 parent ac28dd2 commit 4ad19c6

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ The following templating functions are available:
260260
| **`evalCache`** | Evaluates the value of a template variable, and cache for future usage (to avoid further computation) | ``{{evalCache `var1`}}`` |
261261
| **`fromJson`** | Decodes a JSON document into a structure. If the input cannot be decoded as JSON, the function will return an empty string | ``{{fromJson `{"a":"b"}`}}`` |
262262
| **`mustFromJson`** | Similar to **`fromJson`**, but will return an error in case the JSON is invalid. A common usecase consists of returning a JSON stringified data structure from a JavaScript expression (object, array), and use one of its members in the template. Example: ``{{(eval `myExpression` \| fromJson).myArr}}`` or ``{{(eval `myExpression` \| fromJson).myObj}}`` | ``{{mustFromJson `{"a":"b"}`}}`` |
263+
| **`b64RawEnc`** | Encode a string to a b64 raw encoded string as defined in [RFC 4648 section 3.2](https://www.rfc-editor.org/rfc/rfc4648.html#section-3.2). Example: ``{{eval `myString` \| b64RawEnc}}`` | ``{{b64RawEnc `a nice string`}}`` |
264+
| **`b64RawDec`** | Decode a b64 raw encoded string as defined in [RFC 4648 section 3.2](https://www.rfc-editor.org/rfc/rfc4648.html#section-3.2) to a decoded string. Example: ``{{eval `cmF3IG1lc3NhZ2U` \| b64RawDec}}`` | ``{{b64RawDec cmF3IG1lc3NhZ2U`}}`` |
263265

264266
### Basic properties
265267

engine/engine_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -1355,3 +1355,18 @@ func TestResolveCallback(t *testing.T) {
13551355
// callback has been created, waiting for its resolution
13561356
assert.Equal(t, resolution.StateWaiting, res.State)
13571357
}
1358+
1359+
func TestB64RawEncodeDecode(t *testing.T) {
1360+
res, err := createResolution("rawb64EncodingDecoding.yaml", nil, nil)
1361+
assert.NotNil(t, res)
1362+
assert.Nil(t, err)
1363+
1364+
res, err = runResolution(res)
1365+
assert.NotNil(t, res)
1366+
assert.Nil(t, err)
1367+
assert.Equal(t, resolution.StateDone, res.State)
1368+
1369+
output := res.Steps["stepOne"].Output.(map[string]interface{})
1370+
assert.Equal(t, "cmF3IG1lc3NhZ2U", output["a"])
1371+
assert.Equal(t, "raw message", output["b"])
1372+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: rawb64EncodingDecoding
2+
description: Ensure that b64 encoding and decoding can be used in template
3+
title_format: "[test] correct b64 raw encoding and decoding"
4+
auto_runnable: true
5+
6+
7+
8+
variables:
9+
- name: rawDecoded
10+
expression: |-
11+
"raw message";
12+
- name: rawEncoded
13+
expression: |-
14+
"cmF3IG1lc3NhZ2U";
15+
16+
steps:
17+
stepOne:
18+
description: first step
19+
action:
20+
type: echo
21+
configuration:
22+
output:
23+
a: '{{ eval `rawDecoded` | b64RawEnc }}'
24+
b: '{{ eval `rawEncoded` | b64RawDec }}'

engine/values/values.go

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package values
22

33
import (
44
"bytes"
5+
"encoding/base64"
56
"encoding/json"
67
"fmt"
78
"reflect"
@@ -77,6 +78,8 @@ func NewValues() *Values {
7778
v.funcMap["fromJson"] = v.fromJSON
7879
v.funcMap["mustFromJson"] = v.mustFromJSON
7980
v.funcMap["uuid"] = uuid.NewV4
81+
v.funcMap["b64RawEnc"] = v.b64RawEnc
82+
v.funcMap["b64RawDec"] = v.b64RawDec
8083

8184
return v
8285
}
@@ -499,6 +502,18 @@ func (v *Values) mustFromJSON(s string) (reflect.Value, error) {
499502
return reflect.ValueOf(output), err
500503
}
501504

505+
func (v *Values) b64RawDec(s string) string {
506+
data, err := base64.RawStdEncoding.DecodeString(s)
507+
if err != nil {
508+
return err.Error()
509+
}
510+
return string(data)
511+
}
512+
513+
func (v *Values) b64RawEnc(s string) string {
514+
return base64.RawStdEncoding.EncodeToString([]byte(s))
515+
}
516+
502517
var errTimedOut = errors.New("Timed out variable evaluation")
503518

504519
func evalUnsafe(exp []byte, delay time.Duration) (v otto.Value, err error) {

0 commit comments

Comments
 (0)