luaTransform
luaTransform
Component: Lua Script Transformer. It can transform or enhance msg
, metadata
, msgType
, and dataType
using Lua scripts. The transformed message is then passed to the next node. 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 transformmsg
,metadata
, andmsgType
. Only the function body content is required. If it is a file path, a complete script function must be provided:function Transform(msg, metadata, msgType, dataType) ${script} end
1
2
3- msg: Message content. If dataType=JSON, it can be accessed and modified using
msg.temperature
. If dataType is of another type, the field type isstring
. - 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:
msg, metadata, msgType
- msg: Message content. If dataType=JSON, it can be accessed and modified using
Data Type Handling
- JSON Type: The
msg
parameter is a Lua table, and fields can be accessed and modified directly, such asmsg.temperature = 30
. - BINARY Type: The
msg
parameter is a Lua table (byte array), and the first byte can be accessed usingmsg[1]
(1-based index). A new byte array can be returned. - Other Types: The
msg
parameter is a string. - Returning Binary Data: Return a Lua table containing numbers (0-255), such as
{0xAA, 0xBB, 0x01, 0x02}
.
# 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
The script modifies the content of msg
, metadata
, and msgType
.
# Configuration Examples
# Basic Transformation Example
{
"id": "s1",
"type": "x/luaTransform",
"name": "Transformation",
"configuration": {
"script": " -- Convert temperature from Celsius to Fahrenheit
msg.temperature = msg.temperature * 1.8 + 32
-- Add a field to metadata to indicate the temperature unit
metadata.unit = \"F\"
metadata.from = global.from
metadata.add = add(5,4)
return msg, metadata, msgType"
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Data Type Handling Example
{
"id": "s2",
"type": "x/luaTransform",
"name": "Smart Transformation",
"configuration": {
"script": "metadata.originalType = dataType; if dataType == 'JSON' then msg.processed = true; msg.timestamp = os.date('%Y-%m-%d %H:%M:%S') elseif dataType == 'BINARY' then metadata.binaryLength = #msg elseif dataType == 'TEXT' then metadata.textLength = string.len(msg) end; return msg, metadata, msgType"
}
}
2
3
4
5
6
7
8
# Adding a Binary Header Example
{
"id": "s3",
"type": "x/luaTransform",
"name": "Add Binary Header",
"configuration": {
"script": "if dataType == 'BINARY' then local result = {0xAA, 0xBB}; for i = 1, #msg do table.insert(result, msg[i]) end; metadata.headerAdded = 'true'; return result, metadata, msgType end; return msg, metadata, msgType"
}
}
2
3
4
5
6
7
8
# Binary Data Encryption Example
{
"id": "s4",
"type": "x/luaTransform",
"name": "XOR Encryption",
"configuration": {
"script": "if dataType == 'BINARY' then local result = {}; for i = 1, #msg do table.insert(result, msg[i] ~ 0x55) end; metadata.encrypted = 'true'; metadata.algorithm = 'xor'; return result, metadata, msgType end; return msg, metadata, msgType"
}
}
2
3
4
5
6
7
8
# Device Protocol Parsing Example
{
"id": "s5",
"type": "x/luaTransform",
"name": "Protocol Parsing",
"configuration": {
"script": "if dataType == 'BINARY' and #msg >= 6 then local deviceId = msg[1] * 256 + msg[2]; local functionCode = msg[3] * 256 + msg[4]; local dataLength = msg[5] * 256 + msg[6]; local payload = {}; for i = 7, #msg do table.insert(payload, msg[i]) end; local result = {deviceId = deviceId, functionCode = functionCode, dataLength = dataLength, payload = payload, parsedAt = os.date('%Y-%m-%d %H:%M:%S')}; metadata.protocol = 'custom'; metadata.parsed = 'true'; return result, metadata, 'PARSED_DATA' end; return msg, metadata, msgType"
}
}
2
3
4
5
6
7
8
# Text Log Parsing Example
{
"id": "s6",
"type": "x/luaTransform",
"name": "Log Parsing",
"configuration": {
"script": "if dataType == 'TEXT' then local pattern = '(%d%d%d%d%-%d%d%-%d%d %d%d:%d%d:%d%d) %[([%w]+)%] (.+)'; local timestamp, level, message = string.match(msg, pattern); if timestamp then local result = {timestamp = timestamp, level = level, message = message, source = 'application'}; metadata.logParsed = 'true'; return result, metadata, 'LOG_ENTRY' end end; return msg, metadata, msgType"
}
}
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)
})
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")
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