Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions cel_expr_python/cel_env_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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={
Expand Down Expand Up @@ -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__":
Expand Down
23 changes: 14 additions & 9 deletions cel_expr_python/py_cel_env_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,15 @@ namespace cel_python {

PyCelEnvInternal::PyCelEnvInternal(
const PyCelEnvConfig& env_config, PyObject* py_descriptor_pool,
std::vector<CelExtensionHandle> extension_handles,
const std::string& container)
std::vector<CelExtensionHandle> extension_handles)
: env_config_(env_config),
py_descriptor_database_(py_descriptor_pool),
descriptor_pool_(
std::make_shared<google::protobuf::DescriptorPool>(&py_descriptor_database_)),
message_factory_(descriptor_pool_.get()),
py_message_factory_(
std::make_shared<PyMessageFactory>(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_);
Expand Down Expand Up @@ -115,9 +113,10 @@ PyCelEnvInternal::NewCelEnvInternal(
extension_handles.push_back(std::move(handle));
}

config.SetContainerConfig(cel::Config::ContainerConfig{.name = container});
return std::shared_ptr<PyCelEnvInternal>(
new PyCelEnvInternal(PyCelEnvConfig(config), py_descriptor_pool,
std::move(extension_handles), container));
std::move(extension_handles)));
}

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

const cel::Config& config = env->env_config_.GetConfig();

CEL_PYTHON_ASSIGN_OR_RETURN(
std::unique_ptr<cel::CompilerBuilder> 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,
Expand All @@ -156,7 +161,7 @@ absl::StatusOr<const cel::Runtime*> 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;
Expand Down
4 changes: 1 addition & 3 deletions cel_expr_python/py_cel_env_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ class PyCelEnvInternal {
// Use NewCelEnvInternal() to create an instance.
PyCelEnvInternal(const PyCelEnvConfig& env_config,
PyObject* py_descriptor_pool,
std::vector<CelExtensionHandle> extensions,
const std::string& container);
std::vector<CelExtensionHandle> extensions);

absl::Status ConfigureStandardExtension(
cel::CompilerBuilder& compiler_builder, const std::string& extension);
Expand All @@ -129,7 +128,6 @@ class PyCelEnvInternal {
// Synchronized by the GIL.
std::unordered_map<std::string, PyCelType> variable_types_;
std::vector<CelExtensionHandle> extensions_;
std::string container_;
std::unique_ptr<cel::Compiler> compiler_;
absl::flat_hash_map<RuntimeMode, std::unique_ptr<const cel::Runtime>>
runtimes_;
Expand Down
Loading