-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.go
More file actions
73 lines (57 loc) · 1.51 KB
/
base.go
File metadata and controls
73 lines (57 loc) · 1.51 KB
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
)
type Rule struct {
Body struct {
Expressions []Expression `json:"expression"`
}
}
type Operator string
func readRule(source string) (*Rule, error) {
b, err := ioutil.ReadFile(source)
if err != nil {
return nil, fmt.Errorf("failed to open file: %v", err)
}
var rule Rule
err = json.Unmarshal(b, &rule)
return &rule, err
}
// FulfillOperandsFields updates both operands of the expression.
// So they contain values from the lookup fields of event json
func (r *Rule) FulfillOperandsFields(exampleSourceFileName string) error {
var jsonData interface{}
data, err := ioutil.ReadFile(exampleSourceFileName)
if err != nil {
return fmt.Errorf("failed to open example json file: %v", err)
}
err = json.Unmarshal(data, &jsonData)
if err != nil {
return fmt.Errorf("failed to unmarshal json: %v", err)
}
for _, expression := range r.Body.Expressions {
err = expression.SetMissingFields(jsonData)
if err != nil {
return fmt.Errorf("failed to set missing fields for expression: %#v due to %v", expression, err)
}
}
return nil
}
// CreateEvaluationString prepares string for following evaluation.
func (r Rule) CreateEvaluationString() (
evaluateString string,
err error,
) {
buf := strings.Builder{}
for _, exp := range r.Body.Expressions {
stringExp, err := exp.toString()
if err != nil {
return "", fmt.Errorf("failed to prepare string for expression: %v", err)
}
buf.WriteString(stringExp)
}
return buf.String(), nil
}