Skip to content

Commit

Permalink
Add strings.format CEL extension implementation to C++.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 710751468
  • Loading branch information
CEL Dev Team authored and copybara-github committed Jan 22, 2025
1 parent 9a8b79d commit 3321bc0
Show file tree
Hide file tree
Showing 10 changed files with 1,631 additions and 7 deletions.
22 changes: 16 additions & 6 deletions bazel/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ def cel_spec_deps():
urls = ["https://github.com/google/cel-spec/archive/" + CEL_SPEC_GIT_SHA + ".zip"],
)

_ICU4C_VERSION_MAJOR = "76"
_ICU4C_VERSION_MINOR = "1"
_ICU4C_BUILD = """
load("@rules_foreign_cc//foreign_cc:configure.bzl", "configure_make")
Expand All @@ -161,9 +163,9 @@ filegroup(
config_setting(
name = "dbg",
values = {
values = {{
"compilation_mode": "dbg",
},
}},
visibility = ["//visibility:private"],
)
Expand All @@ -178,16 +180,24 @@ configure_make(
"--disable-icuio",
"--disable-layoutex",
"--disable-icu-config",
] + select({
] + select({{
":dbg": ["--enable-debug"],
"//conditions:default": [],
}),
}}),
lib_source = ":all",
out_shared_libs = [
"libicudata.so",
"libicudata.so.{version_major}",
"libicudata.so.{version_major}.{version_minor}",
"libicui18n.so",
"libicui18n.so.{version_major}",
"libicui18n.so.{version_major}.{version_minor}",
"libicutu.so",
"libicutu.so.{version_major}",
"libicutu.so.{version_major}.{version_minor}",
"libicuuc.so",
"libicuuc.so.{version_major}",
"libicuuc.so.{version_major}.{version_minor}",
],
out_static_libs = [
"libicudata.a",
Expand All @@ -198,7 +208,7 @@ configure_make(
args = ["-j 8"],
visibility = ["//visibility:public"],
)
"""
""".format(version_major = _ICU4C_VERSION_MAJOR, version_minor = _ICU4C_VERSION_MINOR)

def cel_cpp_extensions_deps():
http_archive(
Expand All @@ -210,7 +220,7 @@ def cel_cpp_extensions_deps():
http_archive(
name = "icu4c",
sha256 = "dfacb46bfe4747410472ce3e1144bf28a102feeaa4e3875bac9b4c6cf30f4f3e",
url = "https://github.com/unicode-org/icu/releases/download/release-76-1/icu4c-76_1-src.tgz",
url = "https://github.com/unicode-org/icu/releases/download/release-{version_major}-{version_minor}/icu4c-{version_major}_{version_minor}-src.tgz".format(version_major = _ICU4C_VERSION_MAJOR, version_minor = _ICU4C_VERSION_MINOR),
strip_prefix = "icu",
patch_cmds = [
"rm -f source/common/BUILD.bazel",
Expand Down
3 changes: 2 additions & 1 deletion eval/public/cel_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ cel::RuntimeOptions ConvertToRuntimeOptions(const InterpreterOptions& options) {
options.enable_lazy_bind_initialization,
options.max_recursion_depth,
options.enable_recursive_tracing,
options.enable_fast_builtins};
options.enable_fast_builtins,
options.locale};
}

} // namespace google::api::expr::runtime
7 changes: 7 additions & 0 deletions eval/public/cel_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_CEL_OPTIONS_H_
#define THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_CEL_OPTIONS_H_

#include <string>

#include "absl/base/attributes.h"
#include "runtime/runtime_options.h"
#include "google/protobuf/arena.h"
Expand Down Expand Up @@ -196,6 +198,11 @@ struct InterpreterOptions {
//
// Currently applies to !_, @not_strictly_false, _==_, _!=_, @in
bool enable_fast_builtins = true;

// The locale to use for string formatting.
//
// Default is en_US.
std::string locale = "en_US";
};
// LINT.ThenChange(//depot/google3/runtime/runtime_options.h)

Expand Down
57 changes: 57 additions & 0 deletions extensions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ cc_library(
srcs = ["strings.cc"],
hdrs = ["strings.h"],
deps = [
":formatting",
"//checker:type_checker_builder",
"//checker/internal:builtins_arena",
"//common:casting",
Expand Down Expand Up @@ -578,3 +579,59 @@ cc_test(
"@com_google_absl//absl/status:status_matchers",
],
)

cc_library(
name = "formatting",
srcs = ["formatting.cc"],
hdrs = ["formatting.h"],
deps = [
"//common:value",
"//common:value_kind",
"//internal:status_macros",
"//runtime:function_adapter",
"//runtime:function_registry",
"//runtime:runtime_options",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/numeric:bits",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/time",
"@icu4c",
],
)

cc_test(
name = "formatting_test",
srcs = ["formatting_test.cc"],
deps = [
":formatting",
"//common:allocator",
"//common:value",
"//extensions/protobuf:runtime_adapter",
"//internal:parse_text_proto",
"//internal:testing",
"//internal:testing_descriptor_pool",
"//internal:testing_message_factory",
"//parser",
"//parser:options",
"//runtime",
"//runtime:activation",
"//runtime:runtime_builder",
"//runtime:runtime_options",
"//runtime:standard_runtime_builder_factory",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/time",
"@com_google_cel_spec//proto/cel/expr:syntax_cc_proto",
"@com_google_cel_spec//proto/cel/expr/conformance/proto3:test_all_types_cc_proto",
"@com_google_protobuf//:protobuf",
],
)
Loading

0 comments on commit 3321bc0

Please sign in to comment.