-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
38 lines (35 loc) · 882 Bytes
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package entity
import (
"errors"
"github.com/aarzilli/golua/lua"
"github.com/mitchellh/mapstructure"
"sync"
)
type Handler struct {
ScriptPath string `mapstructure:"script_path"`
AddData *sync.Map `mapstructure:"add_data"`
Lua *lua.State `mapstructure:"-"`
}
func NewHandlers(handlerData map[string]interface{}) (map[string]Handler, error) {
if handlerData == nil {
return nil, errors.New("handlerData is nil")
}
handlers := make(map[string]Handler)
for label, handlerObj := range handlerData {
handler := Handler{}
err := mapstructure.Decode(handlerObj, &handler)
if err != nil {
return nil, err
}
if handler.ScriptPath != "" {
handler.Lua = lua.NewState()
err = handler.Lua.DoFile(handler.ScriptPath)
if err != nil {
return nil, err
}
handler.Lua.OpenLibs()
}
handlers[label] = handler
}
return handlers, nil
}