add match!

This commit is contained in:
lda
2026-05-19 06:43:05 +07:00 Verified
parent 66db83dc5a
commit 6ad3d20975
6 changed files with 137 additions and 4 deletions
+4
View File
@@ -11,6 +11,8 @@ from .models import (
DraftForeachStep,
DraftInterruptStep,
DraftJoinStep,
DraftMatchCase,
DraftMatchStep,
DraftWhenStep,
DraftUseStep,
WorkflowDraft,
@@ -23,6 +25,8 @@ __all__ = [
"DraftForeachStep",
"DraftInterruptStep",
"DraftJoinStep",
"DraftMatchCase",
"DraftMatchStep",
"DraftWhenStep",
"DraftUseStep",
"WorkflowDraft",
+10 -1
View File
@@ -1,16 +1,18 @@
from __future__ import annotations
from wf_authoring import WorkflowBuilder
from wf_authoring.dsl import PathExpr
from wf_core import JoinNode, Workflow
from .models import (
DraftChooseStep,
DraftForeachStep,
DraftInterruptStep,
DraftJoinStep,
DraftMatchStep,
DraftStep,
DraftUseStep,
DraftWhenStep,
DraftChooseStep,
WorkflowDraft,
)
@@ -79,4 +81,11 @@ def _add_step(builder: WorkflowBuilder, step_id: str, step: DraftStep):
id=step_id,
default=step.choose.default,
).entry
if isinstance(step, DraftMatchStep):
return builder.match(
PathExpr(step.match.value),
{case.equals: case.then for case in step.match.cases},
id=step_id,
default=step.match.default,
).entry
raise TypeError(f"unsupported draft step {type(step)!r}")
+25 -1
View File
@@ -7,7 +7,9 @@ from pydantic import BaseModel, Field, model_validator
from wf_core.models.conditions import Condition
JsonObject = dict[str, Any]
STEP_KIND_KEYS = frozenset({"use", "foreach", "interrupt", "join", "when", "choose"})
STEP_KIND_KEYS = frozenset(
{"use", "foreach", "interrupt", "join", "when", "choose", "match"}
)
class DraftUseStep(BaseModel):
@@ -91,6 +93,27 @@ class DraftChooseStep(BaseModel):
choose: DraftChoosePayload
class DraftMatchCase(BaseModel):
"""One ordered equality case in a draft match decision."""
equals: Any
then: str
class DraftMatchPayload(BaseModel):
"""Payload for matching one graph value against ordered equality cases."""
value: str
cases: list[DraftMatchCase] = Field(min_length=1)
default: str = "__end__"
class DraftMatchStep(BaseModel):
"""Draft step that delegates equality decisions to `WorkflowBuilder.match`."""
match: DraftMatchPayload
DraftStep = (
DraftUseStep
| DraftForeachStep
@@ -98,6 +121,7 @@ DraftStep = (
| DraftJoinStep
| DraftWhenStep
| DraftChooseStep
| DraftMatchStep
)