mirror of
https://github.com/hoshikawa2/first_contas.git
synced 2026-07-09 18:24:20 +00:00
first commit
This commit is contained in:
59
legacy_reference/workflows/conditions.py
Normal file
59
legacy_reference/workflows/conditions.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from agente_contas_tim.workflows.exceptions import WorkflowConfigurationError
|
||||
|
||||
|
||||
def get_path_value(data: dict[str, Any], path: str) -> Any:
|
||||
"""Resolve paths simples no formato $.a.b.c."""
|
||||
if not path.startswith("$."):
|
||||
return path
|
||||
|
||||
current: Any = data
|
||||
for part in path[2:].split("."):
|
||||
if isinstance(current, dict):
|
||||
current = current.get(part)
|
||||
continue
|
||||
return None
|
||||
return current
|
||||
|
||||
|
||||
def resolve_value(value: Any, context: dict[str, Any]) -> Any:
|
||||
if isinstance(value, str) and value.startswith("$."):
|
||||
return get_path_value(context, value)
|
||||
return value
|
||||
|
||||
|
||||
def evaluate_condition(
|
||||
condition: dict[str, Any] | None, context: dict[str, Any]
|
||||
) -> bool:
|
||||
if condition is None:
|
||||
return True
|
||||
|
||||
if "eq" in condition:
|
||||
left, right = condition["eq"]
|
||||
return resolve_value(left, context) == resolve_value(right, context)
|
||||
|
||||
if "neq" in condition:
|
||||
left, right = condition["neq"]
|
||||
return resolve_value(left, context) != resolve_value(right, context)
|
||||
|
||||
if "all" in condition:
|
||||
return all(evaluate_condition(item, context) for item in condition["all"])
|
||||
|
||||
if "any" in condition:
|
||||
return any(evaluate_condition(item, context) for item in condition["any"])
|
||||
|
||||
if "exists" in condition:
|
||||
return resolve_value(condition["exists"], context) is not None
|
||||
|
||||
if "in" in condition:
|
||||
left, right = condition["in"]
|
||||
return resolve_value(left, context) in resolve_value(right, context)
|
||||
|
||||
if "not_in" in condition:
|
||||
left, right = condition["not_in"]
|
||||
return resolve_value(left, context) not in resolve_value(right, context)
|
||||
|
||||
raise WorkflowConfigurationError(f"Operador de condição não suportado: {condition}")
|
||||
Reference in New Issue
Block a user