-
Notifications
You must be signed in to change notification settings - Fork 0
/
condition.go
117 lines (100 loc) · 3.44 KB
/
condition.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
package yabre
import (
"fmt"
"github.com/dop251/goja"
)
type Condition struct {
Default bool `yaml:"default"`
Name string `yaml:"name"`
Description string `yaml:"description"`
Check string `yaml:"check"`
True *Decision `yaml:"true"`
False *Decision `yaml:"false"`
}
type Decision struct {
Name string `yaml:"-"`
Description string `yaml:"description"`
Action string `yaml:"action"`
Next string `yaml:"next"`
Terminate bool `yaml:"terminate"`
Value bool `yaml:"-"`
}
func (cr *Decision) UnmarshalYAML(unmarshal func(interface{}) error) error {
type decision Decision // we need to create an intermediate type to avoid infinite recursion
var dsn decision
if err := unmarshal(&dsn); err != nil {
return err
}
if dsn.Next != "" && dsn.Terminate {
return fmt.Errorf("next and terminate cannot be used together")
}
*cr = Decision(dsn)
return nil
}
// Run the conditions recursively
func (runner *RulesRunner[Context]) runCondition(vm *goja.Runtime, rules *Rules, condition *Condition) error {
runner.decisionCallback("Evaluating condition: [%s] %s", condition.Name, condition.Description)
// Get the custom function name for the check function
checkFuncName := runner.getFunctionName(condition.Name)
// Evaluate the check function
checkFunc, ok := goja.AssertFunction(vm.Get(checkFuncName))
if !ok {
return fmt.Errorf("check function not found: %s", checkFuncName)
}
checkResult, err := checkFunc(goja.Undefined())
if err != nil {
return fmt.Errorf("error evaluating check function %s: %v", checkFuncName, err)
}
if checkResult.ToBoolean() {
runner.decisionCallback("Condition [%s] evaluated to [true]", condition.Name)
if condition.True == nil {
runner.decisionCallback("No action or next condition defined, terminating")
return nil
}
return runner.runAction(vm, rules, condition.True)
} else {
runner.decisionCallback("Condition [%s] evaluated to [false]", condition.Name)
if condition.False == nil {
runner.decisionCallback("No action or next condition defined, terminating")
return nil
}
return runner.runAction(vm, rules, condition.False)
}
}
// Helper function to run the action
func (runner *RulesRunner[Context]) runAction(vm *goja.Runtime, rules *Rules, result *Decision) error {
if result.Action != "" {
actionFuncName := runner.getFunctionName(result.Name)
runner.decisionCallback("Running action: [%s] %s", actionFuncName, result.Description)
actionFunc, ok := goja.AssertFunction(vm.Get(actionFuncName))
if !ok {
return fmt.Errorf("action function not found: %s", actionFuncName)
}
_, err := actionFunc(goja.Undefined())
if err != nil {
return fmt.Errorf("error running action: %v", err)
}
}
if result.Next != "" {
nextCondition, err := findConditionByName(rules, result.Next)
if err != nil {
return fmt.Errorf("unexpected error: condition '%s' not found", result.Next)
}
runner.decisionCallback("Moving to next condition:[%s]", nextCondition.Name)
err = runner.runCondition(vm, rules, nextCondition)
if err != nil {
return fmt.Errorf("error while evaluating condition '%s': %v", result.Next, err)
}
}
if result.Terminate {
runner.decisionCallback("Terminating")
return nil
}
return nil
}
func findConditionByName(rule *Rules, name string) (*Condition, error) {
if condition, ok := rule.Conditions[name]; ok {
return &condition, nil
}
return nil, fmt.Errorf("Condition not found: %s", name)
}