RuleGo RuleGo
🏠首页
  • 快速入门
  • 规则链
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 可视化
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • 触发器
  • 高级主题
  • 性能
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 组件市场
  • 概述
  • 快速入门
  • 路由
  • DSL
  • API
  • Options
  • 组件
🔥编辑器 (opens new window)
  • 可视化编辑器 (opens new window)
  • RuleGo-Server (opens new window)
  • ❓问答

    • FAQ
💖支持
👥加入社区
  • Github (opens new window)
  • Gitee (opens new window)
  • GitCode (opens new window)
  • 更新日志 (opens new window)
  • English
  • 简体中文
🏠首页
  • 快速入门
  • 规则链
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 可视化
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • 触发器
  • 高级主题
  • 性能
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 组件市场
  • 概述
  • 快速入门
  • 路由
  • DSL
  • API
  • Options
  • 组件
🔥编辑器 (opens new window)
  • 可视化编辑器 (opens new window)
  • RuleGo-Server (opens new window)
  • ❓问答

    • FAQ
💖支持
👥加入社区
  • Github (opens new window)
  • Gitee (opens new window)
  • GitCode (opens new window)
  • 更新日志 (opens new window)
  • English
  • 简体中文

广告采用随机轮播方式显示 ❤️成为赞助商
  • 快速入门

  • 规则链

  • 标准组件

    • 标准组件概述
    • 过滤器

    • 动作

    • 转换器

      • 脚本转换器
        • 配置
        • Relation Type
        • 执行结果
        • 配置示例
        • 应用示例
      • 表达式转换器
      • 元数据转换器
      • 模板解析器
    • 外部的

    • 流

  • 扩展组件

  • 自定义组件

  • 组件市场

  • 可视化

  • AOP

  • 触发器

  • 高级主题

  • RuleGo-Server

  • 问题

目录

脚本转换器

jsTransform组件:脚本转换器。用于使用JavaScript脚本对消息进行转换和处理,可以灵活地修改msg、metadata和msgType的内容,实现数据转换、格式转换、数据增强等功能。

JavaScript脚本支持ECMAScript 5.1(+)语法规范和部分ES6规范,包括async/await/Promise/let等特性。支持调用Go自定义函数扩展功能,详见udf。

# 配置

字段 类型 说明 默认值
jsScript string JavaScript转换脚本 无
  • jsScript:JavaScript转换脚本,用于处理消息。该字段作为以下函数的函数体:

    function Transform(msg, metadata, msgType) {
        ${jsScript}
    }
    
    1
    2
    3

    参数说明:

    • msg: 消息内容
      • 当dataType=JSON时为jsonObject类型,可通过msg.field方式访问字段
      • 其他dataType时为string类型
    • metadata: 消息元数据,jsonObject类型
    • msgType: 消息类型

    返回值:

    • 必须返回包含转换后msg、metadata、msgType的对象:
      return {
          'msg': msg,        // 转换后的消息内容
          'metadata': metadata,  // 转换后的元数据
          'msgType': msgType    // 转换后的消息类型
      };
      
      1
      2
      3
      4
      5

注意

  1. 脚本执行有超时限制,通过config.ScriptMaxExecutionTime配置

# Relation Type

  • Success: 脚本执行成功,转换后的消息发送到Success链路
  • Failure: 以下情况消息发送到Failure链路:
    • 脚本语法错误
    • 脚本执行异常
    • 脚本执行超时
    • 返回值格式错误

# 执行结果

组件通过执行JavaScript脚本对消息进行转换:

  • 可以修改msg内容
  • 可以修改/添加metadata
  • 可以修改msgType
  • 转换后的完整消息传递给下一个节点

# 配置示例

  {
    "id": "s1",
    "type": "jsTransform",
    "name": "转换",
    "configuration": {
      "jsScript": "metadata['name']='test01';\n metadata['index']=11;\n msg['addField']='addValue1'; return {'msg':msg,'metadata':metadata,'msgType':msgType};"
    }
  }
1
2
3
4
5
6
7
8

# 应用示例

把消息进行转换后再执行后续逻辑。

{
  "ruleChain": {
    "id":"rule01",
    "name": "测试规则链",
    "root": true
  },
  "metadata": {
    "nodes": [
       {
        "id": "s1",
        "type": "jsTransform",
        "name": "转换",
        "configuration": {
          "jsScript": "metadata['name']='test02';\n metadata['index']=22;\n msg['addField']='addValue2'; return {'msg':msg,'metadata':metadata,'msgType':msgType};"
        }
      },
      {
        "id": "s2",
        "type": "restApiCall",
        "name": "推送数据",
        "configuration": {
          "restEndpointUrlPattern": "http://192.168.136.26:9099/api/msg",
          "requestMethod": "POST",
          "maxParallelRequestsCount": 200
        }
      }
    ],
    "connections": [
      {
        "fromId": "s1",
        "toId": "s2",
        "type": "Success"
      }
    ]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
在 GitHub 上编辑此页 (opens new window)
上次更新: 2025/04/02, 01:29:50
汇聚
表达式转换器

← 汇聚 表达式转换器→

Theme by Vdoing | Copyright © 2023-2025 RuleGo Team | Apache 2.0 License

  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式