diff --git a/docs/docs/articles/test-workflows-expressions.md b/docs/docs/articles/test-workflows-expressions.md index adce5565a13..48f886f410f 100644 --- a/docs/docs/articles/test-workflows-expressions.md +++ b/docs/docs/articles/test-workflows-expressions.md @@ -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"`
`join(["a", "b"], " - ")` is `"a - b"` | -| `split` | `list` | Split string to list | `split("a,b,c")` is `["a", "b", "c"]`
`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`
`len("foobar")` is `6`
`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`
`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\""`
`shellquote("foo", "bar baz")` is `"foo \"bar baz\""` | -| `shellparse` | `[]string` | Parse shell arguments | `shellparse("foo bar")` is `["foo", "bar"]`
`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 | jq([1,2,3,4,5], ". | max") is `[5]` | -| `range` | `[]int` | Build range of numbers | `range(5, 10)` is `[5, 6, 7, 8, 9]`
`range(5)` is `[0, 1, 2, 3, 4]` | -| `relpath` | `string` | Build relative path | `relpath("/a/b/c")` may be `./b/c`
`relpath("/a/b/c", "/a/b")` is `"./c"` | -| `abspath` | `string` | Build absolute path | `abspath("/a/b/c")` is `/a/b/c`
`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"`
`join(["a", "b"], " - ")` is `"a - b"` | +| `split` | `list` | Split string to list | `split("a,b,c")` is `["a", "b", "c"]`
`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`
`len("foobar")` is `6`
`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`
`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\""`
`shellquote("foo", "bar baz")` is `"foo \"bar baz\""` | +| `shellparse` | `[]string` | Parse shell arguments | `shellparse("foo bar")` is `["foo", "bar"]`
`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 | jq([1,2,3,4,5], ". | max") is `[5]` | +| `range` | `[]int` | Build range of numbers | `range(5, 10)` is `[5, 6, 7, 8, 9]`
`range(5)` is `[0, 1, 2, 3, 4]` | +| `relpath` | `string` | Build relative path | `relpath("/a/b/c")` may be `./b/c`
`relpath("/a/b/c", "/a/b")` is `"./c"` | +| `abspath` | `string` | Build absolute path | `abspath("/a/b/c")` is `/a/b/c`
`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"`
`date("2006-01-02")` may be `2024-06-04` | ### File System diff --git a/pkg/tcl/expressionstcl/parse_test.go b/pkg/tcl/expressionstcl/parse_test.go index f2a8c0f22af..a49bc481c27 100644 --- a/pkg/tcl/expressionstcl/parse_test.go +++ b/pkg/tcl/expressionstcl/parse_test.go @@ -13,6 +13,7 @@ import ( "fmt" "strings" "testing" + "time" "github.com/stretchr/testify/assert" ) @@ -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) { diff --git a/pkg/tcl/expressionstcl/stdlib.go b/pkg/tcl/expressionstcl/stdlib.go index 38fa662a98c..95dc30d18c4 100644 --- a/pkg/tcl/expressionstcl/stdlib.go +++ b/pkg/tcl/expressionstcl/stdlib.go @@ -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) @@ -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 (