Flatbuffers code reflection based generation tool with scripting support.
Flatbuffers is awesome, but you'll eventually need to (or wish you could) write additional code generation steps for your schema files. For example, generating a network protocol handler/dispatcher for you client and server and keep them in sync.
flatt exposes a lua runtime with some additional helper functions to help you deal with Flatbuffers schema files, reflection and template rendering. You can for example write a flatt script that will reflect your schema, manipulate that data, and render (using a template engine or not) a file with the content you just processed.
All builtin lua libraries are exported and you can use luarocks packages with it if you want to.
An example of what a flatt script looks like:
-- luarocks install lunajson
local lunajson = require("lunajson")
flatt.log.info("Generating headers...")
flatt.flatc({ "--cpp", "./schema.fbs" })
local json = flatt.reflect("./schema.fbs")
flatt.log.trace(json)
--[[
{
"tables": [
{
"id": 182975129,
"name": "MyTable",
"namespace": "my.namespace",
"fields": [
{
"name": "some_field",
...
}
]
}
...
],
"structs": [
...
],
...
}
]]
local info = lunajson.decode(json)
-- generate a file using `info` data
npm install -g flatt
- Windows (PRs welcome to add Linux+WSL and Mac support)
flatt some/project.lua
scoop install cmakescoop install charm-gumscoop install just
npm install && npm run build
Has the flatt executable directory.
Project directory (from the given project script file).
A table (list) of arguments passed next to the script file.
$ flatt project.lua --hello --world abc 123
flatt.vars.argv
-- { "--hello", "--world", "abc", "123" }
flatt.vars.argv[1]
-- { "--hello" }-- `luarocks install argparse`
local argparse = require("argparse")
local parser = argparse("example", "Arguments example.")
parser:flag("-h --hello", "Hello argument.", "")
parser:flag("-w --world", "World argument.", "")
parser:argument("a", "Some value")
parser:argument("b", "Some value")
local args = parser:parse(flatt.vars.argv)
args.hello
-- true
args.world
-- false
args.a
-- "abc"
args.b
-- "123"flatt.flatc({ "--cpp", "schema.fbs" })
-- return: the exit codeflatt.shell("echo", { "hello", "world" })
-- return: the exit codeflatt.reflect("schema.fbs")
--[[
{
// JSON encoded schema
}
]]Functions
tracedebuginfowarnerrorcritical
flatt.log.set_level("info")
-- Sets the current logging levelflatt.log.get_level()
-- Gets the current logging levelflatt.log.trace("log line")
-- Writes to console in white (windows)flatt.log.trace("log line")
-- Writes to console in white (windows)flatt.log.debug("log line")
-- Writes to console in blue (windows)flatt.log.info("log line")
-- Writes to console in green (windows)flatt.log.warn("log line")
-- Writes to console in yellow (windows)flatt.log.error("log line")
-- Writes to console in red (windows)flatt.log.critical("log line")
-- Writes to console in white with red background (windows)Functions
flatt.dir.read("hello.txt", "Hello world!)
-- return: "Hello world!"flatt.dir.read("hello.txt")
-- return: "Hello world!"flatt.dir.exists("./non-existing-file.ext")
-- return: false
flatt.dir.exists("hello.txt")
-- return: trueflatt.dir.hash("./non-existing-file.ext")
-- return: nil
flatt.dir.hash("some-file.ext")
-- return: "EDCBA5DD333A60F3C98452672A1AB1711409040D"Functions
flatt.dir.hash("./non-existing-dir")
-- return: nil
flatt.dir.hash(".")
-- return: "4CF8ADEDB3B43E78645E4DE673D2D7DD4CFADA58"Builtin provided template engine functions (inja).
Use aspect for a more Lua friendly template system.
Functions
Accepts JSON encoded string for data.
flatt.templates.render_string("hello {{name}}", "{\"name\":\"world\"}")
-- return: "hello world"Accepts JSON encoded string for data.
flatt.templates.render_string("template.txt", "output.txt", "{\"name\":\"world\"}")
-- return: trueFunctions
flatt.string.pad_left("1234", 8)
-- return: " 1234"
flatt.string.pad_left("1234", 8, "0")
-- return: "00001234"flatt.string.pad_left("1234", 8)
-- return: "1234 "
flatt.string.pad_right("1234", 8, "0")
-- return: "12340000"flatt.string.starts_with("hello world", "hello")
-- return: true
flatt.string.starts_with("hello world", "world")
-- return: falseflatt.string.ends_with("hello world", "hello")
-- return: false
flatt.string.ends_with("hello world", "world")
-- return: trueflatt.string.split("hello world", " ")
-- return: {"hello", "world"}
flatt.string.split("is this real life", " ", 3)
-- return: {"is", "this", "real life"}flatt.string.trim(" hello world ")
-- return: "hello world"flatt.string.trim(" hello world ")
-- return: "hello world "flatt.string.trim(" hello world")
-- return: "hello world "Splits the string whenever one of the following characters is found:
. -_|/\
flatt.string.explode("a.b c-d|e/f\\g")
-- return: { "a", "b", "c", "d", "e", "f", "g" }flatt.string.join({ "a", "b", "c", "d", "e", "f", "g" }, "~")
-- return: "a~b~c~d~e~f~g"flatt.string.lower_case("NewGUIAlertBox")
-- return: "newguialertbox"flatt.string.upper_case("NewGUIAlertBox")
-- return: "NEWGUIALERTBOX"flatt.string.ucfirst("NewGUIAlertBox")
-- return: "NewGUIAlertBox"flatt.string.lcfirst("NewGUIAlertBox")
-- return: "newGUIAlertBox"flatt.string.snake_case("NewGUIAlertBox")
-- return: "new_gui_alert_box"flatt.string.kebab_case("NewGUIAlertBox")
-- return: "new-gui-alert-box"flatt.string.pascal_case("NewGUIAlertBox")
-- return: "NewGuiAlertBox"flatt.string.camel_case("NewGUIAlertBox")
-- return: "newGuiAlertBox"flatt.string.const_case("NewGUIAlertBox")
-- return: "NEW_GUI_ALERT_BOX"flatt.string.train_case("NewGUIAlertBox")
-- return: "New-Gui-Alert-Box"flatt.string.ada_case("NewGUIAlertBox")
-- return: "New_Gui_Alert_Box"flatt.string.cobol_case("NewGUIAlertBox")
-- return: "NEW-GUI-ALERT-BOX"flatt.string.dot_case("NewGUIAlertBox")
-- return: "new.gui.alert.box"flatt.string.path_case("NewGUIAlertBox")
-- return: "new/gui/alert/box"flatt.string.space_case("NewGUIAlertBox")
-- return: "new gui alert box"flatt.string.capital_case("NewGUIAlertBox")
-- return: "New Gui Alert Box"flatt.string.cpp_case("NewGUIAlertBox")
-- return: "new::gui::alert::box"