mirror of
https://github.com/hoshikawa2/first_contas.git
synced 2026-07-09 18:24:20 +00:00
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
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}")
|