mirror of
https://github.com/metabarcoding/obitools4.git
synced 2025-06-29 16:20:46 +00:00
Patch conversion from lua table to go slice or map
Former-commit-id: d94b879215374438a096e4b80a49b362266149ac
This commit is contained in:
@ -64,7 +64,7 @@ func pushMapStringInterfaceToLua(L *lua.LState, m map[string]interface{}) {
|
||||
case string:
|
||||
luaTable.RawSetString(key, lua.LString(v))
|
||||
default:
|
||||
log.Fatalf("Doesn't deal with map containing value %v", v)
|
||||
log.Fatalf("Doesn't deal with map containing value %v of type %T", v, v)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,17 @@ func Table2Interface(interpreter *lua.LState, table *lua.LTable) interface{} {
|
||||
if isArray {
|
||||
val := make([]interface{}, table.Len())
|
||||
for i := 1; i <= table.Len(); i++ {
|
||||
val[i-1] = table.RawGetInt(i)
|
||||
v := table.RawGetInt(i)
|
||||
switch v.Type() {
|
||||
case lua.LTNil:
|
||||
val[i-1] = nil
|
||||
case lua.LTBool:
|
||||
val[i-1] = bool(v.(lua.LBool))
|
||||
case lua.LTNumber:
|
||||
val[i-1] = float64(v.(lua.LNumber))
|
||||
case lua.LTString:
|
||||
val[i-1] = string(v.(lua.LString))
|
||||
}
|
||||
}
|
||||
return val
|
||||
} else {
|
||||
@ -22,7 +32,16 @@ func Table2Interface(interpreter *lua.LState, table *lua.LTable) interface{} {
|
||||
val := make(map[string]interface{})
|
||||
table.ForEach(func(k, v lua.LValue) {
|
||||
if ks, ok := k.(lua.LString); ok {
|
||||
val[string(ks)] = v
|
||||
switch v.Type() {
|
||||
case lua.LTNil:
|
||||
val[string(ks)] = nil
|
||||
case lua.LTBool:
|
||||
val[string(ks)] = bool(v.(lua.LBool))
|
||||
case lua.LTNumber:
|
||||
val[string(ks)] = float64(v.(lua.LNumber))
|
||||
case lua.LTString:
|
||||
val[string(ks)] = string(v.(lua.LString))
|
||||
}
|
||||
}
|
||||
})
|
||||
return val
|
||||
|
Reference in New Issue
Block a user