Skip to content
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
18 changes: 18 additions & 0 deletions cel/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ func (l library) CompileOptions() []cel.EnvOption { //nolint:funlen,gocyclo
l.uniqueMemberOverload(cel.StringType, l.uniqueScalar),
l.uniqueMemberOverload(cel.BytesType, l.uniqueBytes),
),
cel.Function("getField",
cel.Overload(
"get_field_any_string",
[]*cel.Type{cel.AnyType, cel.StringType},
cel.AnyType,
cel.FunctionBinding(func(values ...ref.Val) ref.Val {
message, ok := values[0].(traits.Indexer)
if !ok {
return types.UnsupportedRefValConversionErr(values[0])
}
fieldName, ok := values[1].Value().(string)
if !ok {
return types.UnsupportedRefValConversionErr(values[1])
}
return message.Get(types.String(fieldName))
}),
),
),
cel.Function("isNan",
cel.MemberOverload(
"double_is_nan_bool",
Expand Down
49 changes: 42 additions & 7 deletions cel/library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ package cel
import (
"testing"

"github.com/bufbuild/protovalidate-go/internal/gen/buf/validate/conformance/cases"
"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/interpreter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -26,15 +29,31 @@ import (
func TestCELLib(t *testing.T) {
t.Parallel()

env, err := cel.NewEnv(cel.Lib(NewLibrary()))
testValue := cases.StringConst_builder{Val: "test_string"}.Build()

activation, err := interpreter.NewActivation(map[string]any{
"test": testValue,
})
require.NoError(t, err)

env, err := cel.NewEnv(
cel.Lib(NewLibrary()),
cel.Variable(
"test",
cel.ObjectType(
string(testValue.ProtoReflect().Descriptor().FullName()),
),
),
)

require.NoError(t, err)

t.Run("ext", func(t *testing.T) {
t.Parallel()

tests := []struct {
expr string
ex bool
ex any
}{
{"0.0.isInf()", false},
{"0.0.isNan()", false},
Expand Down Expand Up @@ -197,18 +216,34 @@ func TestCELLib(t *testing.T) {
"'foo@example.com '.isEmail()",
false,
},
{
"getField(test, 'val')",
"test_string",
},
{
"getField(test, 'lav')",
types.NewErrFromString("no such field"),
},
{
"getField(0, 'val')",
types.NewErrFromString("unsupported conversion"),
},
}

for _, tc := range tests {
test := tc
t.Run(test.expr, func(t *testing.T) {
t.Parallel()
prog := buildTestProgram(t, env, test.expr)
val, _, err := prog.Eval(interpreter.EmptyActivation())
require.NoError(t, err)
isUnique, ok := val.Value().(bool)
require.True(t, ok)
assert.Equal(t, test.ex, isUnique)
val, _, err := prog.Eval(activation)
if refEx, ok := test.ex.(ref.Val); ok && types.IsError(refEx) {
refErr, ok := refEx.Value().(error)
require.True(t, ok)
assert.ErrorContains(t, err, refErr.Error())
} else {
require.NoError(t, err)
assert.Equal(t, test.ex, val.Value())
}
})
}
})
Expand Down