Skip to content

support string evaluation in CEL expressions #882

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

Merged
merged 1 commit into from
Mar 11, 2025
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
13 changes: 13 additions & 0 deletions runtime/cel/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,16 @@ func (e *Expression) EvaluateBoolean(ctx context.Context, data map[string]any) (
}
return bool(result), nil
}

// EvaluateString evaluates the expression with the given data and returns the result as a string.
func (e *Expression) EvaluateString(ctx context.Context, data map[string]any) (string, error) {
val, _, err := e.prog.ContextEval(ctx, data)
if err != nil {
return "", fmt.Errorf("failed to evaluate the CEL expression '%s': %w", e.expr, err)
}
result, ok := val.(types.String)
if !ok {
return "", fmt.Errorf("failed to evaluate CEL expression as string: '%s'", e.expr)
}
return string(result), nil
}
140 changes: 140 additions & 0 deletions runtime/cel/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,143 @@ func TestExpression_EvaluateBoolean(t *testing.T) {
})
}
}

func TestExpression_EvaluateString(t *testing.T) {
for _, tt := range []struct {
name string
expr string
opts []cel.Option
data map[string]any
result string
err string
}{
{
name: "non-existent field",
expr: "foo",
data: map[string]any{},
err: "failed to evaluate the CEL expression 'foo': no such attribute(s): foo",
},
{
name: "string field",
expr: "foo",
data: map[string]any{"foo": "some-value"},
result: "some-value",
},
{
name: "non-string field",
expr: "foo",
data: map[string]any{"foo": 123},
err: "failed to evaluate CEL expression as string: 'foo'",
},
{
name: "nested string field",
expr: "foo.bar",
data: map[string]any{"foo": map[string]any{"bar": "some-value"}},
result: "some-value",
},
{
name: "compiled expression returning string",
expr: "foo.bar",
opts: []cel.Option{cel.WithCompile(), cel.WithStructVariables("foo")},
data: map[string]any{"foo": map[string]any{"bar": "some-value"}},
result: "some-value",
},
{
name: "compiled expression returning string multiple variables",
expr: "foo.bar + '/' + foo.baz + '/' + bar.biz",
opts: []cel.Option{
cel.WithCompile(),
cel.WithStructVariables("foo", "bar"),
},
data: map[string]any{
"foo": map[string]any{
"bar": "some-value",
"baz": "some-other-value"},
"bar": map[string]any{
"biz": "some-third-value",
},
},
result: "some-value/some-other-value/some-third-value",
},
{
name: "compiled expression with string manipulation and zero index",
expr: "foo.bar + '/' + foo.baz + '/' + bar.uid.split('-')[0].lowerAscii()",
opts: []cel.Option{
cel.WithCompile(),
cel.WithStructVariables("foo", "bar"),
},
data: map[string]any{
"foo": map[string]any{
"bar": "some-value",
"baz": "some-other-value"},
"bar": map[string]any{
"uid": "AKS2J23-DAFLSDD-123J5LS",
},
},
result: "some-value/some-other-value/aks2j23",
},
{
name: "compiled expression with string manipulation and first",
expr: "foo.bar + '/' + foo.baz + '/' + bar.uid.split('-').first().value().lowerAscii()",
opts: []cel.Option{
cel.WithCompile(),
cel.WithStructVariables("foo", "bar"),
},
data: map[string]any{
"foo": map[string]any{
"bar": "some-value",
"baz": "some-other-value"},
"bar": map[string]any{
"uid": "AKS2J23-DAFLSDD-123J5LS",
},
},
result: "some-value/some-other-value/aks2j23",
},
{
name: "compiled expression with first",
expr: "foo.bar.split('-').first().value()",
opts: []cel.Option{cel.WithCompile(), cel.WithStructVariables("foo")},
data: map[string]any{
"foo": map[string]any{"bar": "hello-world-testing-123"},
},
result: "hello",
},
{
name: "compiled expression with last",
expr: "foo.bar.split('-').last().value()",
opts: []cel.Option{cel.WithCompile(), cel.WithStructVariables("foo")},
data: map[string]any{
"foo": map[string]any{"bar": "hello-world-testing-123"},
},
result: "123",
},
{
name: "error without value method",
expr: "foo.bar.split('-').first()",
opts: []cel.Option{cel.WithCompile(), cel.WithStructVariables("foo")},
data: map[string]any{
"foo": map[string]any{"bar": "hello-world-testing-123"},
},
err: "failed to evaluate CEL expression as string: 'foo.bar.split('-').first()'",
},
} {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

g := NewWithT(t)

e, err := cel.NewExpression(tt.expr, tt.opts...)
g.Expect(err).NotTo(HaveOccurred())

result, err := e.EvaluateString(context.Background(), tt.data)

if tt.err != "" {
g.Expect(err).To(HaveOccurred())
g.Expect(err.Error()).To(ContainSubstring(tt.err))
} else {
g.Expect(err).NotTo(HaveOccurred())
g.Expect(result).To(Equal(tt.result))
}
})
}
}
Loading