Skip to content

condition · 条件 ​

求一个布尔表达式,把运行推向 true 边或 false 边——工作流里的 if。

输出:{"result": true}(或 false)

在画布上怎么配 ​

condition 节点:控制台工作流编排中的画布与节点配置

截图来自控制台「工作流编排」:选择本页示例对应的路由,点击画布上的节点,右侧就是它的配置表单。点击图片可看大图。

  • 表达式:求值必须是布尔值,例如 priority == 'urgent'。
  • 画布上的两条出边标签必须是 true / false,写错标签保存会被校验拦下。

什么时候用 ​

  • 按消息属性分流:priority 是 urgent 就走电话/短信,否则走 IM。
  • 按 payload 字段判断:payload.severity == 'P1'、payload.env != 'prod'。
  • 按上游节点结果决定后续:nodes.apply.ok == true(上游是 script 或 condition 时)。
  • 想在求值出错时兜底:condition 也允许 error 出边,表达式求值失败时节点失败、由兜底分支接手。
  • 多值分流(P0/P1/P2/其他)不要堆 condition——用 switch。

字段 ​

字段类型必填默认说明
exprstring是—expr-lang 表达式,必须求值为 bool。缺了校验 400:condition 节点 "c1" 缺少 with.expr。

expr 的求值环境与其它节点一致:payload、variables、params、priority、target、item、index、nodes(详见表达式与数据流)。priority 是字符串(low / normal / high / urgent),payload 是这条消息的结构化负载,params 是 payload 覆盖 spec.params 默认值后的结果。

最小示例 ​

priority == 'urgent' 为真走短信,为假走钉钉。

json
{ "id": "c1", "type": "condition", "with": { "expr": "priority == 'urgent'" } }

放进路由的 workflow:

json
{
  "trigger": { "type": "api" },
  "nodes": [
    { "id": "c1", "type": "condition", "with": { "expr": "priority == 'urgent'" } },
    { "id": "s_sms", "type": "send", "with": { "channel": "ch-sms-oncall" } },
    { "id": "s_im", "type": "send", "with": { "channel": "ch-dingtalk-ops" } }
  ],
  "edges": [
    { "from": "c1", "to": "s_sms", "when": "true" },
    { "from": "c1", "to": "s_im", "when": "false" }
  ]
}

输出与下游引用 ​

引用含义
nodes.c1.result本次求值结果(bool),断点续跑时边就是按它重放的
nodes.c1.output.result等价写法

出边标签是硬约束:

标签何时激活
trueresult 为真
falseresult 为假
error节点失败(表达式求值失败等),目标必须是 error-handler

不写标签是 400:边 c1→s_sms 的标签 "" 非法(condition 出边仅 true|false|error)。

完整示例 ​

优先级 + payload 字段混合判断,true 分支等回执,false 分支走 IM;两条分支加上 condition 自身都接 error 兜底。

json
{
  "trigger": { "type": "api" },
  "params": [
    { "name": "severity", "type": "string", "required": true }
  ],
  "nodes": [
    { "id": "urgent", "type": "condition", "with": {
      "expr": "priority == 'urgent' || payload.severity == 'P1'" } },
    { "id": "page", "type": "send", "with": { "channel": "ch-sms-oncall", "await": true } },
    { "id": "im", "type": "send", "with": { "channel": "ch-dingtalk-ops" } },
    { "id": "onfail", "type": "error-handler" },
    { "id": "ticket", "type": "send", "with": { "channel": "ch-webhook-ticket" } }
  ],
  "edges": [
    { "from": "urgent", "to": "page", "when": "true" },
    { "from": "urgent", "to": "im", "when": "false" },
    { "from": "urgent", "to": "onfail", "when": "error" },
    { "from": "page", "to": "onfail", "when": "error" },
    { "from": "onfail", "to": "ticket" }
  ],
  "limits": { "deadline": "10m" }
}

三条错误路径汇到同一个 onfail:urgent 求值失败、page 等回执拿到死信、im 投递失败。ticket 只在兜底分支里执行,正常路径不会误发。

行为细节与坑 ​

  • 校验期就收紧类型:表达式按 expr.AsBool() 编译,能静态判定为非 bool 的直接 400(节点 "c1": 表达式 "…" 编译失败: …)。
  • 运行期再判一次:动态值(如 payload.flag)拿到非 bool 结果 → 节点失败,错误文本 表达式 "…" 结果不是 bool(得到 <type>)。
  • 求值失败 = 节点失败:拼错的字段访问也可能在运行期炸(例如对 nil 做链式取字段),错误形如 表达式 "nodes.up.ok" 求值失败: …。没有 error 边 → 整次运行失败。
  • 可以只写一条边:只画 true 边,false 时节点记为 succeeded、没有出边激活,下游不执行——不算失败,运行照常成功。
  • 别引用可能被跳过的上游:nodes.<id> 只在该节点产出过输出时才存在,先判空再取字段,例如 nodes.up != nil && nodes.up.result == true。
  • with 整体缺失也会报错:节点 "c1" 缺少 with(先于 expr 检查)。
  • 边不能自环:边 c1→c1 不能指向自身;condition 出边也不要指向自己形成环,校验会报 图中存在环: …。

相关 ​

基于 Apache License 2.0 发布