forked from hootrhino/rulex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule.go
84 lines (74 loc) · 1.75 KB
/
rule.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package typex
import (
"errors"
"reflect"
luajson "github.com/wwhai/gopher-json"
"rulex/utils"
lua "github.com/yuin/gopher-lua"
)
//
//
//
type Rule struct {
Id string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
VM *lua.LState `json:"-"`
From []string `json:"from"`
Actions string `json:"actions"`
Success string `json:"success"`
Failed string `json:"failed"`
}
//
// New
//
func NewRule(e RuleX,
name string,
description string,
from []string,
success string,
actions string,
failed string) *Rule {
vm := lua.NewState(lua.Options{
RegistrySize: 1024 * 20,
RegistryMaxSize: 1024 * 80,
RegistryGrowStep: 32,
})
// LoadTargetLib(e, vm)
// LoadJqLib(e, vm)
luajson.Preload(vm)
return &Rule{
Id: utils.MakeUUID("RULE"),
Name: name,
Description: description,
From: from,
Actions: actions,
Success: success,
Failed: failed,
VM: vm,
}
}
// LUA Callback : Success
func (r *Rule) ExecuteSuccess() (interface{}, error) {
return Execute(r.VM, "Success")
}
// LUA Callback : Failed
func (r *Rule) ExecuteFailed(arg lua.LValue) (interface{}, error) {
return Execute(r.VM, "Failed", arg)
}
//
func (r *Rule) ExecuteActions(arg lua.LValue) (lua.LValue, error) {
table := r.VM.GetGlobal("Actions")
if table != nil && table.Type() == lua.LTTable {
funcs := make(map[string]*lua.LFunction)
table.(*lua.LTable).ForEach(func(idx, f lua.LValue) {
t := reflect.TypeOf(f).Elem().Name()
if t == "LFunction" {
funcs[idx.String()] = f.(*lua.LFunction)
}
})
return RunPipline(r.VM, funcs, arg)
} else {
return nil, errors.New("Actions not a lua table or not exist")
}
}