Skip to content

Commit 8c48cdb

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
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: 893173014
1 parent eb309bc commit 8c48cdb

3 files changed

Lines changed: 30 additions & 31 deletions

File tree

cel_expr_python/cel_env_test.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
ability to be created from and serialized to YAML format.
1919
"""
2020

21+
import textwrap
22+
2123
from absl.testing import absltest
2224
from cel_expr_python import cel
2325
from cel_expr_python.ext import ext_math
@@ -92,6 +94,18 @@ def test_invalid_yaml(self):
9294
str(e.exception),
9395
)
9496

97+
def test_config_export_container(self):
98+
env = cel.NewEnv(
99+
container="test.container"
100+
)
101+
yaml = env.config().to_yaml()
102+
self.assertEqual(
103+
normalize_yaml(yaml),
104+
normalize_yaml("""
105+
container: "test.container"
106+
"""),
107+
)
108+
95109
def test_config_export_variables(self):
96110
config = cel.NewEnv(
97111
variables={
@@ -308,25 +322,7 @@ def __init__(self):
308322

309323

310324
def normalize_yaml(yaml: str) -> str:
311-
lines = yaml.split("\n")
312-
indent = -1
313-
unindented_lines = []
314-
for line in lines:
315-
pos = -1
316-
for i, char in enumerate(line):
317-
if char != " " and char != "\t":
318-
pos = i
319-
break
320-
if pos == -1:
321-
# Skip blank lines.
322-
continue
323-
if indent == -1:
324-
indent = pos
325-
if pos >= indent:
326-
unindented_lines.append(line[indent:])
327-
else:
328-
unindented_lines.append(line)
329-
return "\n".join(unindented_lines)
325+
return textwrap.dedent(yaml).strip()
330326

331327

332328
if __name__ == "__main__":

cel_expr_python/py_cel_env_internal.cc

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,15 @@ namespace cel_python {
5353

5454
PyCelEnvInternal::PyCelEnvInternal(
5555
const PyCelEnvConfig& env_config, PyObject* py_descriptor_pool,
56-
std::vector<CelExtensionHandle> extension_handles,
57-
const std::string& container)
56+
std::vector<CelExtensionHandle> extension_handles)
5857
: env_config_(env_config),
5958
py_descriptor_database_(py_descriptor_pool),
6059
descriptor_pool_(
6160
std::make_shared<google::protobuf::DescriptorPool>(&py_descriptor_database_)),
6261
message_factory_(descriptor_pool_.get()),
6362
py_message_factory_(
6463
std::make_shared<PyMessageFactory>(py_descriptor_pool)),
65-
extensions_(std::move(extension_handles)),
66-
container_(std::move(container)) {
64+
extensions_(std::move(extension_handles)) {
6765
cel_env_.SetDescriptorPool(descriptor_pool_);
6866
cel_env_.SetConfig(env_config_.GetConfig());
6967
cel::RegisterStandardExtensions(cel_env_);
@@ -115,9 +113,10 @@ PyCelEnvInternal::NewCelEnvInternal(
115113
extension_handles.push_back(std::move(handle));
116114
}
117115

116+
config.SetContainerConfig(cel::Config::ContainerConfig{.name = container});
118117
return std::shared_ptr<PyCelEnvInternal>(
119118
new PyCelEnvInternal(PyCelEnvConfig(config), py_descriptor_pool,
120-
std::move(extension_handles), container));
119+
std::move(extension_handles)));
121120
}
122121

123122
absl::StatusOr<const cel::Compiler*> PyCelEnvInternal::GetCompiler(
@@ -128,15 +127,21 @@ absl::StatusOr<const cel::Compiler*> PyCelEnvInternal::GetCompiler(
128127
return env->compiler_.get();
129128
}
130129

130+
const cel::Config& config = env->env_config_.GetConfig();
131+
131132
CEL_PYTHON_ASSIGN_OR_RETURN(
132133
std::unique_ptr<cel::CompilerBuilder> compiler_builder,
133134
env->cel_env_.NewCompilerBuilder());
134-
compiler_builder->GetCheckerBuilder().set_container(env->container_);
135+
136+
cel::TypeCheckerBuilder& checker_builder =
137+
compiler_builder->GetCheckerBuilder();
138+
139+
checker_builder.set_container(config.GetContainerConfig().name);
135140

136141
// Convert variable types from cel::TypeInfo to PyCelType.
137-
google::protobuf::Arena* arena = compiler_builder->GetCheckerBuilder().arena();
142+
google::protobuf::Arena* arena = checker_builder.arena();
138143
for (const cel::Config::VariableConfig& variable_config :
139-
env->env_config_.GetConfig().GetVariableConfigs()) {
144+
config.GetVariableConfigs()) {
140145
CEL_PYTHON_ASSIGN_OR_RETURN(
141146
cel::Type cel_type,
142147
cel::TypeInfoToType(variable_config.type_info,
@@ -156,7 +161,7 @@ absl::StatusOr<const cel::Runtime*> PyCelEnvInternal::GetRuntime(
156161
}
157162

158163
cel::RuntimeOptions& opts = env->cel_env_runtime_.mutable_runtime_options();
159-
opts.container = env->container_;
164+
opts.container = env->GetEnvConfig().GetConfig().GetContainerConfig().name;
160165
opts.enable_empty_wrapper_null_unboxing = true;
161166
opts.enable_qualified_type_identifiers = true;
162167
opts.enable_timestamp_duration_overflow_errors = true;

cel_expr_python/py_cel_env_internal.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ class PyCelEnvInternal {
109109
// Use NewCelEnvInternal() to create an instance.
110110
PyCelEnvInternal(const PyCelEnvConfig& env_config,
111111
PyObject* py_descriptor_pool,
112-
std::vector<CelExtensionHandle> extensions,
113-
const std::string& container);
112+
std::vector<CelExtensionHandle> extensions);
114113

115114
absl::Status ConfigureStandardExtension(
116115
cel::CompilerBuilder& compiler_builder, const std::string& extension);
@@ -129,7 +128,6 @@ class PyCelEnvInternal {
129128
// Synchronized by the GIL.
130129
std::unordered_map<std::string, PyCelType> variable_types_;
131130
std::vector<CelExtensionHandle> extensions_;
132-
std::string container_;
133131
std::unique_ptr<cel::Compiler> compiler_;
134132
absl::flat_hash_map<RuntimeMode, std::unique_ptr<const cel::Runtime>>
135133
runtimes_;

0 commit comments

Comments
 (0)