From 751e381230762caa6937e9c516de82fd27c59250 Mon Sep 17 00:00:00 2001 From: unwox Date: Fri, 23 Aug 2024 21:14:30 +0600 Subject: pass a single response object instead of multiple arguments to lua --- lua.go | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'lua.go') diff --git a/lua.go b/lua.go index a150e27..875aac0 100644 --- a/lua.go +++ b/lua.go @@ -151,13 +151,36 @@ func (l *Lua) SetTableItem(key string) { C.lua_setfield(l.l, -2, C.CString(key)) } -// PushStringTable pushes string->string table onto the stack. -func (l *Lua) PushStringTable(table map[string]string) { - l.CreateTable(len(table)) - for k, v := range table { - l.PushString(v) - C.lua_setfield(l.l, -2, C.CString(k)) +// PushTable pushes string->any table onto the stack. +func (l *Lua) PushObject(table map[string]any) error { + var pushTable func(t map[string]any) error + pushTable = func (t map[string]any) error { + l.CreateTable(len(t)) + for k, v := range t { + switch v.(type) { + case string: + v, _ := v.(string) + l.PushString(v) + case func (l *Lua) int: + v, _ := v.(func (l *Lua) int) + l.PushGoFunction(v) + case int: + v, _ := v.(int) + l.PushNumber(v) + case map[string]any: + v, _ := v.(map[string]any) + pushTable(v) + default: + return fmt.Errorf( + "unsupported value type: %T", + v, + ) + } + l.SetTableItem(k) + } + return nil } + return pushTable(table) } // PushFromRef pushes a value from registry ref onto the stack. -- cgit v1.2.3