From 35a65f6cc1260f2e3b2ef6102b05445cbdc6c2a0 Mon Sep 17 00:00:00 2001 From: Dmitri Plotnikov Date: Tue, 7 Apr 2026 17:35:03 -0700 Subject: [PATCH] Store CEL container within cel::Config This change simplifies the internal representation and ensures the container is part of the exported environment configuration. PiperOrigin-RevId: 896171337 --- cel_expr_python/cel_env_test.py | 34 ++++++++++++-------------- cel_expr_python/py_cel_env_internal.cc | 23 ++++++++++------- cel_expr_python/py_cel_env_internal.h | 4 +-- 3 files changed, 30 insertions(+), 31 deletions(-) diff --git a/cel_expr_python/cel_env_test.py b/cel_expr_python/cel_env_test.py index cd26c77..2aa16c2 100644 --- a/cel_expr_python/cel_env_test.py +++ b/cel_expr_python/cel_env_test.py @@ -18,6 +18,8 @@ ability to be created from and serialized to YAML format. """ +import textwrap + from absl.testing import absltest from cel_expr_python import cel from cel_expr_python.ext import ext_math @@ -92,6 +94,18 @@ def test_invalid_yaml(self): str(e.exception), ) + def test_config_export_container(self): + env = cel.NewEnv( + container="test.container" + ) + yaml = env.config().to_yaml() + self.assertEqual( + normalize_yaml(yaml), + normalize_yaml(""" + container: "test.container" + """), + ) + def test_config_export_variables(self): config = cel.NewEnv( variables={ @@ -308,25 +322,7 @@ def __init__(self): def normalize_yaml(yaml: str) -> str: - lines = yaml.split("\n") - indent = -1 - unindented_lines = [] - for line in lines: - pos = -1 - for i, char in enumerate(line): - if char != " " and char != "\t": - pos = i - break - if pos == -1: - # Skip blank lines. - continue - if indent == -1: - indent = pos - if pos >= indent: - unindented_lines.append(line[indent:]) - else: - unindented_lines.append(line) - return "\n".join(unindented_lines) + return textwrap.dedent(yaml).strip() if __name__ == "__main__": diff --git a/cel_expr_python/py_cel_env_internal.cc b/cel_expr_python/py_cel_env_internal.cc index 9e2c885..7af9af2 100644 --- a/cel_expr_python/py_cel_env_internal.cc +++ b/cel_expr_python/py_cel_env_internal.cc @@ -53,8 +53,7 @@ namespace cel_python { PyCelEnvInternal::PyCelEnvInternal( const PyCelEnvConfig& env_config, PyObject* py_descriptor_pool, - std::vector extension_handles, - const std::string& container) + std::vector extension_handles) : env_config_(env_config), py_descriptor_database_(py_descriptor_pool), descriptor_pool_( @@ -62,8 +61,7 @@ PyCelEnvInternal::PyCelEnvInternal( message_factory_(descriptor_pool_.get()), py_message_factory_( std::make_shared(py_descriptor_pool)), - extensions_(std::move(extension_handles)), - container_(std::move(container)) { + extensions_(std::move(extension_handles)) { cel_env_.SetDescriptorPool(descriptor_pool_); cel_env_.SetConfig(env_config_.GetConfig()); cel::RegisterStandardExtensions(cel_env_); @@ -115,9 +113,10 @@ PyCelEnvInternal::NewCelEnvInternal( extension_handles.push_back(std::move(handle)); } + config.SetContainerConfig(cel::Config::ContainerConfig{.name = container}); return std::shared_ptr( new PyCelEnvInternal(PyCelEnvConfig(config), py_descriptor_pool, - std::move(extension_handles), container)); + std::move(extension_handles))); } absl::StatusOr PyCelEnvInternal::GetCompiler( @@ -128,15 +127,21 @@ absl::StatusOr PyCelEnvInternal::GetCompiler( return env->compiler_.get(); } + const cel::Config& config = env->env_config_.GetConfig(); + CEL_PYTHON_ASSIGN_OR_RETURN( std::unique_ptr compiler_builder, env->cel_env_.NewCompilerBuilder()); - compiler_builder->GetCheckerBuilder().set_container(env->container_); + + cel::TypeCheckerBuilder& checker_builder = + compiler_builder->GetCheckerBuilder(); + + checker_builder.set_container(config.GetContainerConfig().name); // Convert variable types from cel::TypeInfo to PyCelType. - google::protobuf::Arena* arena = compiler_builder->GetCheckerBuilder().arena(); + google::protobuf::Arena* arena = checker_builder.arena(); for (const cel::Config::VariableConfig& variable_config : - env->env_config_.GetConfig().GetVariableConfigs()) { + config.GetVariableConfigs()) { CEL_PYTHON_ASSIGN_OR_RETURN( cel::Type cel_type, cel::TypeInfoToType(variable_config.type_info, @@ -156,7 +161,7 @@ absl::StatusOr PyCelEnvInternal::GetRuntime( } cel::RuntimeOptions& opts = env->cel_env_runtime_.mutable_runtime_options(); - opts.container = env->container_; + opts.container = env->GetEnvConfig().GetConfig().GetContainerConfig().name; opts.enable_empty_wrapper_null_unboxing = true; opts.enable_qualified_type_identifiers = true; opts.enable_timestamp_duration_overflow_errors = true; diff --git a/cel_expr_python/py_cel_env_internal.h b/cel_expr_python/py_cel_env_internal.h index 744881f..aecbcb1 100644 --- a/cel_expr_python/py_cel_env_internal.h +++ b/cel_expr_python/py_cel_env_internal.h @@ -109,8 +109,7 @@ class PyCelEnvInternal { // Use NewCelEnvInternal() to create an instance. PyCelEnvInternal(const PyCelEnvConfig& env_config, PyObject* py_descriptor_pool, - std::vector extensions, - const std::string& container); + std::vector extensions); absl::Status ConfigureStandardExtension( cel::CompilerBuilder& compiler_builder, const std::string& extension); @@ -129,7 +128,6 @@ class PyCelEnvInternal { // Synchronized by the GIL. std::unordered_map variable_types_; std::vector extensions_; - std::string container_; std::unique_ptr compiler_; absl::flat_hash_map> runtimes_;