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 .elideOpt() function #1103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions cel/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2818,6 +2818,10 @@ func TestOptionalValuesEval(t *testing.T) {
{expr: `['a','b','c'].first()`, out: types.OptionalOf(types.String("a"))},
{expr: `[].last()`, out: types.OptionalNone},
{expr: `[1, 2, 3].last()`, out: types.OptionalOf(types.Int(3))},
{expr: `[].elideOpt()`, out: []any{}},
{expr: `[optional.none(), optional.none()].elideOpt()`, out: []any{}},
{expr: `[optional.of(42), optional.none(), optional.of("a")].elideOpt()`, out: []any{types.Int(42), types.String("a")}},
{expr: `[optional.of(42), optional.of("a")].elideOpt()`, out: []any{types.Int(42), types.String("a")}},
}

for i, tst := range tests {
Expand Down
30 changes: 30 additions & 0 deletions cel/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package cel

import (
"fmt"
"math"
"strconv"
"strings"
Expand All @@ -35,6 +36,7 @@ const (
optMapMacro = "optMap"
optFlatMapMacro = "optFlatMap"
hasValueFunc = "hasValue"
elideOptFunc = "elideOpt"
optionalNoneFunc = "optional.none"
optionalOfFunc = "optional.of"
optionalOfNonZeroValueFunc = "optional.ofNonZeroValue"
Expand Down Expand Up @@ -281,6 +283,14 @@ func (stdLibrary) ProgramOptions() []ProgramOption {
//
// This is syntactic sugar for msg.elements[msg.elements.size()-1].

// # ElideOpt
//
// Introduced in version: 2
//
// Returns a list of all the values that are not none in the input list of optional values.
//
// [optional.of(42), optional.none()].elideOpt() == [42]

func OptionalTypes(opts ...OptionalTypesOption) EnvOption {
lib := &optionalLib{version: math.MaxUint32}
for _, opt := range opts {
Expand Down Expand Up @@ -324,6 +334,7 @@ func (lib *optionalLib) CompileOptions() []EnvOption {
optionalTypeV := OptionalType(paramTypeV)
listTypeV := ListType(paramTypeV)
mapTypeKV := MapType(paramTypeK, paramTypeV)
listOptionalTypeV := ListType(optionalTypeV)

opts := []EnvOption{
// Enable the optional syntax in the parser.
Expand Down Expand Up @@ -427,6 +438,25 @@ func (lib *optionalLib) CompileOptions() []EnvOption {
}),
),
))

opts = append(opts, Function(elideOptFunc,
MemberOverload("optional_elideOpt", []*Type{listOptionalTypeV}, listTypeV,
UnaryBinding(func(value ref.Val) ref.Val {
list := value.(traits.Lister)
var elidedList []ref.Val
iter := list.Iterator()
for iter.HasNext() == types.True {
val := iter.Next()
opt, isOpt := val.(*types.Optional)
if !isOpt {
return types.WrapErr(fmt.Errorf("value %v is not optional", val))
}
if opt.HasValue() {
elidedList = append(elidedList, opt.GetValue())
}
}
return types.DefaultTypeAdapter.NativeToValue(elidedList)
}))))
}

return opts
Expand Down