Skip to content

Add support for is_in event parameter comparison operator #875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
15 changes: 6 additions & 9 deletions nemoguardrails/colang/v2_x/runtime/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,11 @@ class ComparisonExpression:
"""An expression to compare to values."""

def __init__(self, operator: Callable[[Any], bool], value: Any) -> None:
if not isinstance(value, (int, float)):
raise ColangValueError(
f"Comparison operators don't support values of type '{type(value)}'"
)
self.value = value
self.operator = operator

def compare(self, value: Any) -> bool:
"""Compare given value with the expression's value."""
if not isinstance(value, type(self.value)):
raise ColangValueError(
"Comparing variables of different types is not supported!"
)

return self.operator(value)


Expand Down Expand Up @@ -168,6 +159,7 @@ def eval_expression(expr: str, context: dict) -> Any:
"greater_than": _greater_than_operator,
"equal_greater_than": _equal_or_greater_than_operator,
"not_equal_to": _not_equal_to_operator,
"is_in": _is_in,
"list": list,
}
)
Expand Down Expand Up @@ -286,6 +278,11 @@ def _not_equal_to_operator(v_ref: Any) -> ComparisonExpression:
return ComparisonExpression(lambda val, val_ref=v_ref: val != val_ref, v_ref)


def _is_in(v_ref: Any) -> ComparisonExpression:
"""Create a is in comparison expression."""
return ComparisonExpression(lambda val, val_ref=v_ref: val in val_ref, v_ref)


def _flows_info(state: State, flow_instance_uid: Optional[str] = None) -> dict:
"""Return a summary of the provided state, or all states by default."""
if flow_instance_uid is not None and flow_instance_uid in state.flow_states:
Expand Down