Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add first and last functions. #1636

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions docs/cel_expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,35 @@ which can be accessed by indexing.
<pre>{"testing":"value"}.marshalJSON() == "{\"testing\": \"value\"}"</pre>
</td>
</tr>
<tr>
<th>
first()
</th>
<td>
<pre>&lt;jsonArray&gt;.first() -> &lt;jsonObject&gt;</pre>
</td>
<td>
Returns the first element in the array.
</td>
<td>
<pre>[1, 2, 3, 4, 5].first() == 1</pre>
</td>
</tr>
<tr>
<th>
last()
</th>
<td>
<pre>&lt;jsonArray&gt;.last() -> &lt;jsonObject&gt;</pre>
</td>
<td>
Returns the last element in the array.
</td>
<td>
<pre>[1, 2, 3, 4, 5].last() == 5</pre>
</td>
</tr>

</table>

## Troubleshooting CEL expressions
Expand Down
45 changes: 45 additions & 0 deletions pkg/interceptors/cel/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,10 @@ func TestExpressionEvaluation(t *testing.T) {
},
},
},
"numbers": []int64{
1, 2, 3, 4, 5,
},
"emptyList": []int64{},
}

refParts := strings.Split(testRef, "/")
Expand Down Expand Up @@ -621,6 +625,47 @@ func TestExpressionEvaluation(t *testing.T) {
expr: "body.jsonArray.join(', ')",
want: types.String("one, two"),
},
{
name: "last element in array",
expr: "body.testURL.parseURL().path.split('/').last()",
want: types.String("to"),
},
{
name: "last element in empty array",
expr: `body.emptyList.last()`,
want: types.NullValue,
},
{
name: "last element in numeric array",
expr: `body.numbers.last()`,
want: types.Int(5),
},
{
name: "last element in literal array",
expr: "[1, 2, 3, 4, 5].last()",
want: types.Int(5),
},
{
name: "first element in literal array",
expr: "[1, 2, 3, 4, 5].first()",
want: types.Int(1),
},
{
name: "first element in array",
// Splitting gets an empty route element so this filters it out.
expr: "body.testURL.parseURL().path.split('/').filter(t, t.size() > 0).first()",
want: types.String("path"),
},
{
name: "first element in empty array",
expr: `body.emptyList.first()`,
want: types.NullValue,
},
{
name: "first element in numeric array",
expr: `body.numbers.first()`,
want: types.Int(1),
},
}
for _, tt := range tests {
t.Run(tt.name, func(rt *testing.T) {
Expand Down
29 changes: 29 additions & 0 deletions pkg/interceptors/cel/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/google/cel-go/common/types/traits"
"github.com/google/cel-go/interpreter/functions"
"github.com/tektoncd/triggers/pkg/interceptors"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -187,6 +188,12 @@ func (t triggersLib) CompileOptions() []cel.EnvOption {
cel.UnaryBinding(marshalJSON)),
cel.MemberOverload("marshalJSON_list", []*cel.Type{listStrDyn}, cel.StringType,
cel.UnaryBinding(marshalJSON))),
cel.Function("last",
cel.MemberOverload("last_list", []*cel.Type{listStrDyn}, cel.DynType,
cel.UnaryBinding(listLast))),
cel.Function("first",
cel.MemberOverload("first_list", []*cel.Type{listStrDyn}, cel.DynType,
cel.UnaryBinding(listFirst))),
}
}

Expand Down Expand Up @@ -324,6 +331,28 @@ func marshalJSON(val ref.Val) ref.Val {
return types.String(marshaledVal)
}

func listLast(val ref.Val) ref.Val {
l := val.(traits.Lister)
sz := l.Size().Value().(int64)

if sz == 0 {
return types.NullValue
}

return l.Get(types.Int(sz - 1))
}

func listFirst(val ref.Val) ref.Val {
l := val.(traits.Lister)
sz := l.Size().Value().(int64)

if sz == 0 {
return types.NullValue
}

return l.Get(types.Int(0))
}

func max(x, y types.Int) types.Int {
switch x.Compare(y) {
case types.IntNegOne:
Expand Down