mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-21 09:28:14 +00:00
61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
package plugin
|
|
|
|
import lua "github.com/yuin/gopher-lua"
|
|
|
|
func luaValueToGo(lv lua.LValue) interface{} {
|
|
switch v := lv.(type) {
|
|
case lua.LString:
|
|
return string(v)
|
|
case lua.LNumber:
|
|
return float64(v)
|
|
case lua.LBool:
|
|
return bool(v)
|
|
case *lua.LTable:
|
|
if v.MaxN() > 0 {
|
|
arr := make([]interface{}, 0, v.MaxN())
|
|
v.ForEach(func(_, val lua.LValue) {
|
|
arr = append(arr, luaValueToGo(val))
|
|
})
|
|
return arr
|
|
}
|
|
m := make(map[string]interface{})
|
|
v.ForEach(func(key, val lua.LValue) {
|
|
m[key.String()] = luaValueToGo(val)
|
|
})
|
|
return m
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func goValueToLua(L *lua.LState, val interface{}) lua.LValue {
|
|
switch v := val.(type) {
|
|
case string:
|
|
return lua.LString(v)
|
|
case float64:
|
|
return lua.LNumber(v)
|
|
case int:
|
|
return lua.LNumber(v)
|
|
case int64:
|
|
return lua.LNumber(v)
|
|
case bool:
|
|
return lua.LBool(v)
|
|
case nil:
|
|
return lua.LNil
|
|
case []interface{}:
|
|
tbl := L.NewTable()
|
|
for i, item := range v {
|
|
tbl.RawSetInt(i+1, goValueToLua(L, item))
|
|
}
|
|
return tbl
|
|
case map[string]interface{}:
|
|
tbl := L.NewTable()
|
|
for k, item := range v {
|
|
tbl.RawSetString(k, goValueToLua(L, item))
|
|
}
|
|
return tbl
|
|
default:
|
|
return lua.LNil
|
|
}
|
|
}
|