A comprehensive guide to using Glazed commands within Lua scripts through the Glazed Lua wrapper
The Glazed Lua wrapper provides an interface for executing Glazed commands within Lua scripts. This guide covers the key components, usage patterns, and data conversion utilities available in the wrapper.
func CallGlazedCommandFromLua(L *lua.LState, cmd cmds.GlazeCommand, luaTable *lua.LTable) (*types.Table, error)
Executes a GlazeCommand with fields from a Lua table.
func CallGlazedBareCommandFromLua(L *lua.LState, cmd cmds.BareCommand, luaTable *lua.LTable) error
Executes a BareCommand with fields from a Lua table.
func CallGlazedWriterCommandFromLua(L *lua.LState, cmd cmds.WriterCommand, luaTable *lua.LTable) (string, error)
Executes a WriterCommand with fields from a Lua table.
func RegisterGlazedCommand(L *lua.LState, cmd interface{})
Registers a Glazed command (GlazeCommand, BareCommand, or WriterCommand) in the Lua state.
func ParseNestedLuaTableMiddleware(L *lua.LState, luaTable *lua.LTable) middlewares.Middleware
Middleware to parse nested Lua tables into Values.
func LuaValueToInterface(L *lua.LState, value lua.LValue) interface{}
Converts a Lua value to a Go interface{}.
func InterfaceToLuaValue(L *lua.LState, value interface{}) lua.LValue
Converts a Go interface{} to a Lua value.
func GlazedTableToLuaTable(L *lua.LState, glazedTable *types.Table) *lua.LTable
Converts a Glazed table to a Lua table.
func ParseLuaTableToSection(L *lua.LState, luaTable *lua.LTable, section schema.Section) (*values.SectionValues, error)
Parses a Lua table into a SectionValues.
func ParseNestedLuaTableToValues(L *lua.LState, luaTable *lua.LTable, schema_ *schema.Schema) (*values.Values, error)
Parses a nested Lua table into Values.
func ParseFieldFromLua(L *lua.LState, value lua.LValue, paramDef *fields.Definition) (interface{}, error)
Parses a Lua value into a Go value based on the field definition.
First, create a new Lua state:
L := lua.NewState()
defer L.Close()
Register your Glazed command with the Lua state:
animalListCmd, _ := NewAnimalListCommand()
lua2.RegisterGlazedCommand(L, animalListCmd)
This registers the command and creates:
command_name_params)Once registered, you can call the command from Lua:
local params = {
default = {
count = 3
},
glazed = {
fields = {"animal", "diet"}
}
}
local result = animal_list(params)
The registration process creates a global Lua table with field information:
for section_name, section_fields in pairs(animal_list_params) do
print("Section: " .. section_name)
for field_name, field_info in pairs(section_fields) do
print(string.format(" %s (%s): %s", field_name, field_info.type, field_info.description))
print(string.format(" Default: %s", tostring(field_info.default)))
print(string.format(" Required: %s", tostring(field_info.required)))
end
end
The wrapper supports nested Lua tables for complex field structures:
local params = {
default = {
count = 3
},
glazed = {
fields = {"animal", "diet"},
format = "table"
},
output = {
file = "animals.txt"
}
}
local result = animal_list(params)
The wrapper handles automatic conversion between Lua and Go types:
L := lua.NewState()
defer L.Close()
if err := L.DoString(script); err != nil {
fmt.Printf("Error executing Lua script: %v\n", err)
}
Use type assertions when handling return values from Lua functions
Structure your field tables to match the expected section organization
Leverage the field information tables for runtime validation and documentation