Skip to content

Commit 83ab641

Browse files
Create an util method to convert rpc status to eval status (#1178)
* Create an util method to convert rpc status to eval status * Formatted the Go files. * Formatted the WORKSPACE file. * Add failing conformance test to SKIP_TESTS. * Added RefValue to ExprValue converter. * Update Module bazel. * Updated go.mod and go.sum. * Removed gRpc status dependencies from io and io_test. * formatted the files. * Followed the convention of returning the v1alpha1 format. * Updated the documentation. * Remove unnecessary dependencies.
1 parent b1209b8 commit 83ab641

File tree

12 files changed

+300
-196
lines changed

12 files changed

+300
-196
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ proto/checked.pb.go
1010
proto/syntax.pb.go
1111
*~
1212
MODULE.bazel.lock
13+
.ijwb/

WORKSPACE

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ http_archive(
4545
urls = ["https://github.com/protocolbuffers/protobuf/archive/v3.21.5.zip"],
4646
)
4747

48-
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
4948
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
5049
load("@com_google_googleapis//:repository_rules.bzl", "switched_rules_by_language")
5150
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
51+
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
5252

5353
switched_rules_by_language(
5454
name = "com_google_googleapis_imports",
@@ -101,8 +101,8 @@ go_repository(
101101
go_repository(
102102
name = "dev_cel_expr",
103103
importpath = "cel.dev/expr",
104-
sum = "h1:K4KOtPCJQjVggkARsjG9RWXP6O4R73aHeJMa/dmCQQg=",
105-
version = "v0.23.1",
104+
sum = "h1:56OvJKSH3hDGL0ml5uSxZmz3/3Pq4tJ+fb1unVLAFcY=",
105+
version = "v0.24.0",
106106
)
107107

108108
# local_repository(
@@ -158,12 +158,15 @@ go_register_toolchains(version = "1.22.0")
158158
gazelle_dependencies()
159159

160160
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")
161+
161162
rules_proto_dependencies()
162163

163164
load("@rules_proto//proto:setup.bzl", "rules_proto_setup")
165+
164166
rules_proto_setup()
165167

166168
load("@rules_proto//proto:toolchains.bzl", "rules_proto_toolchains")
169+
167170
rules_proto_toolchains()
168171

169172
protobuf_deps()

cel/BUILD.bazel

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ go_library(
1111
"decls.go",
1212
"env.go",
1313
"folding.go",
14-
"io.go",
1514
"inlining.go",
15+
"io.go",
1616
"library.go",
1717
"macro.go",
1818
"optimizer.go",
@@ -64,8 +64,8 @@ go_test(
6464
"decls_test.go",
6565
"env_test.go",
6666
"folding_test.go",
67-
"io_test.go",
6867
"inlining_test.go",
68+
"io_test.go",
6969
"optimizer_test.go",
7070
"prompt_test.go",
7171
"validator_test.go",
@@ -90,8 +90,8 @@ go_test(
9090
"//test/proto2pb:go_default_library",
9191
"//test/proto3pb:go_default_library",
9292
"@org_golang_google_genproto_googleapis_api//expr/v1alpha1:go_default_library",
93-
"@org_golang_google_protobuf//proto:go_default_library",
9493
"@org_golang_google_protobuf//encoding/prototext:go_default_library",
94+
"@org_golang_google_protobuf//proto:go_default_library",
9595
"@org_golang_google_protobuf//types/known/structpb:go_default_library",
9696
"@org_golang_google_protobuf//types/known/wrapperspb:go_default_library",
9797
],

cel/io.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,55 @@ func ValueAsAlphaProto(res ref.Val) (*exprpb.Value, error) {
126126
return alpha, err
127127
}
128128

129+
// RefValToExprValue converts between ref.Val and google.api.expr.v1alpha1.ExprValue.
130+
// The result ExprValue is the serialized proto form.
131+
func RefValToExprValue(res ref.Val) (*exprpb.ExprValue, error) {
132+
return ExprValueAsAlphaProto(res)
133+
}
134+
135+
// ExprValueAsAlphaProto converts between ref.Val and google.api.expr.v1alpha1.ExprValue.
136+
// The result ExprValue is the serialized proto form.
137+
func ExprValueAsAlphaProto(res ref.Val) (*exprpb.ExprValue, error) {
138+
canonical, err := ExprValueAsProto(res)
139+
if err != nil {
140+
return nil, err
141+
}
142+
alpha := &exprpb.ExprValue{}
143+
err = convertProto(canonical, alpha)
144+
return alpha, err
145+
}
146+
147+
// ExprValueAsProto converts between ref.Val and cel.expr.ExprValue.
148+
// The result ExprValue is the serialized proto form.
149+
func ExprValueAsProto(res ref.Val) (*celpb.ExprValue, error) {
150+
switch res := res.(type) {
151+
case *types.Unknown:
152+
return &celpb.ExprValue{
153+
Kind: &celpb.ExprValue_Unknown{
154+
Unknown: &celpb.UnknownSet{
155+
Exprs: res.IDs(),
156+
},
157+
}}, nil
158+
case *types.Err:
159+
return &celpb.ExprValue{
160+
Kind: &celpb.ExprValue_Error{
161+
Error: &celpb.ErrorSet{
162+
// Keeping the error code as UNKNOWN since there's no error codes associated with
163+
// Cel-Go runtime errors.
164+
Errors: []*celpb.Status{{Code: 2, Message: res.Error()}},
165+
},
166+
},
167+
}, nil
168+
default:
169+
val, err := ValueAsProto(res)
170+
if err != nil {
171+
return nil, err
172+
}
173+
return &celpb.ExprValue{
174+
Kind: &celpb.ExprValue_Value{Value: val}}, nil
175+
}
176+
}
177+
129178
// ValueAsProto converts between ref.Val and cel.expr.Value.
130179
// The result Value is the serialized proto form. The ref.Val must not be error or unknown.
131180
func ValueAsProto(res ref.Val) (*celpb.Value, error) {

cel/io_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
celast "github.com/google/cel-go/common/ast"
2727
"github.com/google/cel-go/common/operators"
2828
"github.com/google/cel-go/common/types"
29+
"github.com/google/cel-go/common/types/ref"
2930

3031
proto3pb "github.com/google/cel-go/test/proto3pb"
3132
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
@@ -193,6 +194,58 @@ func TestExprToString(t *testing.T) {
193194
}
194195
}
195196

197+
func TestRefValToExprValue(t *testing.T) {
198+
tests := []struct {
199+
name string
200+
refVal ref.Val
201+
expectError bool
202+
}{
203+
{
204+
name: "unknown value",
205+
refVal: types.NewUnknown(1, nil),
206+
expectError: false,
207+
},
208+
{
209+
name: "error value",
210+
refVal: types.NewErr("test error"),
211+
expectError: false,
212+
},
213+
{
214+
name: "bool value",
215+
refVal: types.Bool(true),
216+
expectError: false,
217+
},
218+
{
219+
name: "string value",
220+
refVal: types.String("test"),
221+
expectError: false,
222+
},
223+
{
224+
name: "int value",
225+
refVal: types.Int(1),
226+
expectError: false,
227+
},
228+
}
229+
for _, tst := range tests {
230+
tc := tst
231+
t.Run(tc.name, func(t *testing.T) {
232+
exprVal, err := ExprValueAsProto(tc.refVal)
233+
if tc.expectError {
234+
if err == nil {
235+
t.Errorf("RefValToExprValue(%v) expected error, got %v", tc.refVal, exprVal)
236+
}
237+
} else {
238+
if err != nil {
239+
t.Errorf("RefValToExprValue(%v) failed with error: %v", tc.refVal, err)
240+
}
241+
if exprVal == nil {
242+
t.Errorf("RefValToExprValue(%v) expected value, got nil", tc.refVal)
243+
}
244+
}
245+
})
246+
}
247+
}
248+
196249
func TestAstToStringNil(t *testing.T) {
197250
expr, err := AstToString(nil)
198251
if err == nil || !strings.Contains(err.Error(), "unsupported expr") {

conformance/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ _TESTS_TO_SKIP = [
4848
"fields/qualified_identifier_resolution/map_value_repeat_key_heterogeneous",
4949
"macros/map/map_extract_keys",
5050
"timestamps/duration_converters/get_milliseconds",
51+
"optionals/optionals/map_optional_select_has",
5152

5253
# Temporarily failing tests, need a spec update
5354
"string_ext/value_errors/indexof_out_of_range,lastindexof_out_of_range",
@@ -84,9 +85,9 @@ go_test(
8485
"@com_github_google_go_cmp//cmp:go_default_library",
8586
"@dev_cel_expr//:expr",
8687
"@dev_cel_expr//conformance:go_default_library",
87-
"@dev_cel_expr//conformance/test:go_default_library",
8888
"@dev_cel_expr//conformance/proto2:go_default_library",
8989
"@dev_cel_expr//conformance/proto3:go_default_library",
90+
"@dev_cel_expr//conformance/test:go_default_library",
9091
"@io_bazel_rules_go//go/runfiles",
9192
"@org_golang_google_genproto_googleapis_api//expr/v1alpha1:go_default_library",
9293
"@org_golang_google_protobuf//encoding/prototext:go_default_library",

conformance/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/google/cel-go/conformance
33
go 1.22.0
44

55
require (
6-
cel.dev/expr v0.23.1
6+
cel.dev/expr v0.24.0
77
github.com/bazelbuild/rules_go v0.49.0
88
github.com/google/cel-go v0.21.0
99
github.com/google/go-cmp v0.6.0

conformance/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
cel.dev/expr v0.23.1 h1:K4KOtPCJQjVggkARsjG9RWXP6O4R73aHeJMa/dmCQQg=
2-
cel.dev/expr v0.23.1/go.mod h1:hLPLo1W4QUmuYdA72RBX06QTs6MXw941piREPl3Yfiw=
1+
cel.dev/expr v0.24.0 h1:56OvJKSH3hDGL0ml5uSxZmz3/3Pq4tJ+fb1unVLAFcY=
2+
cel.dev/expr v0.24.0/go.mod h1:hLPLo1W4QUmuYdA72RBX06QTs6MXw941piREPl3Yfiw=
33
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
44
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
55
github.com/bazelbuild/rules_go v0.49.0 h1:5vCbuvy8Q11g41lseGJDc5vxhDjJtfxr6nM/IC4VmqM=

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.22.0
55
toolchain go1.23.0
66

77
require (
8-
cel.dev/expr v0.23.1
8+
cel.dev/expr v0.24.0
99
github.com/antlr4-go/antlr/v4 v4.13.0
1010
github.com/stoewer/go-strcase v1.2.0
1111
google.golang.org/genproto/googleapis/api v0.0.0-20240826202546-f6391c0de4c7

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
cel.dev/expr v0.23.1 h1:K4KOtPCJQjVggkARsjG9RWXP6O4R73aHeJMa/dmCQQg=
2-
cel.dev/expr v0.23.1/go.mod h1:hLPLo1W4QUmuYdA72RBX06QTs6MXw941piREPl3Yfiw=
1+
cel.dev/expr v0.24.0 h1:56OvJKSH3hDGL0ml5uSxZmz3/3Pq4tJ+fb1unVLAFcY=
2+
cel.dev/expr v0.24.0/go.mod h1:hLPLo1W4QUmuYdA72RBX06QTs6MXw941piREPl3Yfiw=
33
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
44
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
55
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=

0 commit comments

Comments
 (0)