Skip to content

Commit

Permalink
Implement parts of cel.@block in C++ evaluator
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 664168663
  • Loading branch information
jcking authored and copybara-github committed Aug 17, 2024
1 parent ba08c55 commit 54e46c5
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 10 deletions.
4 changes: 2 additions & 2 deletions bazel/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ def cel_spec_deps():
url = "https://github.com/bazelbuild/rules_python/releases/download/0.33.2/rules_python-0.33.2.tar.gz",
)

CEL_SPEC_GIT_SHA = "5299974f1c69103e4bb4eec48f7d9b24413ca3c7" # Jul 19, 2024
CEL_SPEC_GIT_SHA = "5ed294fa64206016a37db2986dab942c80a65e4b" # Aug 16, 2024
http_archive(
name = "com_google_cel_spec",
sha256 = "2beb97d2d8fff4db659f0633d0e432fdb7d328fe9c39061eb142af5dbb6eaea0",
sha256 = "926abf84cde8c05ce99700caee5786bc7d8aeec77185fd669bce27df455a1215",
strip_prefix = "cel-spec-" + CEL_SPEC_GIT_SHA,
urls = ["https://github.com/google/cel-spec/archive/" + CEL_SPEC_GIT_SHA + ".zip"],
)
Expand Down
24 changes: 24 additions & 0 deletions conformance/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ cc_library(
hdrs = ["service.h"],
deps = [
":value_conversion",
"//common:expr",
"//common:memory",
"//common:source",
"//common:value",
Expand All @@ -67,6 +68,8 @@ cc_library(
"//extensions/protobuf:value",
"//internal:status_macros",
"//parser",
"//parser:macro",
"//parser:macro_expr_factory",
"//parser:macro_registry",
"//parser:options",
"//parser:standard_macros",
Expand All @@ -82,6 +85,8 @@ cc_library(
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:optional",
"@com_google_absl//absl/types:span",
"@com_google_cel_spec//proto/test/v1/proto2:test_all_types_cc_proto",
"@com_google_cel_spec//proto/test/v1/proto3:test_all_types_cc_proto",
"@com_google_googleapis//google/api/expr/conformance/v1alpha1:conformance_cc_proto",
Expand Down Expand Up @@ -141,6 +146,7 @@ _ALL_TESTS = [
"@com_google_cel_spec//tests/simple:testdata/timestamps.textproto",
"@com_google_cel_spec//tests/simple:testdata/unknowns.textproto",
"@com_google_cel_spec//tests/simple:testdata/wrappers.textproto",
"@com_google_cel_spec//tests/simple:testdata/block_ext.textproto",
]

_TESTS_TO_SKIP_MODERN = [
Expand Down Expand Up @@ -209,6 +215,13 @@ _TESTS_TO_SKIP_MODERN = [

# TODO: Add missing conversion function
"conversions/bool",

# cel.@block
"block_ext/basic/multiple_macros_1",
"block_ext/basic/multiple_macros_2",
"block_ext/basic/multiple_macros_3",
"block_ext/basic/nested_macros",
"block_ext/basic/nested_macros_2",
]

_TESTS_TO_SKIP_MODERN_DASHBOARD = [
Expand Down Expand Up @@ -276,6 +289,17 @@ _TESTS_TO_SKIP_LEGACY = [

# TODO: Add missing conversion function
"conversions/bool",

# cel.@block
"block_ext/basic/optional_list",
"block_ext/basic/optional_map",
"block_ext/basic/optional_map_chained",
"block_ext/basic/optional_message",
"block_ext/basic/multiple_macros_1",
"block_ext/basic/multiple_macros_2",
"block_ext/basic/multiple_macros_3",
"block_ext/basic/nested_macros",
"block_ext/basic/nested_macros_2",
]

_TESTS_TO_SKIP_LEGACY_DASHBOARD = [
Expand Down
105 changes: 105 additions & 0 deletions conformance/service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "conformance/service.h"

#include <cstdint>
#include <iostream>
#include <memory>
#include <string>
Expand All @@ -33,7 +34,11 @@
#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "absl/types/span.h"
#include "common/expr.h"
#include "common/memory.h"
#include "common/source.h"
#include "common/value.h"
Expand All @@ -56,6 +61,8 @@
#include "extensions/protobuf/type_reflector.h"
#include "extensions/strings.h"
#include "internal/status_macros.h"
#include "parser/macro.h"
#include "parser/macro_expr_factory.h"
#include "parser/macro_registry.h"
#include "parser/options.h"
#include "parser/parser.h"
Expand Down Expand Up @@ -90,6 +97,103 @@ namespace google::api::expr::runtime {

namespace {

bool IsCelNamespace(const cel::Expr& target) {
return target.has_ident_expr() && target.ident_expr().name() == "cel";
}

absl::optional<cel::Expr> CelBlockMacroExpander(cel::MacroExprFactory& factory,
cel::Expr& target,
absl::Span<cel::Expr> args) {
if (!IsCelNamespace(target)) {
return absl::nullopt;
}
cel::Expr& bindings_arg = args[0];
if (!bindings_arg.has_list_expr()) {
return factory.ReportErrorAt(
bindings_arg, "cel.block requires the first arg to be a list literal");
}
return factory.NewCall("cel.@block", args);
}

absl::optional<cel::Expr> CelIndexMacroExpander(cel::MacroExprFactory& factory,
cel::Expr& target,
absl::Span<cel::Expr> args) {
if (!IsCelNamespace(target)) {
return absl::nullopt;
}
cel::Expr& index_arg = args[0];
if (!index_arg.has_const_expr() || !index_arg.const_expr().has_int_value()) {
return factory.ReportErrorAt(
index_arg, "cel.index requires a single non-negative int constant arg");
}
int64_t index = index_arg.const_expr().int_value();
if (index < 0) {
return factory.ReportErrorAt(
index_arg, "cel.index requires a single non-negative int constant arg");
}
return factory.NewIdent(absl::StrCat("@index", index));
}

absl::optional<cel::Expr> CelIterVarMacroExpander(
cel::MacroExprFactory& factory, cel::Expr& target,
absl::Span<cel::Expr> args) {
if (!IsCelNamespace(target)) {
return absl::nullopt;
}
cel::Expr& index_arg = args[0];
if (!index_arg.has_const_expr() || !index_arg.const_expr().has_int_value()) {
return factory.ReportErrorAt(
index_arg,
"cel.iterVar requires a single non-negative int constant arg");
}
int64_t index = index_arg.const_expr().int_value();
if (index < 0) {
return factory.ReportErrorAt(
index_arg,
"cel.iterVar requires a single non-negative int constant arg");
}
return factory.NewIdent(absl::StrCat("@c:", index));
}

absl::optional<cel::Expr> CelAccuVarMacroExpander(
cel::MacroExprFactory& factory, cel::Expr& target,
absl::Span<cel::Expr> args) {
if (!IsCelNamespace(target)) {
return absl::nullopt;
}
cel::Expr& index_arg = args[0];
if (!index_arg.has_const_expr() || !index_arg.const_expr().has_int_value()) {
return factory.ReportErrorAt(
index_arg,
"cel.accuVar requires a single non-negative int constant arg");
}
int64_t index = index_arg.const_expr().int_value();
if (index < 0) {
return factory.ReportErrorAt(
index_arg,
"cel.accuVar requires a single non-negative int constant arg");
}
return factory.NewIdent(absl::StrCat("@x:", index));
}

absl::Status RegisterCelBlockMacros(cel::MacroRegistry& registry) {
CEL_ASSIGN_OR_RETURN(auto block_macro,
cel::Macro::Receiver("block", 2, CelBlockMacroExpander));
CEL_RETURN_IF_ERROR(registry.RegisterMacro(block_macro));
CEL_ASSIGN_OR_RETURN(auto index_macro,
cel::Macro::Receiver("index", 1, CelIndexMacroExpander));
CEL_RETURN_IF_ERROR(registry.RegisterMacro(index_macro));
CEL_ASSIGN_OR_RETURN(
auto iter_var_macro,
cel::Macro::Receiver("iterVar", 1, CelIterVarMacroExpander));
CEL_RETURN_IF_ERROR(registry.RegisterMacro(iter_var_macro));
CEL_ASSIGN_OR_RETURN(
auto accu_var_macro,
cel::Macro::Receiver("accuVar", 1, CelAccuVarMacroExpander));
CEL_RETURN_IF_ERROR(registry.RegisterMacro(accu_var_macro));
return absl::OkStatus();
}

google::rpc::Code ToGrpcCode(absl::StatusCode code) {
return static_cast<google::rpc::Code>(code);
}
Expand Down Expand Up @@ -126,6 +230,7 @@ absl::Status LegacyParse(const conformance::v1alpha1::ParseRequest& request,
CEL_RETURN_IF_ERROR(cel::extensions::RegisterBindingsMacros(macros, options));
CEL_RETURN_IF_ERROR(cel::extensions::RegisterMathMacros(macros, options));
CEL_RETURN_IF_ERROR(cel::extensions::RegisterProtoMacros(macros, options));
CEL_RETURN_IF_ERROR(RegisterCelBlockMacros(macros));
CEL_ASSIGN_OR_RETURN(auto source, cel::NewSource(request.cel_source(),
request.source_location()));
CEL_ASSIGN_OR_RETURN(auto parsed_expr,
Expand Down
1 change: 1 addition & 0 deletions eval/compiler/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ cc_library(
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/container:node_hash_map",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
Expand Down
Loading

0 comments on commit 54e46c5

Please sign in to comment.