Skip to content

Commit 3eea687

Browse files
CEL Dev Teamcopybara-github
CEL Dev Team
authored andcommitted
Add strings.format CEL extension implementation to C++.
PiperOrigin-RevId: 718991570
1 parent e604d82 commit 3eea687

File tree

11 files changed

+1672
-19
lines changed

11 files changed

+1672
-19
lines changed

bazel/antlr.bzl

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,13 @@ def antlr_cc_library(name, src, package):
4242
def _antlr_library(ctx):
4343
output = ctx.actions.declare_directory(ctx.attr.name)
4444

45-
src_path = ctx.file.src.path
46-
47-
# Workaround for Antlr4 bug:
48-
# https://github.com/antlr/antlr4/issues/3138
49-
windows_constraint = ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]
50-
if ctx.target_platform_has_constraint(windows_constraint):
51-
src_path = src_path.replace('/', '\\')
52-
5345
antlr_args = ctx.actions.args()
5446
antlr_args.add("-Dlanguage=Cpp")
5547
antlr_args.add("-no-listener")
5648
antlr_args.add("-visitor")
5749
antlr_args.add("-o", output.path)
5850
antlr_args.add("-package", ctx.attr.package)
59-
antlr_args.add(src_path)
51+
antlr_args.add(ctx.file.src)
6052

6153
# Strip ".g4" extension.
6254
basename = ctx.file.src.basename[:-3]
@@ -106,6 +98,5 @@ antlr_library = rule(
10698
cfg = "exec", # buildifier: disable=attr-cfg
10799
default = Label("//bazel:antlr4_tool"),
108100
),
109-
'_windows_constraint': attr.label(default = '@platforms//os:windows'),
110101
},
111102
)

bazel/deps.bzl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ def cel_spec_deps():
150150
urls = ["https://github.com/google/cel-spec/archive/" + CEL_SPEC_GIT_SHA + ".zip"],
151151
)
152152

153+
_ICU4C_VERSION_MAJOR = "76"
154+
_ICU4C_VERSION_MINOR = "1"
153155
_ICU4C_BUILD = """
154156
load("@rules_foreign_cc//foreign_cc:configure.bzl", "configure_make")
155157
@@ -161,9 +163,9 @@ filegroup(
161163
162164
config_setting(
163165
name = "dbg",
164-
values = {
166+
values = {{
165167
"compilation_mode": "dbg",
166-
},
168+
}},
167169
visibility = ["//visibility:private"],
168170
)
169171
@@ -178,16 +180,24 @@ configure_make(
178180
"--disable-icuio",
179181
"--disable-layoutex",
180182
"--disable-icu-config",
181-
] + select({
183+
] + select({{
182184
":dbg": ["--enable-debug"],
183185
"//conditions:default": [],
184-
}),
186+
}}),
185187
lib_source = ":all",
186188
out_shared_libs = [
187189
"libicudata.so",
190+
"libicudata.so.{version_major}",
191+
"libicudata.so.{version_major}.{version_minor}",
188192
"libicui18n.so",
193+
"libicui18n.so.{version_major}",
194+
"libicui18n.so.{version_major}.{version_minor}",
189195
"libicutu.so",
196+
"libicutu.so.{version_major}",
197+
"libicutu.so.{version_major}.{version_minor}",
190198
"libicuuc.so",
199+
"libicuuc.so.{version_major}",
200+
"libicuuc.so.{version_major}.{version_minor}",
191201
],
192202
out_static_libs = [
193203
"libicudata.a",
@@ -198,7 +208,7 @@ configure_make(
198208
args = ["-j 8"],
199209
visibility = ["//visibility:public"],
200210
)
201-
"""
211+
""".format(version_major = _ICU4C_VERSION_MAJOR, version_minor = _ICU4C_VERSION_MINOR)
202212

203213
def cel_cpp_extensions_deps():
204214
http_archive(
@@ -210,7 +220,7 @@ def cel_cpp_extensions_deps():
210220
http_archive(
211221
name = "icu4c",
212222
sha256 = "dfacb46bfe4747410472ce3e1144bf28a102feeaa4e3875bac9b4c6cf30f4f3e",
213-
url = "https://github.com/unicode-org/icu/releases/download/release-76-1/icu4c-76_1-src.tgz",
223+
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),
214224
strip_prefix = "icu",
215225
patch_cmds = [
216226
"rm -f source/common/BUILD.bazel",

eval/public/cel_options.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ cel::RuntimeOptions ConvertToRuntimeOptions(const InterpreterOptions& options) {
4040
options.enable_lazy_bind_initialization,
4141
options.max_recursion_depth,
4242
options.enable_recursive_tracing,
43-
options.enable_fast_builtins};
43+
options.enable_fast_builtins,
44+
options.locale};
4445
}
4546

4647
} // namespace google::api::expr::runtime

eval/public/cel_options.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_CEL_OPTIONS_H_
1818
#define THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_CEL_OPTIONS_H_
1919

20+
#include <string>
21+
2022
#include "absl/base/attributes.h"
2123
#include "runtime/runtime_options.h"
2224
#include "google/protobuf/arena.h"
@@ -196,6 +198,11 @@ struct InterpreterOptions {
196198
//
197199
// Currently applies to !_, @not_strictly_false, _==_, _!=_, @in
198200
bool enable_fast_builtins = true;
201+
202+
// The locale to use for string formatting.
203+
//
204+
// Default is en_US.
205+
std::string locale = "en_US";
199206
};
200207
// LINT.ThenChange(//depot/google3/runtime/runtime_options.h)
201208

extensions/BUILD

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,15 +439,22 @@ cc_test(
439439

440440
cc_library(
441441
name = "strings",
442-
srcs = ["strings.cc"],
443-
hdrs = ["strings.h"],
442+
srcs = [
443+
"formatting.cc",
444+
"strings.cc",
445+
],
446+
hdrs = [
447+
"formatting.h",
448+
"strings.h",
449+
],
444450
deps = [
445451
"//checker:type_checker_builder",
446452
"//checker/internal:builtins_arena",
447453
"//common:casting",
448454
"//common:decl",
449455
"//common:type",
450456
"//common:value",
457+
"//common:value_kind",
451458
"//eval/public:cel_function_registry",
452459
"//eval/public:cel_options",
453460
"//internal:status_macros",
@@ -456,12 +463,19 @@ cc_library(
456463
"//runtime:function_registry",
457464
"//runtime:runtime_options",
458465
"//runtime/internal:errors",
466+
"@com_google_absl//absl/base:core_headers",
459467
"@com_google_absl//absl/base:no_destructor",
468+
"@com_google_absl//absl/container:btree",
469+
"@com_google_absl//absl/memory",
470+
"@com_google_absl//absl/numeric:bits",
460471
"@com_google_absl//absl/status",
461472
"@com_google_absl//absl/status:statusor",
462473
"@com_google_absl//absl/strings",
463474
"@com_google_absl//absl/strings:cord",
475+
"@com_google_absl//absl/strings:str_format",
464476
"@com_google_absl//absl/strings:string_view",
477+
"@com_google_absl//absl/time",
478+
"@icu4c",
465479
],
466480
)
467481

@@ -578,3 +592,34 @@ cc_test(
578592
"@com_google_absl//absl/status:status_matchers",
579593
],
580594
)
595+
596+
cc_test(
597+
name = "formatting_test",
598+
srcs = ["formatting_test.cc"],
599+
deps = [
600+
":strings",
601+
"//common:allocator",
602+
"//common:value",
603+
"//extensions/protobuf:runtime_adapter",
604+
"//internal:parse_text_proto",
605+
"//internal:testing",
606+
"//internal:testing_descriptor_pool",
607+
"//internal:testing_message_factory",
608+
"//parser",
609+
"//parser:options",
610+
"//runtime",
611+
"//runtime:activation",
612+
"//runtime:runtime_builder",
613+
"//runtime:runtime_options",
614+
"//runtime:standard_runtime_builder_factory",
615+
"@com_google_absl//absl/container:flat_hash_map",
616+
"@com_google_absl//absl/status:status_matchers",
617+
"@com_google_absl//absl/strings",
618+
"@com_google_absl//absl/strings:str_format",
619+
"@com_google_absl//absl/strings:string_view",
620+
"@com_google_absl//absl/time",
621+
"@com_google_cel_spec//proto/cel/expr:syntax_cc_proto",
622+
"@com_google_cel_spec//proto/cel/expr/conformance/proto3:test_all_types_cc_proto",
623+
"@com_google_protobuf//:protobuf",
624+
],
625+
)

0 commit comments

Comments
 (0)