-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.go
120 lines (99 loc) · 3.27 KB
/
rules.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package yabre
import (
"fmt"
"regexp"
"github.com/dop251/goja"
"gopkg.in/yaml.v2"
)
type Rules struct {
Scripts string `yaml:"scripts"`
Conditions map[string]Condition `yaml:"conditions"`
DefaultCondition *Condition `yaml:"-"`
}
// Perform enrichment and validation of rules data during unmarshalling
func (r *Rules) UnmarshalYAML(unmarshal func(interface{}) error) error {
type rules Rules // we need to create an intermediate type to avoid infinite recursion
var rr rules
if err := unmarshal(&rr); err != nil {
return err
}
defaultFound := false
// add names to conditions
// and find the default condition
for name, condition := range rr.Conditions {
condition.Name = name
if condition.True != nil {
condition.True.Name = condition.Name + "_true"
condition.True.Value = true
}
if condition.False != nil {
condition.False.Name = condition.Name + "_false"
condition.False.Value = false
}
rr.Conditions[name] = condition
if condition.Default {
if !defaultFound {
rr.DefaultCondition = &condition
defaultFound = true
} else {
return fmt.Errorf("multiple default conditions found")
}
}
}
*r = Rules(rr)
return nil
}
func (rr *RulesRunner[Context]) loadRulesFromYaml(yamlFile []byte) (*Rules, error) {
// Parse the YAML into a Rule struct
var rules Rules
err := yaml.Unmarshal(yamlFile, &rules)
if err != nil {
return nil, fmt.Errorf("error parsing YAML: %v", err)
}
// Now unmarshal the same yaml into an ordered list to get the first condition
return &rules, nil
}
func (runner *RulesRunner[Context]) addJsFunctions(vm *goja.Runtime) error {
// add all js functions to the vm
if runner.Rules.Scripts != "" {
_, err := vm.RunString(runner.Rules.Scripts)
if err != nil {
return fmt.Errorf("error injecting scripts into vm: %v", err)
}
}
for _, condition := range runner.Rules.Conditions {
if condition.Check != "" {
checkName := condition.Name
if err := runner.injectJSFunction(vm, checkName, condition.Check); err != nil {
return fmt.Errorf("error injecting condition function into vm: %v", err)
}
}
if condition.True != nil && condition.True.Action != "" {
actionName := fmt.Sprintf("%s_%t", condition.Name, condition.True.Value)
if err := runner.injectJSFunction(vm, actionName, condition.True.Action); err != nil {
return fmt.Errorf("error injecting action function into vm: %v", err)
}
}
if condition.False != nil && condition.False.Action != "" {
actionName := fmt.Sprintf("%s_%t", condition.Name, condition.False.Value)
if err := runner.injectJSFunction(vm, actionName, condition.False.Action); err != nil {
return fmt.Errorf("error injecting action function into vm: %v", err)
}
}
}
return nil
}
func (runner *RulesRunner[Context]) injectJSFunction(vm *goja.Runtime, defaultName, funcCode string) error {
funcName := defaultName
re := regexp.MustCompile(`function\s+(\w+)\s*\(`)
matches := re.FindStringSubmatch(funcCode)
if len(matches) > 1 {
funcName = matches[1]
}
runner.functionNames[defaultName] = funcName // Store the function name mapping
_, err := vm.RunString(fmt.Sprintf("%s = %s", funcName, funcCode))
if err != nil {
return fmt.Errorf("error injecting function %s into vm: %v", funcName, err)
}
return nil
}