Skip to content

Commit

Permalink
Add strings extension
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <[email protected]>
  • Loading branch information
alexsnaps committed Oct 29, 2024
1 parent 44052a6 commit fd1d837
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/expressions/cel/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/google/cel-go/cel"
"github.com/google/cel-go/checker/decls"
"github.com/google/cel-go/common/types/ref"
"github.com/google/cel-go/ext"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/structpb"
Expand Down Expand Up @@ -130,6 +131,7 @@ func Compile(expression string, expectedType *cel.Type, opts ...cel.EnvOption) (
decls.NewConst(RootDestinationBinding, decls.NewObjectType("google.protobuf.Struct"), nil),
decls.NewConst(RootAuthBinding, decls.NewObjectType("google.protobuf.Struct"), nil),
)}, opts...)
envOpts = append(envOpts, ext.Strings())
env, env_err := cel.NewEnv(envOpts...)
if env_err != nil {
return nil, env_err
Expand Down
11 changes: 11 additions & 0 deletions pkg/expressions/cel/expressions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,14 @@ func TestPredicate(t *testing.T) {
assert.NilError(t, err)
assert.Equal(t, response, true)
}

func TestExpression(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

expression, err := NewStringExpression(`"hello hello".replace("", "_")`)
assert.NilError(t, err)
resolveFor, err := expression.ResolveFor("{}")
assert.NilError(t, err)
assert.Equal(t, resolveFor, "_h_e_l_l_o_ _h_e_l_l_o_")
}

0 comments on commit fd1d837

Please sign in to comment.