mirror of
https://github.com/akyaiy/GoSally-mvp.git
synced 2026-01-03 15:12:26 +00:00
move lua types converters from utils to sv1
This commit is contained in:
@@ -1,78 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func ConvertLuaTypesToGolang(value lua.LValue) any {
|
||||
switch value.Type() {
|
||||
case lua.LTString:
|
||||
return value.String()
|
||||
case lua.LTNumber:
|
||||
return float64(value.(lua.LNumber))
|
||||
case lua.LTBool:
|
||||
return bool(value.(lua.LBool))
|
||||
case lua.LTTable:
|
||||
tbl := value.(*lua.LTable)
|
||||
|
||||
// Попробуем как массив
|
||||
var arr []any
|
||||
isArray := true
|
||||
tbl.ForEach(func(key, val lua.LValue) {
|
||||
if key.Type() != lua.LTNumber {
|
||||
isArray = false
|
||||
}
|
||||
arr = append(arr, ConvertLuaTypesToGolang(val))
|
||||
})
|
||||
|
||||
if isArray {
|
||||
return arr
|
||||
}
|
||||
|
||||
result := make(map[string]any)
|
||||
tbl.ForEach(func(key, val lua.LValue) {
|
||||
result[key.String()] = ConvertLuaTypesToGolang(val)
|
||||
})
|
||||
return result
|
||||
|
||||
case lua.LTNil:
|
||||
return nil
|
||||
default:
|
||||
return value.String()
|
||||
}
|
||||
}
|
||||
|
||||
func ConvertGolangTypesToLua(L *lua.LState, val any) lua.LValue {
|
||||
switch v := val.(type) {
|
||||
case string:
|
||||
return lua.LString(v)
|
||||
case bool:
|
||||
return lua.LBool(v)
|
||||
case int:
|
||||
return lua.LNumber(float64(v))
|
||||
case int64:
|
||||
return lua.LNumber(float64(v))
|
||||
case float32:
|
||||
return lua.LNumber(float64(v))
|
||||
case float64:
|
||||
return lua.LNumber(v)
|
||||
case []any:
|
||||
tbl := L.NewTable()
|
||||
for i, item := range v {
|
||||
tbl.RawSetInt(i+1, ConvertGolangTypesToLua(L, item))
|
||||
}
|
||||
return tbl
|
||||
case map[string]any:
|
||||
tbl := L.NewTable()
|
||||
for key, value := range v {
|
||||
tbl.RawSetString(key, ConvertGolangTypesToLua(L, value))
|
||||
}
|
||||
return tbl
|
||||
case nil:
|
||||
return lua.LNil
|
||||
default:
|
||||
return lua.LString(fmt.Sprintf("%v", v))
|
||||
}
|
||||
}
|
||||
@@ -7,12 +7,13 @@ import (
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/akyaiy/GoSally-mvp/internal/core/utils"
|
||||
"github.com/akyaiy/GoSally-mvp/internal/engine/logs"
|
||||
"github.com/akyaiy/GoSally-mvp/internal/server/rpc"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
|
||||
|
||||
func (h *HandlerV1) handleLUA(path string, req *rpc.RPCRequest) *rpc.RPCResponse {
|
||||
L := lua.NewState()
|
||||
defer L.Close()
|
||||
@@ -21,7 +22,7 @@ func (h *HandlerV1) handleLUA(path string, req *rpc.RPCRequest) *rpc.RPCResponse
|
||||
paramsTable := L.NewTable()
|
||||
if fetchedParams, ok := req.Params.(map[string]any); ok {
|
||||
for k, v := range fetchedParams {
|
||||
L.SetField(paramsTable, k, utils.ConvertGolangTypesToLua(L, v))
|
||||
L.SetField(paramsTable, k, ConvertGolangTypesToLua(L, v))
|
||||
}
|
||||
}
|
||||
L.SetField(inTable, "Params", paramsTable)
|
||||
@@ -34,29 +35,20 @@ func (h *HandlerV1) handleLUA(path string, req *rpc.RPCRequest) *rpc.RPCResponse
|
||||
|
||||
logTable := L.NewTable()
|
||||
|
||||
L.SetField(logTable, "Info", L.NewFunction(func(L *lua.LState) int {
|
||||
msg := L.ToString(1)
|
||||
h.x.SLog.Info(fmt.Sprintf("the script says: %s", msg), slog.String("script", path))
|
||||
return 0
|
||||
}))
|
||||
logFuncs := map[string]func(string, ...any){
|
||||
"Info": h.x.SLog.Info,
|
||||
"Debug": h.x.SLog.Debug,
|
||||
"Error": h.x.SLog.Error,
|
||||
"Warn": h.x.SLog.Warn,
|
||||
}
|
||||
|
||||
L.SetField(logTable, "Debug", L.NewFunction(func(L *lua.LState) int {
|
||||
msg := L.ToString(1)
|
||||
h.x.SLog.Debug(fmt.Sprintf("the script says: %s", msg), slog.String("script", path))
|
||||
return 0
|
||||
}))
|
||||
|
||||
L.SetField(logTable, "Error", L.NewFunction(func(L *lua.LState) int {
|
||||
msg := L.ToString(1)
|
||||
h.x.SLog.Error(fmt.Sprintf("the script says: %s", msg), slog.String("script", path))
|
||||
return 0
|
||||
}))
|
||||
|
||||
L.SetField(logTable, "Warn", L.NewFunction(func(L *lua.LState) int {
|
||||
msg := L.ToString(1)
|
||||
h.x.SLog.Warn(fmt.Sprintf("the script says: %s", msg), slog.String("script", path))
|
||||
return 0
|
||||
}))
|
||||
for name, logFunc := range logFuncs {
|
||||
L.SetField(logTable, name, L.NewFunction(func(L *lua.LState) int {
|
||||
msg := L.ToString(1)
|
||||
logFunc(fmt.Sprintf("the script says: %s", msg), slog.String("script", path))
|
||||
return 0
|
||||
}))
|
||||
}
|
||||
|
||||
L.SetField(logTable, "Event", L.NewFunction(func(L *lua.LState) int {
|
||||
msg := L.ToString(1)
|
||||
@@ -120,7 +112,7 @@ func (h *HandlerV1) handleLUA(path string, req *rpc.RPCRequest) *rpc.RPCResponse
|
||||
|
||||
out := make(map[string]any)
|
||||
resultTbl.ForEach(func(key lua.LValue, value lua.LValue) {
|
||||
out[key.String()] = utils.ConvertLuaTypesToGolang(value)
|
||||
out[key.String()] = ConvertLuaTypesToGolang(value)
|
||||
})
|
||||
|
||||
out["responsible-node"] = h.cs.UUID32
|
||||
|
||||
Reference in New Issue
Block a user