Skip to content

Commit eb309bc

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Change CelExtension definition to return cel::CompilerLibrary.
This change updates the CelExtension interface in cel-python to return a cel::CompilerLibrary instead of implementing a ConfigureCompiler method. This simplifies the integration of C++ extensions, allowing the temporary adapter classes used to bridge the old interface to be removed. PiperOrigin-RevId: 896169754
1 parent 0786ac2 commit eb309bc

15 files changed

Lines changed: 89 additions & 223 deletions

README.md

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -234,36 +234,34 @@ expr = cel_env.compile("my_func(1)")
234234

235235
To define a custom extension in C++, define a class extending
236236
`cel_python::CelExtension`. There are two methods you will need to implement:
237-
`ConfigureCompiler` and `ConfigureRuntime`. The implementations of these methods
238-
use the same API as extensions written for the C++ CEL runtime. In fact,
237+
`GetCompilerLibrary` and `ConfigureRuntime`. The implementations of these
238+
methods use the same API as extensions written for the C++ CEL runtime. In fact,
239239
extensions written for the C++ runtime can be used unchanged with
240240
cel-expr-python - you would just need to write a trivial wrapper class invoking
241241
the registration functions defined by the C++ extension.
242242

243-
```cpp
244-
absl::Status ConfigureCompiler(
245-
cel::CompilerBuilder& compiler_builder,
246-
const proto2::DescriptorPool& descriptor_pool);
247-
```
248243
This method adds extension function definitions to the provided
249244
`CompilerBuilder`, for example:
250245

251246
```cpp
252-
absl::Status ConfigureCompiler(
253-
cel::CompilerBuilder& compiler_builder,
254-
const proto2::DescriptorPool& descriptor_pool) {
255-
CEL_PYTHON_ASSIGN_OR_RETURN(
256-
auto func_translate,
257-
cel::MakeFunctionDecl("translate",
258-
cel::MakeMemberOverloadDecl("translate_inst",
259-
/*return_type=*/cel::StringType(),
260-
/*target=*/cel::StringType(),
261-
/*from_lang=*/cel::StringType(),
262-
/*to_lang=*/cel::StringType())));
263-
CEL_PYTHON_RETURN_IF_ERROR(
264-
compiler_builder.GetCheckerBuilder().AddFunction(func_translate));
265-
return absl::OkStatus();
266-
}
247+
cel::CompilerLibrary GetCompilerLibrary() {
248+
return cel::CompilerLibrary(
249+
"translate-ext",
250+
[](cel::TypeCheckerBuilder& checker_builder) -> absl::Status {
251+
CEL_PYTHON_ASSIGN_OR_RETURN(
252+
auto func_translate,
253+
cel::MakeFunctionDecl(
254+
"translate",
255+
cel::MakeMemberOverloadDecl("translate_inst",
256+
/*return_type=*/cel::StringType(),
257+
/*target=*/cel::StringType(),
258+
/*from_lang=*/cel::StringType(),
259+
/*to_lang=*/cel::StringType())));
260+
CEL_PYTHON_RETURN_IF_ERROR(
261+
checker_builder.AddFunction(func_translate));
262+
return absl::OkStatus();
263+
});
264+
}
267265
```
268266
269267
The other method registers the actual implementation

cel_expr_python/cel_extension.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "compiler/compiler.h"
2525
#include "runtime/runtime_builder.h"
2626
#include "runtime/runtime_options.h"
27-
#include "google/protobuf/descriptor.h"
2827

2928
#if defined(__cpp_exceptions) || defined(__EXCEPTIONS)
3029
// This include causes many issues if included in C++ environments that don't
@@ -42,10 +41,8 @@ class CelExtension {
4241
explicit CelExtension(std::string name) : name_(std::move(name)) {};
4342
virtual ~CelExtension() = default;
4443

45-
virtual absl::Status ConfigureCompiler(
46-
cel::CompilerBuilder& compiler_builder,
47-
const google::protobuf::DescriptorPool& descriptor_pool) {
48-
return absl::OkStatus();
44+
virtual cel::CompilerLibrary GetCompilerLibrary() {
45+
return cel::CompilerLibrary(name_, nullptr, nullptr);
4946
}
5047

5148
virtual absl::Status ConfigureRuntime(cel::RuntimeBuilder& runtime_builder,
@@ -76,11 +73,11 @@ class CelExtension {
7673
//
7774
// CEL_EXTENSION_MODULE(sample_cel_ext, SampleCelExtension);
7875
//
79-
#define CEL_EXTENSION_MODULE(module_name, class_name) \
80-
PYBIND11_MODULE(module_name, m) { \
81-
pybind11::module_::import(CEL_MODULE_NAME); \
76+
#define CEL_EXTENSION_MODULE(module_name, class_name) \
77+
PYBIND11_MODULE(module_name, m) { \
78+
pybind11::module_::import(CEL_MODULE_NAME); \
8279
pybind11::class_<class_name, cel_python::CelExtension>(m, #class_name) \
83-
.def(pybind11::init<>()); \
80+
.def(pybind11::init<>()); \
8481
}
8582

8683
} // namespace cel_python

cel_expr_python/ext/BUILD

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ pybind_extension(
1313
visibility = ["//visibility:public"],
1414
deps = [
1515
"//cel_expr_python:cel_extension",
16-
"@com_google_absl//absl/status",
1716
"@com_google_cel_cpp//compiler",
1817
"@com_google_cel_cpp//extensions:bindings_ext",
19-
"@com_google_protobuf//:protobuf",
2018
],
2119
)
2220

@@ -32,12 +30,10 @@ pybind_extension(
3230
deps = [
3331
"//cel_expr_python:cel_extension",
3432
"@com_google_absl//absl/status",
35-
"@com_google_cel_cpp//checker:type_checker_builder",
3633
"@com_google_cel_cpp//compiler",
3734
"@com_google_cel_cpp//extensions:encoders",
3835
"@com_google_cel_cpp//runtime:runtime_builder",
3936
"@com_google_cel_cpp//runtime:runtime_options",
40-
"@com_google_protobuf//:protobuf",
4137
],
4238
)
4339

@@ -52,25 +48,12 @@ pybind_extension(
5248
visibility = ["//visibility:public"],
5349
deps = [
5450
"//cel_expr_python:cel_extension",
55-
"//cel_expr_python:status_macros",
56-
"@com_google_absl//absl/base:nullability",
5751
"@com_google_absl//absl/status",
58-
"@com_google_absl//absl/status:statusor",
59-
"@com_google_absl//absl/types:span",
60-
"@com_google_cel_cpp//checker:type_checker_builder",
61-
"@com_google_cel_cpp//common:decl",
62-
"@com_google_cel_cpp//common:function_descriptor",
63-
"@com_google_cel_cpp//common:kind",
64-
"@com_google_cel_cpp//common:type",
65-
"@com_google_cel_cpp//common:value",
6652
"@com_google_cel_cpp//compiler",
6753
"@com_google_cel_cpp//extensions:math_ext",
6854
"@com_google_cel_cpp//extensions:math_ext_decls",
69-
"@com_google_cel_cpp//runtime:function",
7055
"@com_google_cel_cpp//runtime:runtime_builder",
7156
"@com_google_cel_cpp//runtime:runtime_options",
72-
"@com_google_cel_spec//proto/cel/expr:checked_cc_proto",
73-
"@com_google_protobuf//:protobuf",
7457
],
7558
)
7659

@@ -91,7 +74,6 @@ pybind_extension(
9174
"@com_google_cel_cpp//runtime:optional_types",
9275
"@com_google_cel_cpp//runtime:runtime_builder",
9376
"@com_google_cel_cpp//runtime:runtime_options",
94-
"@com_google_protobuf//:protobuf",
9577
],
9678
)
9779

@@ -106,10 +88,8 @@ pybind_extension(
10688
visibility = ["//visibility:public"],
10789
deps = [
10890
"//cel_expr_python:cel_extension",
109-
"@com_google_absl//absl/status",
11091
"@com_google_cel_cpp//compiler",
11192
"@com_google_cel_cpp//extensions:proto_ext",
112-
"@com_google_protobuf//:protobuf",
11393
],
11494
)
11595

@@ -129,6 +109,5 @@ pybind_extension(
129109
"@com_google_cel_cpp//extensions:strings",
130110
"@com_google_cel_cpp//runtime:runtime_builder",
131111
"@com_google_cel_cpp//runtime:runtime_options",
132-
"@com_google_protobuf//:protobuf",
133112
],
134113
)

cel_expr_python/ext/ext_bindings.cc

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,18 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "absl/status/status.h"
1615
#include "compiler/compiler.h"
1716
#include "extensions/bindings_ext.h"
1817
#include "cel_expr_python/cel_extension.h"
19-
#include "google/protobuf/descriptor.h"
2018

2119
namespace cel_python {
2220

2321
class ExtBindings : public CelExtension {
2422
public:
2523
explicit ExtBindings() : CelExtension("cel.lib.ext.cel.bindings") {}
2624

27-
absl::Status ConfigureCompiler(
28-
cel::CompilerBuilder& compiler_builder,
29-
const google::protobuf::DescriptorPool& descriptor_pool) override {
30-
return compiler_builder.AddLibrary(
31-
cel::extensions::BindingsCompilerLibrary());
25+
cel::CompilerLibrary GetCompilerLibrary() override {
26+
return cel::extensions::BindingsCompilerLibrary();
3227
}
3328
};
3429

cel_expr_python/ext/ext_encoders.cc

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,20 @@
1313
// limitations under the License.
1414

1515
#include "absl/status/status.h"
16-
#include "checker/type_checker_builder.h"
1716
#include "compiler/compiler.h"
1817
#include "extensions/encoders.h"
1918
#include "runtime/runtime_builder.h"
2019
#include "runtime/runtime_options.h"
2120
#include "cel_expr_python/cel_extension.h"
22-
#include "google/protobuf/descriptor.h"
2321

2422
namespace cel_python {
2523

2624
class ExtEncoders : public CelExtension {
2725
public:
2826
explicit ExtEncoders() : CelExtension("cel.lib.ext.encoders") {}
2927

30-
absl::Status ConfigureCompiler(
31-
cel::CompilerBuilder& compiler_builder,
32-
const google::protobuf::DescriptorPool& descriptor_pool) override {
33-
return compiler_builder.GetCheckerBuilder().AddLibrary(
34-
cel::extensions::EncodersCheckerLibrary());
28+
cel::CompilerLibrary GetCompilerLibrary() override {
29+
return cel::extensions::EncodersCompilerLibrary();
3530
}
3631

3732
absl::Status ConfigureRuntime(cel::RuntimeBuilder& runtime_builder,

cel_expr_python/ext/ext_math.cc

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,21 @@
1919
#include "runtime/runtime_builder.h"
2020
#include "runtime/runtime_options.h"
2121
#include "cel_expr_python/cel_extension.h"
22-
#include "cel_expr_python/status_macros.h"
23-
#include "google/protobuf/descriptor.h"
2422

2523
namespace cel_python {
2624

2725
class ExtMath : public CelExtension {
2826
public:
2927
explicit ExtMath() : CelExtension("cel.lib.ext.math") {}
3028

31-
absl::Status ConfigureCompiler(
32-
cel::CompilerBuilder& compiler_builder,
33-
const google::protobuf::DescriptorPool& descriptor_pool) override {
34-
return compiler_builder.AddLibrary(cel::extensions::MathCompilerLibrary());
29+
cel::CompilerLibrary GetCompilerLibrary() override {
30+
return cel::extensions::MathCompilerLibrary();
3531
}
3632

3733
absl::Status ConfigureRuntime(cel::RuntimeBuilder& runtime_builder,
3834
const cel::RuntimeOptions& opts) override {
39-
CEL_PYTHON_RETURN_IF_ERROR(cel::extensions::RegisterMathExtensionFunctions(
40-
runtime_builder.function_registry(), opts));
41-
return absl::OkStatus();
35+
return cel::extensions::RegisterMathExtensionFunctions(
36+
runtime_builder.function_registry(), opts);
4237
}
4338
};
4439

cel_expr_python/ext/ext_optional.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,15 @@
1919
#include "runtime/runtime_builder.h"
2020
#include "runtime/runtime_options.h"
2121
#include "cel_expr_python/cel_extension.h"
22-
#include "google/protobuf/descriptor.h"
2322

2423
namespace cel_python {
2524

2625
class ExtOptional : public CelExtension {
2726
public:
2827
explicit ExtOptional() : CelExtension("optional") {}
2928

30-
absl::Status ConfigureCompiler(
31-
cel::CompilerBuilder& compiler_builder,
32-
const google::protobuf::DescriptorPool& descriptor_pool) override {
33-
return compiler_builder.AddLibrary(cel::OptionalCompilerLibrary());
29+
cel::CompilerLibrary GetCompilerLibrary() override {
30+
return cel::OptionalCompilerLibrary();
3431
}
3532

3633
absl::Status ConfigureRuntime(cel::RuntimeBuilder& runtime_builder,

cel_expr_python/ext/ext_proto.cc

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,18 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "absl/status/status.h"
1615
#include "compiler/compiler.h"
1716
#include "extensions/proto_ext.h"
1817
#include "cel_expr_python/cel_extension.h"
19-
#include "google/protobuf/descriptor.h"
2018

2119
namespace cel_python {
2220

2321
class ExtProto : public CelExtension {
2422
public:
2523
explicit ExtProto() : CelExtension("cel.lib.ext.proto") {}
2624

27-
absl::Status ConfigureCompiler(
28-
cel::CompilerBuilder& compiler_builder,
29-
const google::protobuf::DescriptorPool& descriptor_pool) override {
30-
return compiler_builder.AddLibrary(
31-
cel::extensions::ProtoExtCompilerLibrary());
25+
cel::CompilerLibrary GetCompilerLibrary() override {
26+
return cel::extensions::ProtoExtCompilerLibrary();
3227
}
3328
};
3429

cel_expr_python/ext/ext_string.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,15 @@
1818
#include "runtime/runtime_builder.h"
1919
#include "runtime/runtime_options.h"
2020
#include "cel_expr_python/cel_extension.h"
21-
#include "google/protobuf/descriptor.h"
2221

2322
namespace cel_python {
2423

2524
class ExtString : public CelExtension {
2625
public:
2726
explicit ExtString() : CelExtension("cel.lib.ext.string") {}
2827

29-
absl::Status ConfigureCompiler(
30-
cel::CompilerBuilder& compiler_builder,
31-
const google::protobuf::DescriptorPool& descriptor_pool) override {
32-
return compiler_builder.AddLibrary(
33-
cel::extensions::StringsCompilerLibrary());
28+
cel::CompilerLibrary GetCompilerLibrary() override {
29+
return cel::extensions::StringsCompilerLibrary();
3430
}
3531

3632
absl::Status ConfigureRuntime(cel::RuntimeBuilder& runtime_builder,

0 commit comments

Comments
 (0)