RuleGo RuleGo
🏠Home
  • Quick Start
  • Rule Chain
  • Standard Components
  • Extension Components
  • Custom Components
  • Visualization
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • Trigger
  • Advanced Topics
  • Performance
  • Standard Components
  • Extension Components
  • Custom Components
  • Components Marketplace
  • Overview
  • Quick Start
  • Routing
  • DSL
  • API
  • Options
  • Components
🔥Editor (opens new window)
  • RuleGo Editor (opens new window)
  • RuleGo Server (opens new window)
  • Github (opens new window)
  • Gitee (opens new window)
  • Changelog (opens new window)
  • English
  • 简体中文
🏠Home
  • Quick Start
  • Rule Chain
  • Standard Components
  • Extension Components
  • Custom Components
  • Visualization
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • Trigger
  • Advanced Topics
  • Performance
  • Standard Components
  • Extension Components
  • Custom Components
  • Components Marketplace
  • Overview
  • Quick Start
  • Routing
  • DSL
  • API
  • Options
  • Components
🔥Editor (opens new window)
  • RuleGo Editor (opens new window)
  • RuleGo Server (opens new window)
  • Github (opens new window)
  • Gitee (opens new window)
  • Changelog (opens new window)
  • English
  • 简体中文

广告采用随机轮播方式显示 ❤️成为赞助商
  • Quick Start

  • Rule Chain

  • Standard Components

  • Extension Components

    • Extension Components Overview
    • filter

      • luaFilter
        • Configuration
        • Relation Type
        • Execution Result
        • Configuration Examples
          • Basic JSON Filtering Example
          • Data Type Detection Example
          • Binary Data Filtering Example
          • Device Protocol Filtering Example
          • Text Content Filtering Example
        • Custom Functions
        • Loading Lua Modules
    • transform

    • external

    • ai

    • ci

    • IoT

  • Custom Components

  • Components marketplace

  • Visualization

  • AOP

  • Trigger

  • Advanced Topic

  • RuleGo-Server

  • FAQ

  • Endpoint Module

  • Support

目录

luaFilter

luaFilter Component: Lua Script Filter. It can filter msg, metadata, and msgType using Lua scripts. The message is routed to the True or False chain based on the script's return value. It can also load third-party Lua libraries to perform advanced operations such as encryption/decryption, I/O, networking, and file handling.

To enable advanced operations such as encryption/decryption, I/O, networking, and file handling in Lua scripts, the following configuration is required: config.Properties.PutValue(luaEngine.LoadLuaLibs, "true")

Lua scripts support Lua 5.1 syntax. For more details, refer to gopher-lua (opens new window).

# Configuration

Field Type Description Default Value
script string Lua script or file path of a Lua script with a .lua suffix None
  • script: This can filter msg, metadata, and msgType. Only the function body content is required. If it is a file path, a complete script function must be provided:

        function Filter(msg, metadata, msgType, dataType) 
            ${script} 
         end
    
    1
    2
    3
    • msg: Message content. If dataType=JSON, it can be accessed using msg.temperature. If dataType is of another type, the field type is string.
    • metadata: Message metadata, type: jsonObject.
    • msgType: Message type.
    • dataType: Message data type (JSON, TEXT, BINARY, etc.), which needs to be used as a string in Lua.
    • Return value type: boolean, which determines the connection to the next node (Relation Type).

Data Type Handling

  • JSON Type: The msg parameter is a Lua table, and fields can be accessed directly, such as msg.temperature.
  • BINARY Type: The msg parameter is a Lua table (byte array), and the first byte can be accessed using msg[1] (1-based index).
  • Other Types: The msg parameter is a string.

# Relation Type

  • True: Send the message to the True chain.
  • False: Send the message to the False chain.
  • Failure: In case of execution failure, send the message to the Failure chain.

# Execution Result

This component does not modify the content of msg, metadata, and msgType.

# Configuration Examples

# Basic JSON Filtering Example

  {
    "id": "s1",
    "type": "x/luaFilter",
    "name": "Filter",
    "configuration": {
      "script": "return msg.temperature > 50"
    }
  }
1
2
3
4
5
6
7
8

# Data Type Detection Example

  {
    "id": "s2", 
    "type": "x/luaFilter",
    "name": "Data Type Filtering",
    "configuration": {
      "script": "if dataType == 'JSON' then return msg.temperature > 25 elseif dataType == 'BINARY' then return #msg > 10 else return string.len(msg) > 5 end"
    }
  }
1
2
3
4
5
6
7
8

# Binary Data Filtering Example

  {
    "id": "s3",
    "type": "x/luaFilter", 
    "name": "Binary Header Check",
    "configuration": {
      "script": "return dataType == 'BINARY' and #msg >= 2 and msg[1] == 255 and msg[2] == 254"
    }
  }
1
2
3
4
5
6
7
8

# Device Protocol Filtering Example

  {
    "id": "s4",
    "type": "x/luaFilter",
    "name": "Device Data Filtering", 
    "configuration": {
      "script": "if dataType == 'BINARY' and #msg >= 4 then local deviceId = msg[1] * 256 + msg[2]; local functionCode = msg[3] * 256 + msg[4]; return deviceId == 4097 and (functionCode == 1 or functionCode == 2) else return false end"
    }
  }
1
2
3
4
5
6
7
8

# Text Content Filtering Example

  {
    "id": "s5",
    "type": "x/luaFilter",
    "name": "Log Level Filtering",
    "configuration": {
      "script": "return dataType == 'TEXT' and (string.find(msg, 'ERROR') ~= nil or string.find(msg, 'WARN') ~= nil)"
    }
  }
1
2
3
4
5
6
7
8

# Custom Functions

You can register golang functions with config.RegisterUdf and use them in Lua scripts. The registration method and usage are the same as Js. For details, refer to Udf.

# Loading Lua Modules

You can extend Lua capabilities by loading Lua custom or third-party module libraries. Use the following method to register Lua modules to the engine:

import luaEngine "github.com/rulego/rulego-components/pkg/lua_engine"

luaEngine.Preloader.Register(func(state *lua.LState) {
	//load module
	//libs.Preload(state)
})
1
2
3
4
5
6

Creating a module by Go (opens new window)

In addition, the framework has built-in a large number of Lua third-party library modules. Use the following method to enable them, which are not loaded by default:

config.Properties.PutValue(luaEngine.LoadLuaLibs, "true")
1

After starting, you can perform advanced operations such as encryption/decryption, I/O, network, database, file, etc. The list of third-party libraries is:

  • argparse (opens new window) argparse CLI parsing https://github.com/luarocks/argparse (opens new window)
  • base64 (opens new window) encoding/base64 (opens new window) api
  • cloudwatch (opens new window) aws cloudwatch log access
  • cert_util (opens new window) monitoring ssl certs
  • chef (opens new window) chef client api
  • cmd (opens new window) cmd port
  • crypto (opens new window) calculate md5, sha256 hash for string
  • db (opens new window) access to databases
  • filepath (opens new window) path.filepath port
  • goos (opens new window) os port
  • http (opens new window) http.client && http.server
  • humanize (opens new window) humanize github.com/dustin/go-humanize (opens new window) port
  • inspect (opens new window) pretty print github.com/kikito/inspect.lua (opens new window)
  • ioutil (opens new window) io/ioutil port
  • json (opens new window) json implementation
  • log (opens new window) log port
  • plugin (opens new window) run lua code in lua code
  • pprof (opens new window) pprof http-server for golang from lua
  • prometheus (opens new window) prometheus exporter
  • regexp (opens new window) regexp port
  • runtime runtime port
  • pb (opens new window) https://github.com/cheggaaa/pb (opens new window) port (v3)
  • shellescape (opens new window) shellescape https://github.com/alessio/shellescape (opens new window) port
  • stats (opens new window) stats https://github.com/montanaflynn/stats (opens new window) port
  • storage (opens new window) package for store persist data and share values between lua states
  • strings (opens new window) strings port (utf supported)
  • tac (opens new window) tac line-by-line scanner (from end of file to up)
  • tcp (opens new window) raw tcp client lib
  • telegram (opens new window) telegram bot
  • template (opens new window) template engines
  • time (opens new window) time port
  • xmlpath (opens new window) gopkg.in/xmlpath.v2 (opens new window) port
  • yaml (opens new window) gopkg.in/yaml.v2 (opens new window) port
  • zabbix (opens new window) zabbix bot
Edit this page on GitHub (opens new window)
Last Updated: 2025/06/23, 10:45:49
Extension Components Overview
luaTransform

← Extension Components Overview luaTransform→

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

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