Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open
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
2 changes: 2 additions & 0 deletions 2 cel_expr_python/cel.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class CelExtensionBase:
def __init__(self, name: str) -> None: ...

class EnvConfig:
@property
def context_type(self) -> str: ...
def to_yaml(self) -> str: ...

class ExpressionContainer:
Expand Down
46 changes: 46 additions & 0 deletions 46 cel_expr_python/cel_env_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,52 @@ def test_invalid_yaml(self):
str(e.exception),
)

def test_parse_context_variable_config(self):
config = cel.NewEnvConfigFromYaml("""
context_variable:
type_name: "cel.expr.conformance.proto2.TestAllTypes"
""")
self.assertEqual(
config.context_type, "cel.expr.conformance.proto2.TestAllTypes"
)

def test_parse_context_variable_config_alternative_syntax(self):
config = cel.NewEnvConfigFromYaml("""
context_variable:
type: "cel.expr.conformance.proto2.TestAllTypes"
""")
self.assertEqual(
config.context_type, "cel.expr.conformance.proto2.TestAllTypes"
)

def test_parse_context_variable_malformed(self):
with self.assertRaisesRegex(
Exception, "Node 'context_variable' is not a map"
):
cel.NewEnvConfigFromYaml("context_variable: 123")

def test_parse_context_variable_malformed2(self):
with self.assertRaisesRegex(
Exception, "Node 'context_variable' does not have a valid type"
):
cel.NewEnvConfigFromYaml("""
context_variable:
type:
foo: bar
""")

def test_context_variable_basic(self):
config = cel.NewEnvConfigFromYaml("""
context_variable:
type_name: "cel.expr.conformance.proto2.TestAllTypes"
""")
env = cel.NewEnv(config=config)
ast = env.compile("single_int32 > 10")
self.assertIsNotNone(ast)

with self.assertRaises(Exception):
env.compile("non_existent_field > 10")

def test_config_export_container(self):
env: cel.Env = cel.NewEnv(container="test.container")
yaml: str = env.config().to_yaml()
Expand Down
2 changes: 2 additions & 0 deletions 2 cel_expr_python/py_cel_env_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ void PyCelEnvConfig::DefinePythonBindings(pybind11::module& m) {
m.def("NewEnvConfigFromYaml", &PyCelEnvConfig::FromYaml, py::arg("yaml"));

cel_class.def("to_yaml", &PyCelEnvConfig::ToYaml);
cel_class.def_property_readonly("context_type",
&PyCelEnvConfig::GetContextType);
}

PyCelEnvConfig PyCelEnvConfig::FromYaml(std::string yaml) {
Expand Down
1 change: 1 addition & 0 deletions 1 cel_expr_python/py_cel_env_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class PyCelEnvConfig {
std::string ToYaml() const;

const cel::Config& GetConfig() const { return config_; }
std::string GetContextType() const { return config_.GetContextType(); }

private:
cel::Config config_;
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.