Skip to content

Commit

Permalink
feat: add b64 raw encode and decode templating function (#535)
Browse files Browse the repository at this point in the history
Signed-off-by: florian.cazals <[email protected]>
  • Loading branch information
floriancazals authored May 22, 2024
1 parent ac28dd2 commit 4ad19c6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ The following templating functions are available:
| **`evalCache`** | Evaluates the value of a template variable, and cache for future usage (to avoid further computation) | ``{{evalCache `var1`}}`` |
| **`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"}`}}`` |
| **`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"}`}}`` |
| **`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`}}`` |
| **`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`}}`` |

### Basic properties

Expand Down
15 changes: 15 additions & 0 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1355,3 +1355,18 @@ func TestResolveCallback(t *testing.T) {
// callback has been created, waiting for its resolution
assert.Equal(t, resolution.StateWaiting, res.State)
}

func TestB64RawEncodeDecode(t *testing.T) {
res, err := createResolution("rawb64EncodingDecoding.yaml", nil, nil)
assert.NotNil(t, res)
assert.Nil(t, err)

res, err = runResolution(res)
assert.NotNil(t, res)
assert.Nil(t, err)
assert.Equal(t, resolution.StateDone, res.State)

output := res.Steps["stepOne"].Output.(map[string]interface{})
assert.Equal(t, "cmF3IG1lc3NhZ2U", output["a"])
assert.Equal(t, "raw message", output["b"])
}
24 changes: 24 additions & 0 deletions engine/templates_tests/rawb64EncodingDecoding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: rawb64EncodingDecoding
description: Ensure that b64 encoding and decoding can be used in template
title_format: "[test] correct b64 raw encoding and decoding"
auto_runnable: true



variables:
- name: rawDecoded
expression: |-
"raw message";
- name: rawEncoded
expression: |-
"cmF3IG1lc3NhZ2U";
steps:
stepOne:
description: first step
action:
type: echo
configuration:
output:
a: '{{ eval `rawDecoded` | b64RawEnc }}'
b: '{{ eval `rawEncoded` | b64RawDec }}'
15 changes: 15 additions & 0 deletions engine/values/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package values

import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"reflect"
Expand Down Expand Up @@ -77,6 +78,8 @@ func NewValues() *Values {
v.funcMap["fromJson"] = v.fromJSON
v.funcMap["mustFromJson"] = v.mustFromJSON
v.funcMap["uuid"] = uuid.NewV4
v.funcMap["b64RawEnc"] = v.b64RawEnc
v.funcMap["b64RawDec"] = v.b64RawDec

return v
}
Expand Down Expand Up @@ -499,6 +502,18 @@ func (v *Values) mustFromJSON(s string) (reflect.Value, error) {
return reflect.ValueOf(output), err
}

func (v *Values) b64RawDec(s string) string {
data, err := base64.RawStdEncoding.DecodeString(s)
if err != nil {
return err.Error()
}
return string(data)
}

func (v *Values) b64RawEnc(s string) string {
return base64.RawStdEncoding.EncodeToString([]byte(s))
}

var errTimedOut = errors.New("Timed out variable evaluation")

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

0 comments on commit 4ad19c6

Please sign in to comment.