-
Notifications
You must be signed in to change notification settings - Fork 1
/
rule_result.go
96 lines (84 loc) · 2.04 KB
/
rule_result.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
package rulesengine
import (
"encoding/json"
"sync"
)
// RuleResult represents the result of a rule evaluation
type RuleResult struct {
Conditions Condition
Event Event
Priority int
Name string
Result *bool
mu sync.Mutex
}
// NewRuleResult creates a new RuleResult instance
func NewRuleResult(conditions Condition, event Event, priority int, name string) *RuleResult {
return &RuleResult{
Conditions: conditions,
Event: event,
Priority: priority,
Name: name,
Result: nil,
}
}
// SetResult sets the result of the rule evaluation
func (rr *RuleResult) SetResult(result *bool) {
rr.mu.Lock()
defer rr.mu.Unlock()
rr.Result = result
}
// ResolveEventParams resolves the event parameters using the given almanac
func (rr *RuleResult) ResolveEventParams(almanac *Almanac) error {
if IsObjectLike(rr.Event.Params) {
var wg sync.WaitGroup
var mu sync.Mutex
errorsCh := make(chan error, len(rr.Event.Params))
for key, value := range rr.Event.Params {
wg.Add(1)
go func(key string, value interface{}) {
defer wg.Done()
// check if value is a string
if IsObjectLike(value) {
valMap, ok := value.(map[string]interface{})
if ok {
if factPath, ok := valMap["fact"].(string); ok {
resolvedValue, err := almanac.GetValue(factPath)
if err != nil {
errorsCh <- err
return
}
mu.Lock()
rr.Event.Params[key] = resolvedValue
mu.Unlock()
}
}
}
}(key, value)
}
wg.Wait()
close(errorsCh)
if len(errorsCh) > 0 {
return <-errorsCh
}
}
return nil
}
// ToJSON converts the rule result to a JSON-friendly structure
func (rr *RuleResult) ToJSON(stringify bool) (interface{}, error) {
props := map[string]interface{}{
"conditions": rr.Conditions,
"event": rr.Event,
"priority": rr.Priority,
"name": rr.Name,
"result": rr.Result,
}
if stringify {
jsonStr, err := json.Marshal(props)
if err != nil {
return nil, err
}
return string(jsonStr), nil
}
return props, nil
}