mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-21 17:38:10 +00:00
feat: restructure plugin system, add Lua plugin support, update docs
This commit is contained in:
60
internal/plugin/lua_util.go
Normal file
60
internal/plugin/lua_util.go
Normal file
@ -0,0 +1,60 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user