forked from longtao.wu/droboticflow
56 lines
2.0 KiB
Python
56 lines
2.0 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Callable
|
|
|
|
|
|
HelperMap = dict[str, Callable[..., Any]]
|
|
|
|
|
|
def run(
|
|
run_id: str,
|
|
node_id: str,
|
|
output_dir: Any,
|
|
node_data: dict[str, Any],
|
|
inputs: list[dict[str, Any]],
|
|
helpers: HelperMap,
|
|
) -> list[dict[str, Any]]:
|
|
params = helpers["hyperparams"](node_data)
|
|
field_path = params.get("FIELD_PATH", "").strip()
|
|
operator = helpers["normalize_comparison_operator"](params.get("OPERATOR", "eq"))
|
|
expected = helpers["parse_json_param"](params, "EXPECTED_JSON", params.get("EXPECTED", ""))
|
|
rows = helpers["collect_items"](helpers["payloads_from_inputs"](inputs))
|
|
matched = [
|
|
row
|
|
for row in rows
|
|
if helpers["compare_values"](helpers["get_field_value"](row, field_path), operator, expected)
|
|
]
|
|
unmatched = [
|
|
row
|
|
for row in rows
|
|
if not helpers["compare_values"](helpers["get_field_value"](row, field_path), operator, expected)
|
|
]
|
|
outputs = node_data.get("outputs", []) or [
|
|
{"id": "matched", "name": "matched", "type": "json"},
|
|
{"id": "unmatched", "name": "unmatched", "type": "json"},
|
|
]
|
|
return [
|
|
helpers["write_json_artifact"](
|
|
run_id,
|
|
node_id,
|
|
output_dir,
|
|
f"{str(outputs[0].get('name', 'matched')).strip() or 'matched'}.json",
|
|
{"fieldPath": field_path, "operator": operator, "expected": expected, "items": matched, "count": len(matched)},
|
|
str(outputs[0].get("type", "json")),
|
|
output=outputs[0],
|
|
),
|
|
helpers["write_json_artifact"](
|
|
run_id,
|
|
node_id,
|
|
output_dir,
|
|
f"{str(outputs[1].get('name', 'unmatched')).strip() or 'unmatched'}.json",
|
|
{"fieldPath": field_path, "operator": operator, "expected": expected, "items": unmatched, "count": len(unmatched)},
|
|
str(outputs[1].get("type", "json")),
|
|
output=outputs[1],
|
|
),
|
|
]
|