Michael Hernandez

Devlog 0: Lunate - Lua in Go Using Struct Tags

TLDR

Lunate is a Go library that allows users easily map Lua tables to Go structs. This allows developers to easily add Lua as a extension / plugin / configuration language to a compiled Go application.

Example

main.go

package main

import (
	"log"

	"codeberg.org/hernandez/lunate"
)

type Config struct {
	Name     string            `lua:"name"`
	Debug    bool              `lua:"debug"`
}

func main() {
	var cfg Config
	if err := lunate.UnmarshalFile("cfg.lua", &cfg); err != nil {
		log.Fatal(err)
	}
}

cfg.lua

function file_exists(name)
    local f = io.open(name, "r")
    if f ~= nil then
        io.close(f)
        return true
    else
        return false
    end
end

return {
    name = "my-app",
    debug = file_exists("dev_credentials.json"),
}

Philosophy

Applications like Neovim and Wezterm have proven that a core demographics of users want powerful tools for extending applications. Although tools like json, yaml, and toml all have their place, sometimes Lua just needs to do the heavy lifting.

Given the low skill floor and high skill ceiling of Lua, it’s ideal for extending applications. The goal of Lunate is to lower the level of complexity it takes for a developer to include Lua as an option.

Note: Lunate only reads from Lua scripts. Lunate does not pass outputs back to Lua.