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

feat: add .first() & .last() methods to list in cel #64

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions funcs/cel_exports.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions funcs/coll_cel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package funcs

import (
"reflect"

"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
)

var collListFirst = cel.Function("first",
cel.MemberOverload("slice_first_any",
[]*cel.Type{
cel.ListType(cel.DynType),
},
cel.DynType,
cel.FunctionBinding(func(args ...ref.Val) ref.Val {
list, err := args[0].ConvertToNative(reflect.TypeOf([]any{}))
if err != nil {
return types.WrapErr(err)
}

l, ok := list.([]any)
if !ok {
return types.WrapErr(err)
}

if len(l) == 0 {
return types.DefaultTypeAdapter.NativeToValue("")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be the nil value for the type

}

return types.DefaultTypeAdapter.NativeToValue(l[0])
}),
),
)

var collListLast = cel.Function("last",
cel.MemberOverload("slice_last_any",
[]*cel.Type{
cel.ListType(cel.DynType),
},
cel.DynType,
cel.FunctionBinding(func(args ...ref.Val) ref.Val {
list, err := args[0].ConvertToNative(reflect.TypeOf([]any{}))
if err != nil {
return types.WrapErr(err)
}

l, ok := list.([]any)
if !ok {
return types.WrapErr(err)
}

if len(l) == 0 {
return types.DefaultTypeAdapter.NativeToValue("")
}

return types.DefaultTypeAdapter.NativeToValue(l[len(l)-1])
}),
),
)
18 changes: 18 additions & 0 deletions tests/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ func unstructure(o any) interface{} {
return out
}

func TestCelCollection(t *testing.T) {
runTests(t, []Test{
{nil, "[1,2,3].first()", "1"},
{nil, "['a', 'b', 'c'].first()", "a"},
{nil, "[].first()", ""},
{map[string]any{"my_list": []int{5, 6, 7}}, "my_list.first()", "5"},
{map[string]any{"my_list": []Person{{Name: "John"}, {Name: "Bob"}}}, "my_list.first().name", "John"},
})

runTests(t, []Test{
{nil, "[1,2,3].last()", "3"},
{nil, "['a', 'b', 'c'].last()", "c"},
{nil, "[].last()", ""},
{map[string]any{"my_list": []int{5, 6, 7}}, "my_list.last()", "7"},
{map[string]any{"my_list": []Person{{Name: "John"}, {Name: "Bob"}}}, "my_list.last().name", "Bob"},
})
}

func TestCelAws(t *testing.T) {
runTests(t, []Test{
{nil, "aws.arnToMap('arn:aws:sns:eu-west-1:123:MMS-Topic').account", "123"},
Expand Down
Loading