Skip to content

Commit

Permalink
Upgrade cel-go to support bazel-mod (#1049)
Browse files Browse the repository at this point in the history
* Support for cel-spec v0.18.0
* Minor update to the WORKSPACE
* Update go version in Cloud Build
  • Loading branch information
TristonianJones authored Oct 28, 2024
1 parent 7c13168 commit 3338c3f
Show file tree
Hide file tree
Showing 48 changed files with 5,524 additions and 214 deletions.
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.4.0
7.0.1
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ bazel-testlogs
proto/checked.pb.go
proto/syntax.pb.go
*~
MODULE.bazel.lock
89 changes: 89 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
module(
name = "cel-go",
)

bazel_dep(
name = "bazel_skylib",
version = "1.7.1",
)
bazel_dep(
name = "gazelle",
version = "0.39.1",
repo_name = "bazel_gazelle",
)
bazel_dep(
name = "googleapis",
version = "0.0.0-20240819-fe8ba054a",
repo_name = "com_google_googleapis",
)
bazel_dep(
name = "protobuf",
version = "26.0",
repo_name = "com_google_protobuf",
)
bazel_dep(
name = "rules_go",
version = "0.50.1",
repo_name = "io_bazel_rules_go",
)
bazel_dep(
name = "rules_proto",
version = "6.0.0",
)

switched_rules = use_extension("@com_google_googleapis//:extensions.bzl", "switched_rules")
switched_rules.use_languages(
go = True,
)
use_repo(switched_rules, "com_google_googleapis_imports")

go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.21.1")

go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps")
go_deps.gazelle_default_attributes(
directives = [
"gazelle:proto disable_global",
],
)
go_deps.gazelle_override(
# Force Gazelle to wipe out the existing build files before regenerate them.
build_file_generation = "on",
directives = [
"gazelle:go_generate_proto false",
# Provide hints to gazelle about how includes and imports map to build targets
"gazelle:resolve go cel.dev/expr @dev_cel_expr//:expr",
"gazelle:resolve proto go google/rpc/status.proto @org_golang_google_genproto_googleapis_rpc//status",
"gazelle:resolve proto proto google/rpc/status.proto @googleapis//google/rpc:status_proto",
],
path = "cel.dev/expr",
)
go_deps.from_file(go_mod = "//:go.mod")
go_deps.module(
path = "gopkg.in/yaml.v3",
sum = "h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=",
version = "v3.0.1",
)
go_deps.module(
path = "github.com/chzyer/readline",
sum = "h1:upd/6fQk4src78LMRzh5vItIt361/o4uq553V8B5sGI=",
version = "v1.5.1",
)
go_deps.module(
path = "github.com/google/go-cmp",
sum = "h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=",
version = "v0.5.9",
)
use_repo(
go_deps,
"com_github_antlr4_go_antlr_v4",
"com_github_chzyer_readline",
"com_github_google_go_cmp",
"com_github_stoewer_go_strcase",
"dev_cel_expr",
"in_gopkg_yaml_v3",
"org_golang_google_genproto_googleapis_api",
"org_golang_google_genproto_googleapis_rpc",
"org_golang_google_protobuf",
"org_golang_x_text",
)
28 changes: 7 additions & 21 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,6 @@ go_repository(
version = "v0.0.0-20240823204242-4ba0660f739c",
)

# gRPC deps for v1.49.0 (including x/text and x/net)
go_repository(
name = "org_golang_google_grpc",
build_file_proto_mode = "disable_global",
importpath = "google.golang.org/grpc",
tag = "v1.49.0",
)

go_repository(
name = "org_golang_x_net",
importpath = "golang.org/x/net",
sum = "h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=",
version = "v0.0.0-20190311183353-d8887717615a",
)

go_repository(
name = "org_golang_x_text",
importpath = "golang.org/x/text",
Expand All @@ -112,16 +97,17 @@ go_repository(
version = "v4.13.0",
)

# CEL Spec deps (v0.16.1)
# CEL Spec deps
go_repository(
name = "com_google_cel_spec",
commit = "aa4eb92b7d469b32ff1a767ef4ef340b2d05a5d0",
name = "dev_cel_expr",
importpath = "cel.dev/expr",
sum = "h1:CJ6drgk+Hf96lkLikr4rFf19WrU0BOWEihyZnI2TAzo=",
version = "v0.18.0",
)

# local_repository(
# name = "com_google_cel_spec",
# path = "<abs/path>/github.com/google/cel-spec",
# name = "dev_cel_expr",
# path = "</abs/path>/github.com/google/cel-spec",
# )

# strcase deps
Expand Down Expand Up @@ -167,7 +153,7 @@ go_repository(
# of the above repositories but at different versions, so ours must come first.
go_rules_dependencies()

go_register_toolchains(version = "1.21.0")
go_register_toolchains(version = "1.21.1")

gazelle_dependencies()

Expand Down
Empty file added WORKSPACE.bzlmod
Empty file.
2 changes: 1 addition & 1 deletion cel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ go_library(
"//common/types/traits:go_default_library",
"//interpreter:go_default_library",
"//parser:go_default_library",
"@dev_cel_expr//:expr",
"@org_golang_google_genproto_googleapis_api//expr/v1alpha1:go_default_library",
"@org_golang_google_protobuf//proto:go_default_library",
"@org_golang_google_protobuf//reflect/protodesc:go_default_library",
Expand Down Expand Up @@ -81,7 +82,6 @@ go_test(
"//test:go_default_library",
"//test/proto2pb:go_default_library",
"//test/proto3pb:go_default_library",
"@io_bazel_rules_go//proto/wkt:descriptor_go_proto",
"@org_golang_google_genproto_googleapis_api//expr/v1alpha1:go_default_library",
"@org_golang_google_protobuf//proto:go_default_library",
"@org_golang_google_protobuf//encoding/prototext:go_default_library",
Expand Down
27 changes: 21 additions & 6 deletions cel/decls.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"

celpb "cel.dev/expr"
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
)

Expand Down Expand Up @@ -312,20 +313,34 @@ func ExprTypeToType(t *exprpb.Type) (*Type, error) {

// ExprDeclToDeclaration converts a protobuf CEL declaration to a CEL-native declaration, either a Variable or Function.
func ExprDeclToDeclaration(d *exprpb.Decl) (EnvOption, error) {
return AlphaProtoAsDeclaration(d)
}

// AlphaProtoAsDeclaration converts a v1alpha1.Decl value describing a variable or function into an EnvOption.
func AlphaProtoAsDeclaration(d *exprpb.Decl) (EnvOption, error) {
canonical := &celpb.Decl{}
if err := convertProto(d, canonical); err != nil {
return nil, err
}
return ProtoAsDeclaration(canonical)
}

// ProtoAsDeclaration converts a canonical celpb.Decl value describing a variable or function into an EnvOption.
func ProtoAsDeclaration(d *celpb.Decl) (EnvOption, error) {
switch d.GetDeclKind().(type) {
case *exprpb.Decl_Function:
case *celpb.Decl_Function:
overloads := d.GetFunction().GetOverloads()
opts := make([]FunctionOpt, len(overloads))
for i, o := range overloads {
args := make([]*Type, len(o.GetParams()))
for j, p := range o.GetParams() {
a, err := types.ExprTypeToType(p)
a, err := types.ProtoAsType(p)
if err != nil {
return nil, err
}
args[j] = a
}
res, err := types.ExprTypeToType(o.GetResultType())
res, err := types.ProtoAsType(o.GetResultType())
if err != nil {
return nil, err
}
Expand All @@ -336,15 +351,15 @@ func ExprDeclToDeclaration(d *exprpb.Decl) (EnvOption, error) {
}
}
return Function(d.GetName(), opts...), nil
case *exprpb.Decl_Ident:
t, err := types.ExprTypeToType(d.GetIdent().GetType())
case *celpb.Decl_Ident:
t, err := types.ProtoAsType(d.GetIdent().GetType())
if err != nil {
return nil, err
}
if d.GetIdent().GetValue() == nil {
return Variable(d.GetName(), t), nil
}
val, err := ast.ConstantToVal(d.GetIdent().GetValue())
val, err := ast.ProtoConstantAsVal(d.GetIdent().GetValue())
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 3338c3f

Please sign in to comment.