Fix up some miscellaneous shit around the project regarding lua conversions

This commit is contained in:
2025-03-25 17:46:21 +01:00
parent 1bcc6735ab
commit 15ae116447
4 changed files with 101 additions and 85 deletions

View File

@@ -2,7 +2,7 @@ package processor
import (
"fmt"
"reflect"
"strconv"
"strings"
lua "github.com/yuin/gopher-lua"
@@ -52,65 +52,75 @@ func NewLuaState() (*lua.LState, error) {
return L, nil
}
// ToLuaTable converts a struct or map to a Lua table recursively
func ToLuaTable(L *lua.LState, data interface{}) (*lua.LTable, error) {
luaTable := L.NewTable()
// ToLua converts a struct or map to a Lua table recursively
func ToLua(L *lua.LState, data interface{}) (lua.LValue, error) {
switch v := data.(type) {
case map[string]interface{}:
luaTable := L.NewTable()
for key, value := range v {
luaValue, err := ToLuaTable(L, value)
luaValue, err := ToLua(L, value)
if err != nil {
return nil, err
}
luaTable.RawSetString(key, luaValue)
}
case struct{}:
val := reflect.ValueOf(v)
for i := 0; i < val.NumField(); i++ {
field := val.Type().Field(i)
luaValue, err := ToLuaTable(L, val.Field(i).Interface())
return luaTable, nil
case []interface{}:
luaTable := L.NewTable()
for i, value := range v {
luaValue, err := ToLua(L, value)
if err != nil {
return nil, err
}
luaTable.RawSetString(field.Name, luaValue)
luaTable.RawSetInt(i+1, luaValue) // Lua arrays are 1-indexed
}
return luaTable, nil
case string:
luaTable.RawSetString("v", lua.LString(v))
return lua.LString(v), nil
case bool:
luaTable.RawSetString("v", lua.LBool(v))
return lua.LBool(v), nil
case float64:
luaTable.RawSetString("v", lua.LNumber(v))
return lua.LNumber(v), nil
default:
return nil, fmt.Errorf("unsupported data type: %T", data)
}
return luaTable, nil
}
// FromLuaTable converts a Lua table to a struct or map recursively
func FromLuaTable(L *lua.LState, luaTable *lua.LTable) (map[string]interface{}, error) {
result := make(map[string]interface{})
luaTable.ForEach(func(key lua.LValue, value lua.LValue) {
switch v := value.(type) {
case *lua.LTable:
nestedMap, err := FromLuaTable(L, v)
// FromLua converts a Lua table to a struct or map recursively
func FromLua(L *lua.LState, luaValue lua.LValue) (interface{}, error) {
switch v := luaValue.(type) {
case *lua.LTable:
result := make(map[string]interface{})
v.ForEach(func(key lua.LValue, value lua.LValue) {
result[key.String()], _ = FromLua(L, value)
})
// This may be a bit wasteful...
// Hopefully it won't run often enough to matter
isArray := true
for key := range result {
_, err := strconv.Atoi(key)
if err != nil {
return
isArray = false
break
}
result[key.String()] = nestedMap
case lua.LString:
result[key.String()] = string(v)
case lua.LBool:
result[key.String()] = bool(v)
case lua.LNumber:
result[key.String()] = float64(v)
default:
result[key.String()] = nil
}
})
return result, nil
if isArray {
list := make([]interface{}, 0, len(result))
for _, value := range result {
list = append(list, value)
}
return list, nil
}
return result, nil
case lua.LString:
return string(v), nil
case lua.LBool:
return bool(v), nil
case lua.LNumber:
return float64(v), nil
default:
return nil, nil
}
}
// InitLuaHelpers initializes common Lua helper functions