From 39dd3e3ce8b93aa287f5f6f678f787b09307eecf Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Wed, 4 Dec 2024 10:58:17 +0100 Subject: [PATCH] Add management of []interface{} to the LUA API. --- pkg/obilua/lua_push_interface.go | 25 +++++++++++++++++++++++++ pkg/obioptions/version.go | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pkg/obilua/lua_push_interface.go b/pkg/obilua/lua_push_interface.go index 2e57ff1..8d983fc 100644 --- a/pkg/obilua/lua_push_interface.go +++ b/pkg/obilua/lua_push_interface.go @@ -46,6 +46,8 @@ func pushInterfaceToLua(L *lua.LState, val interface{}) { pushSliceNumericToLua(L, v) case []bool: pushSliceBoolToLua(L, v) + case []interface{}: + pushSliceInterfaceToLua(L, v) case nil: L.Push(lua.LNil) case *sync.Mutex: @@ -78,6 +80,29 @@ func pushMapStringInterfaceToLua(L *lua.LState, m map[string]interface{}) { L.Push(luaTable) } +func pushSliceInterfaceToLua(L *lua.LState, s []interface{}) { + // Create a new Lua table + luaTable := L.NewTable() + // Iterate over the Go map and set the key-value pairs in the Lua table + for _, value := range s { + switch v := value.(type) { + case int: + luaTable.Append(lua.LNumber(v)) + case float64: + luaTable.Append(lua.LNumber(v)) + case bool: + luaTable.Append(lua.LBool(v)) + case string: + luaTable.Append(lua.LString(v)) + default: + log.Fatalf("Doesn't deal with slice containing value %v of type %T", v, v) + } + } + + // Push the Lua table onto the stack + L.Push(luaTable) +} + // pushMapStringIntToLua creates a new Lua table and iterates over the Go map to set key-value pairs in the Lua table. It then pushes the Lua table onto the stack. // // L *lua.LState - the Lua state diff --git a/pkg/obioptions/version.go b/pkg/obioptions/version.go index 2319e78..86414e1 100644 --- a/pkg/obioptions/version.go +++ b/pkg/obioptions/version.go @@ -7,7 +7,7 @@ import ( // TODO: The version number is extracted from git. This induces that the version // corresponds to the last commit, and not the one when the file will be // commited -var _Commit = "40fb4e9" +var _Commit = "ad2461a" var _Version = "Release 4.3.0" // Version returns the version of the obitools package.