-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.go
132 lines (107 loc) · 3.36 KB
/
runner.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
121
122
123
124
125
126
127
128
129
130
131
132
package yabre
import (
"fmt"
"github.com/dop251/goja"
)
type RulesRunner[Context interface{}] struct {
Rules *Rules
Context *Context
debugCallback func(...interface{})
goFunctions map[string]func(...interface{}) (interface{}, error)
// callback to be called when a decision is made
decisionCallback func(msg string, args ...interface{})
// mapping of js functions in business rules to standard names
functionNames map[string]string
}
type WithOption[Context interface{}] func(*RulesRunner[Context]) error
// WithDebugCallback sets the DebugCallback option
func WithDebugCallback[Context interface{}](callback func(...interface{})) WithOption[Context] {
return func(runner *RulesRunner[Context]) error {
runner.debugCallback = callback
return nil
}
}
func WithGoFunction[Context interface{}](name string, f any) WithOption[Context] {
return func(runner *RulesRunner[Context]) error {
if runner.goFunctions == nil {
runner.goFunctions = make(map[string]func(...interface{}) (interface{}, error))
}
var fn func(...interface{}) (interface{}, error)
// if the function is NOT of expected signature `func(...interface{}) (interface{}, error)` then wrap it
dontWrap, err := checkVariadicAnySignature(f)
if err != nil {
return fmt.Errorf("invalid go function signature: %w", err)
} else if !dontWrap {
fn = goFuncWrapper(f)
} else {
fn = f.(func(...interface{}) (interface{}, error))
}
runner.goFunctions[name] = fn
return nil
}
}
func WithDecisionCallback[Context interface{}](callback func(msg string, args ...interface{})) WithOption[Context] {
return func(runner *RulesRunner[Context]) error {
runner.decisionCallback = callback
return nil
}
}
func (runner *RulesRunner[Context]) getFunctionName(name string) string {
if functionName, ok := runner.functionNames[name]; ok {
return functionName
}
return name
}
func NewRulesRunnerFromYaml[Context interface{}](yamlData []byte, context *Context, options ...WithOption[Context]) (*RulesRunner[Context], error) {
runner := &RulesRunner[Context]{
Context: context,
functionNames: map[string]string{},
decisionCallback: func(msg string, args ...interface{}) {},
}
// Execute options
for _, op := range options {
err := op(runner)
if err != nil {
return nil, err
}
}
// Load the rules from the YAML data
rules, err := runner.loadRulesFromYaml(yamlData)
if err != nil {
return nil, err
}
runner.Rules = rules
return runner, nil
}
func (rr *RulesRunner[Context]) RunRules(context *Context, startCondition *Condition) (*Context, error) {
rules := rr.Rules
vm := goja.New()
// Add context to vm
vm.Set("context", *context)
// Add debug function to vm
if rr.debugCallback != nil {
vm.Set("debug", rr.debugCallback)
}
// Add go functions to vm
if rr.goFunctions != nil {
for name, f := range rr.goFunctions {
vm.Set(name, f)
}
}
// Add all js functions to the vm
err := rr.addJsFunctions(vm)
if err != nil {
return nil, err
}
if startCondition == nil {
startCondition = rules.DefaultCondition
}
if startCondition == nil && rules.DefaultCondition == nil {
return nil, fmt.Errorf("no default condition found")
}
// Start running the conditions from the first condition
err = rr.runCondition(vm, rules, startCondition)
// Get the updated context
*context = vm.Get("context").ToObject(vm).Export().(Context)
return context, err
}