Skip to content

Commit

Permalink
feat(testworkflows): provide a way to get current date in TestWorkflow (
Browse files Browse the repository at this point in the history
  • Loading branch information
rangoo94 authored Jun 6, 2024
1 parent 180cbdc commit 015d17d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
47 changes: 24 additions & 23 deletions docs/docs/articles/test-workflows-expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,29 +138,30 @@ There are some functions that help to cast values to a different type. Additiona

### General

| Name | Returns | Description | Example |
|--------------|-----------------|-----------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------|
| `join` | `string` | Join list elements | `join(["a", "b"])` is `"a,b"`<br />`join(["a", "b"], " - ")` is `"a - b"` |
| `split` | `list` | Split string to list | `split("a,b,c")` is `["a", "b", "c"]`<br />`split("a - b - c", " - ")` is `["a", "b", "c"]` |
| `trim` | `string` | Trim whitespaces from the string | `trim(" \nabc d ")` is `"abc d"` |
| `len` | `int` | Length of array, map or string | `len([ "a", "b" ])` is `2`<br />`len("foobar")` is `6`<br />`len({ "foo": "bar" })` is `1` |
| `floor` | `int` | Round value down | `floor(10.5)` is `10` |
| `ceil` | `int` | Round value up | `ceil(10.5)` is `11` |
| `round` | `int` | Round value to nearest integer | `round(10.5)` is `11` |
| `at` | anything | Get value of the element | `at([10, 2], 1)` is `2`<br />`at({"foo": "bar"}, "foo")` is `"bar"` |
| `tojson` | `string` | Serialize value to JSON | `tojson({ "foo": "bar" })` is `"{\"foo\":\"bar\"}"` |
| `json` | anything | Parse the JSON | `json("{\"foo\":\"bar\"}")` is `{ "foo": "bar" }` |
| `toyaml` | `string` | Serialize value to YAML | `toyaml({ "foo": "bar" })` is `"foo: bar\n` |
| `yaml` | anything | Parse the YAML | `yaml("foo: bar")` is `{ "foo": "bar" }` |
| `shellquote` | `string` | Sanitize arguments for shell | `shellquote("foo bar")` is `"\"foo bar\""`<br />`shellquote("foo", "bar baz")` is `"foo \"bar baz\""` |
| `shellparse` | `[]string` | Parse shell arguments | `shellparse("foo bar")` is `["foo", "bar"]`<br />`shellparse("foo \"bar baz\"")` is `["foo", "bar baz"]` |
| `map` | `list` or `map` | Map list or map values with expression; `_.value` and `_.index`/`_.key` are available | `map([1,2,3,4,5], "_.value * 2")` is `[2,4,6,8,10]` |
| `filter` | `list` | Filter list values with expression; `_.value` and `_.index` are available | `filter([1,2,3,4,5], "_.value > 2")` is `[3,4,5]` |
| `jq` | anything | Execute [**jq**](https://en.wikipedia.org/wiki/Jq_(programming_language)) against value | <code>jq([1,2,3,4,5], ". &#124; max")</code> is `[5]` |
| `range` | `[]int` | Build range of numbers | `range(5, 10)` is `[5, 6, 7, 8, 9]`<br />`range(5)` is `[0, 1, 2, 3, 4]` |
| `relpath` | `string` | Build relative path | `relpath("/a/b/c")` may be `./b/c`<br />`relpath("/a/b/c", "/a/b")` is `"./c"` |
| `abspath` | `string` | Build absolute path | `abspath("/a/b/c")` is `/a/b/c`<br />`abspath("b/c")` may be `/some/working/dir/b/c` |
| `chunk` | `[]list` | Split list to chunks of specified maximum size | `chunk([1,2,3,4,5], 2)` is `[[1,2], [3,4], [5]]` |
| Name | Returns | Description | Example |
|--------------|-----------------|-------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------|
| `join` | `string` | Join list elements | `join(["a", "b"])` is `"a,b"`<br />`join(["a", "b"], " - ")` is `"a - b"` |
| `split` | `list` | Split string to list | `split("a,b,c")` is `["a", "b", "c"]`<br />`split("a - b - c", " - ")` is `["a", "b", "c"]` |
| `trim` | `string` | Trim whitespaces from the string | `trim(" \nabc d ")` is `"abc d"` |
| `len` | `int` | Length of array, map or string | `len([ "a", "b" ])` is `2`<br />`len("foobar")` is `6`<br />`len({ "foo": "bar" })` is `1` |
| `floor` | `int` | Round value down | `floor(10.5)` is `10` |
| `ceil` | `int` | Round value up | `ceil(10.5)` is `11` |
| `round` | `int` | Round value to nearest integer | `round(10.5)` is `11` |
| `at` | anything | Get value of the element | `at([10, 2], 1)` is `2`<br />`at({"foo": "bar"}, "foo")` is `"bar"` |
| `tojson` | `string` | Serialize value to JSON | `tojson({ "foo": "bar" })` is `"{\"foo\":\"bar\"}"` |
| `json` | anything | Parse the JSON | `json("{\"foo\":\"bar\"}")` is `{ "foo": "bar" }` |
| `toyaml` | `string` | Serialize value to YAML | `toyaml({ "foo": "bar" })` is `"foo: bar\n` |
| `yaml` | anything | Parse the YAML | `yaml("foo: bar")` is `{ "foo": "bar" }` |
| `shellquote` | `string` | Sanitize arguments for shell | `shellquote("foo bar")` is `"\"foo bar\""`<br />`shellquote("foo", "bar baz")` is `"foo \"bar baz\""` |
| `shellparse` | `[]string` | Parse shell arguments | `shellparse("foo bar")` is `["foo", "bar"]`<br />`shellparse("foo \"bar baz\"")` is `["foo", "bar baz"]` |
| `map` | `list` or `map` | Map list or map values with expression; `_.value` and `_.index`/`_.key` are available | `map([1,2,3,4,5], "_.value * 2")` is `[2,4,6,8,10]` |
| `filter` | `list` | Filter list values with expression; `_.value` and `_.index` are available | `filter([1,2,3,4,5], "_.value > 2")` is `[3,4,5]` |
| `jq` | anything | Execute [**jq**](https://en.wikipedia.org/wiki/Jq_(programming_language)) against value | <code>jq([1,2,3,4,5], ". &#124; max")</code> is `[5]` |
| `range` | `[]int` | Build range of numbers | `range(5, 10)` is `[5, 6, 7, 8, 9]`<br />`range(5)` is `[0, 1, 2, 3, 4]` |
| `relpath` | `string` | Build relative path | `relpath("/a/b/c")` may be `./b/c`<br />`relpath("/a/b/c", "/a/b")` is `"./c"` |
| `abspath` | `string` | Build absolute path | `abspath("/a/b/c")` is `/a/b/c`<br />`abspath("b/c")` may be `/some/working/dir/b/c` |
| `chunk` | `[]list` | Split list to chunks of specified maximum size | `chunk([1,2,3,4,5], 2)` is `[[1,2], [3,4], [5]]` |
| `date` | `string` | Return current date (either `2006-01-02T15:04:05.000Z07:00` format or custom argument ([**Go syntax**](https://go.dev/src/time/format.go#L101)) | `date()` may be `"2024-06-04T11:59:32.308Z"`<br />`date("2006-01-02")` may be `2024-06-04` |

### File System

Expand Down
3 changes: 3 additions & 0 deletions pkg/tcl/expressionstcl/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"fmt"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -290,6 +291,8 @@ a:
assert.Equal(t, `[]`, MustCompile(`range(5, 3)`).String())
assert.Equal(t, `[0,1,2,3,4]`, MustCompile(`range(5)`).String())
assert.Equal(t, `[5,6,7]`, MustCompile(`range(5, 8)`).String())
assert.InDelta(t, time.Now().UnixMilli(), must(time.Parse(RFC3339Millis, must(MustCompile(`date()`).Static().StringValue()))).UnixMilli(), 5)
assert.Equal(t, time.Now().Truncate(24*time.Hour).UnixMilli(), must(time.Parse("2006-01-02", must(MustCompile(`date("2006-01-02")`).Static().StringValue()))).UnixMilli())
}

func TestCompileWildcard_Unknown(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions pkg/tcl/expressionstcl/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import (
"gopkg.in/yaml.v3"
)

const (
RFC3339Millis = "2006-01-02T15:04:05.000Z07:00"
)

type StdFunction struct {
ReturnType Type
Handler func(...StaticValue) (Expression, error)
Expand Down Expand Up @@ -551,6 +555,18 @@ var stdFunctions = map[string]StdFunction{
return NewValue(result), nil
},
},
"date": {
ReturnType: TypeString,
Handler: func(value ...StaticValue) (Expression, error) {
if len(value) == 0 {
return NewValue(time.Now().UTC().Format(RFC3339Millis)), nil
} else if len(value) == 1 {
format, _ := value[0].StringValue()
return NewValue(time.Now().UTC().Format(format)), nil
}
return nil, fmt.Errorf(`"date" function expects 0-1 arguments, %d provided`, len(value))
},
},
}

const (
Expand Down

0 comments on commit 015d17d

Please sign in to comment.