luaTransform
luaTransform
component: lua script transformer. It can use Lua script to transform or enhance msg, metadata, and msgType. Then it passes the transformed message to the next node.You can also load lua third-party libraries to perform advanced operations such as encryption, decryption, io, network, file, etc.
Advanced operations such as encryption and decryption, I/O, network, and file need to be enabled by setting:
config.Properties.PutValue(luaEngine.LoadLuaLibs, "true")
Lua script supports Lua 5.1 syntax specification, please refer to gopher-lua (opens new window).
# Configuration
Field | Type | Description | Default |
---|---|---|---|
script | string | lua script, or lua script file path with .lua suffix | None |
script
: It can transform msg, metadata, and msgType. Only need to provide the function body content, if it is a file path, you need to provide the complete script function:function Transform(msg, metadata, msgType) ${script} end
1
2
3- msg: message content, if dataType=JSON, you can use
msg.temperature
to operate. If dataType is other types, the field type is:string
- metadata: message metadata, type:
jsonObject
- msgType: message type
- Function return type:
msg, metadata, msgType
- msg: message content, if dataType=JSON, you can use
# Relation Type
- True: Send the message to the
True
chain - False: Send the message to the
False
chain - Failure: Execution failed, send the message to the
Failure
chain
# Execution Result
Change the msg
, metadata
, and msgType
content through the script.
# Configuration Example
{
"id": "s1",
"type": "x/luaTransform",
"name": "luaTransform",
"configuration": {
"script": " -- Convert the temperature value from Celsius to Fahrenheit
msg.temperature = msg.temperature * 1.8 + 32
-- Add a field in 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
# 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