Skip to content

Commit

Permalink
Fix: Detect and handle Lua arrays
Browse files Browse the repository at this point in the history
Enhanced the convert function to correctly identify and handle Lua arrays separately from maps. This ensures proper conversion of sequentially indexed tables into Go slices while maintaining the integrity of Lua maps.

Signed-off-by: Christian Roessner <c@roessner.co>
  • Loading branch information
Christian Roessner committed Nov 28, 2024
1 parent 8534bd4 commit 851ea82
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions server/lualib/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,37 @@ func LuaValueToGo(value lua.LValue) any {
case lua.LString:
return v.String()
case *lua.LTable:
table := make(map[any]any)
// Try to detect if it's an array or a map
isArray := true

mp := make(map[any]any)

if v == nil {
return table
return mp
}

v.ForEach(func(key lua.LValue, v2 lua.LValue) {
table[LuaValueToGo(key)] = LuaValueToGo(v2)
array := make([]any, 0, v.Len())

v.ForEach(func(key lua.LValue, value lua.LValue) {
if isArray && key.Type() == lua.LTNumber {
index := int(key.(lua.LNumber))
if index == len(array)+1 {
array = append(array, LuaValueToGo(value))
} else {
isArray = false
}
} else {
isArray = false
}

mp[LuaValueToGo(key)] = LuaValueToGo(value)
})

return table
if isArray {
return array
}

return mp
default:
return v.String()
}
Expand Down

0 comments on commit 851ea82

Please sign in to comment.