Skip to content

Introduction

The Lua language is an easy to use and high performance scripting language. The Fluxweave Platform provides a Lua API and scripting interface for easy access to Org data and for integrating with other systems.

Info

For security reasons the majority of the standard library has been disabled. Table features are still active.

Available libraries

Example

local ls = core.QueryDispatch("given trackers [journal]")

for index, dispatch in pairs(ls) do 
    log.info(dispatch.displayName)
end

Introduction to working with Lua

Simple numbers

local n = 0
n = n + 1
log.info(n)

Strings

local str = "Hello"
str = str .. ", world!"
log.info(str)

Arrays and objects

local arr = {"apples", "oranges"}
local obj = {foo = "hey", bar = "there"}

table.insert(arr, "kiwi")
log.info(arr)

obj.other = "hello"
log.info (obj)

Flow control

local test = 5
if test == 5 then
    log.info("its 5")
end

Arrays and objects passed as JSON arguments

Lua objects are automatically converted to objects. To force conversion to array add a _type element (it will be removed in the final data).

local arr1 = {_type="array", "apples", "oranges"}

local arr2 = {_type="array", {fruit="apples"}, {fruit="oranges"}}

local arr3 = {{fruit="apples"}, {fruit="oranges"}}
arr1 = ["apples", "oranges"]
arr2 = [{fruit: "apples"}, {fruit:"oranges"}]
arr3 = {
    0:{fruit: "apples"}, 
    1: {fruit:"oranges}
}

Working with Lua arrays and tables

Most functions take or return lua tables. These are map data structures that can be key value pair or simple lists where the key is an increasing integer.

To iterate a range

for n=1,20 do
   log.info(n)
end

To create an iterate an array

local numbers = {1, 5, 24, 44, 80}
for index, n in pairs(numbers) do 
    log.info(n)
end

To iterate a list

for index, item in pairs(list) do 
    -- do something with the item
end

To get and set values

local data = {foo="hello", bar="goodbye"}
log.info(data.foo)
log.info(data["bar"])
data["another"] = "hello again"

For more information about Lua check out the documentation.