condition builder

This commit is contained in:
lda
2026-04-28 18:24:09 +07:00 Verified
parent df4a808a73
commit 3d428c71db
5 changed files with 210 additions and 36 deletions
+9
View File
@@ -1,5 +1,7 @@
from .builder import WorkflowBuilder
from .catalog import NodeCatalog, NodeCatalogEntry
from .conditions import context, exists, input, state
from .mapping import bind_fields, bind_state, merge_maps
from .paths import GraphPath, context_path, graph_path, input_path, state_path
from .spec import NodeReturn, NodeSpec, build_registry, node
@@ -10,10 +12,17 @@ __all__ = [
"NodeReturn",
"NodeSpec",
"WorkflowBuilder",
"bind_fields",
"build_registry",
"bind_state",
"merge_maps",
"context",
"context_path",
"exists",
"graph_path",
"input",
"input_path",
"node",
"state",
"state_path",
]
+24 -14
View File
@@ -1,5 +1,6 @@
from __future__ import annotations
from collections.abc import Mapping
from dataclasses import dataclass, field
from typing import Any, Literal, TypeAlias
@@ -13,27 +14,32 @@ from wf_core import (
StateSchema,
Workflow,
)
from wf_core.model import Condition as CoreCondition
from .conditions import Expr, compile_condition
from .mapping import PathArg
from .paths import GraphPath
from .spec import NodeSpec
PathArg: TypeAlias = str | GraphPath
StepRef: TypeAlias = str | NodeUse | ConditionNode | ForeachNode | InterruptNode
MapArg: TypeAlias = Mapping[Any, Any]
def _normalize_path(path: PathArg) -> str:
if isinstance(path, GraphPath):
return path.value
return path
def _coerce_path(value: object) -> str:
if isinstance(value, str):
return value
if isinstance(value, GraphPath):
return value.value
raise TypeError(f"unsupported graph path value {value!r}")
def _normalize_mapping(
mapping: dict[PathArg, PathArg] | None,
mapping: MapArg | None,
) -> dict[str, str]:
if mapping is None:
return {}
return {
_normalize_path(source): _normalize_path(destination)
_coerce_path(source): _coerce_path(destination)
for source, destination in mapping.items()
}
@@ -60,8 +66,8 @@ class WorkflowBuilder:
spec: NodeSpec[Any, Any],
*,
id: str,
in_map: dict[PathArg, PathArg] | None = None,
out_map: dict[PathArg, PathArg] | None = None,
in_map: MapArg | None = None,
out_map: MapArg | None = None,
desc: str | None = None,
) -> NodeUse:
self.node_specs[spec.name] = spec
@@ -76,8 +82,12 @@ class WorkflowBuilder:
self.nodes.append(node)
return node
def condition(self, *, id: str, check: Any) -> ConditionNode:
node = ConditionNode(id=id, type="condition", check=check)
def condition(self, *, id: str, check: CoreCondition | Expr) -> ConditionNode:
node = ConditionNode(
id=id,
type="condition",
check=compile_condition(check),
)
self.nodes.append(node)
return node
@@ -94,7 +104,7 @@ class WorkflowBuilder:
{
"id": id,
"type": "foreach",
"over": _normalize_path(over),
"over": _coerce_path(over),
"as": as_,
"mode": mode,
"on_item_error": on_item_error,
@@ -108,8 +118,8 @@ class WorkflowBuilder:
*,
id: str,
kind: str,
request_map: dict[PathArg, PathArg] | None = None,
out_map: dict[PathArg, PathArg] | None = None,
request_map: MapArg | None = None,
out_map: MapArg | None = None,
outcomes: list[str] | None = None,
) -> InterruptNode:
node = InterruptNode(
+101
View File
@@ -0,0 +1,101 @@
from __future__ import annotations
from dataclasses import dataclass
from typing import Literal
from wf_core.model import (
BinaryCondition,
Condition,
ExistsCondition,
LiteralOperand,
NotCondition,
PathOperand,
VariadicCondition,
)
def _operand(value: object) -> PathOperand | LiteralOperand:
if isinstance(value, PathExpr):
return PathOperand(path=value.path)
return LiteralOperand(value=value)
@dataclass(frozen=True, slots=True)
class Expr:
condition: Condition
def __and__(self, other: object) -> Expr:
if not isinstance(other, Expr):
return NotImplemented
return Expr(VariadicCondition(op="and", args=[self.condition, other.condition]))
def __or__(self, other: object) -> Expr:
if not isinstance(other, Expr):
return NotImplemented
return Expr(VariadicCondition(op="or", args=[self.condition, other.condition]))
def __invert__(self) -> Expr:
return Expr(NotCondition(op="not", arg=self.condition))
def to_condition(self) -> Condition:
return self.condition
@dataclass(frozen=True, slots=True)
class PathExpr:
path: str
def _binary(self, op: Literal["eq", "ne", "gt", "lt"], other: object) -> Expr:
return Expr(
BinaryCondition(
op=op,
left=PathOperand(path=self.path),
right=_operand(other),
)
)
def eq(self, other: object) -> Expr:
return self._binary("eq", other)
def ne(self, other: object) -> Expr:
return self._binary("ne", other)
def gt(self, other: object) -> Expr:
return self._binary("gt", other)
def lt(self, other: object) -> Expr:
return self._binary("lt", other)
def __eq__(self, other: object) -> Expr: # ty: ignore[invalid-method-override]
return self._binary("eq", other)
def __ne__(self, other: object) -> Expr: # ty: ignore[invalid-method-override]
return self._binary("ne", other)
def __gt__(self, other: object) -> Expr:
return self.gt(other)
def __lt__(self, other: object) -> Expr:
return self.lt(other)
def state(field: str) -> PathExpr:
return PathExpr(path=f"state.{field}")
def input(field: str) -> PathExpr:
return PathExpr(path=f"input.{field}")
def context(field: str) -> PathExpr:
return PathExpr(path=f"context.{field}")
def exists(value: PathExpr) -> Expr:
return Expr(ExistsCondition(op="exists", path=value.path))
def compile_condition(value: Condition | Expr) -> Condition:
if isinstance(value, Expr):
return value.to_condition()
return value
+35
View File
@@ -0,0 +1,35 @@
from __future__ import annotations
from collections.abc import Mapping
from typing import TypeAlias
from .paths import GraphPath
PathArg: TypeAlias = str | GraphPath
def normalize_path(path: PathArg) -> str:
if isinstance(path, GraphPath):
return path.value
return path
def bind_fields(**mapping: PathArg) -> dict[str, str]:
return {
normalize_path(source): destination
for destination, source in mapping.items()
}
def bind_state(**mapping: PathArg) -> dict[str, str]:
return {
destination: normalize_path(target)
for destination, target in mapping.items()
}
def merge_maps(*maps: Mapping[str, str]) -> dict[str, str]:
merged: dict[str, str] = {}
for mapping in maps:
merged.update(mapping)
return merged